PirateTreasureHunt/scenes/CoinSpawner.gd

48 lines
1.1 KiB
GDScript

extends Node2D
var Coin = preload("res://scenes/Coin.tscn")
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var coin_spread = 100
var coin_lifetime = 5
# 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 = []
for i in range (num):
var coin = Coin.instance()
coin.position = position
if _position != null:
coin.position = _position
coin.velocity = Vector2 (rand_range(-coin_spread, coin_spread), rand_range (-coin_spread, coin_spread))
coin.lifetime = coin_lifetime
coin.z_pos = 0
coin.z_vel = rand_range(300, 800)
result.push_back(coin)
return result
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)