diff options
Diffstat (limited to 'Scripts/Spawner.gd')
-rw-r--r-- | Scripts/Spawner.gd | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Scripts/Spawner.gd b/Scripts/Spawner.gd new file mode 100644 index 0000000..c2c60c5 --- /dev/null +++ b/Scripts/Spawner.gd @@ -0,0 +1,57 @@ +extends Node2D +class_name Spawner + +@export var enemy : PackedScene +@export var interval_seconds : float = 10 +@export var count : int = 5 +@export var initial_delay : float = 3 +@export var curr_lvl : Node2D + +var init_timer : Timer +var timer : Timer + +var counter : int = 0 + +func _ready() -> void: + timer = Timer.new() + add_child(timer) + timer.wait_time = interval_seconds + timer.one_shot = false + timer.timeout.connect(_on_timeout) + + + init_timer = Timer.new() + add_child(init_timer) + init_timer.wait_time = initial_delay + init_timer.one_shot = true + init_timer.timeout.connect(_actual_start) + +func start(): + init_timer.start() + +func _actual_start(): + _on_timeout() + timer.start() + +func _on_timeout() -> void: + if counter >= count: + timer.stop() + return + + var new : Entity = enemy.instantiate() + new.position = position + curr_lvl.add_child(new) + new.entity_death.connect(_upon_spawned_death) + + counter += 1 + +var dead_counter = 0 + +signal all_spawned_dead + +func _upon_spawned_death() -> void: + if dead_counter >= count - 1: + print("ALL SPAWNED DEAD") + all_spawned_dead.emit() + else: + dead_counter += 1 |