summaryrefslogtreecommitdiff
path: root/Scripts/Spawner.gd
blob: c2c60c5d232187bd76fd049017bb75bb4847b6ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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