PirateTreasureHunt/scenes/CoinSpawner.gd

46 lines
1.1 KiB
GDScript
Raw Normal View History

2022-01-23 11:47:18 +01:00
extends Node2D
onready var Coin = preload("./Coin.tscn")
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var coin_spread = 100
2022-01-23 11:47:18 +01:00
# Called when the node enters the scene tree for the first time.
func _ready():
assert (Coin != null)
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
func create_coins (num, _position = null):
var result = []
2022-01-23 11:47:18 +01:00
for i in range (num):
var coin = Coin.instance()
coin.position = position
if _position != null:
coin.position = _position
coin.velocity = Vector2 (2 * rand_range(-coin_spread, coin_spread), rand_range (-coin_spread, coin_spread))
2022-01-23 11:47:18 +01:00
coin.z_pos = 0
coin.z_vel = rand_range(300, 800)
result.push_back(coin)
return result
2022-01-23 11:47:18 +01:00
func _unhandled_input(event):
if 'position' in event:
var relative_pos = self.transform.affine_inverse() * event.position
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
var coins = create_coins(25, relative_pos)
for coin in coins:
add_child(coin)