67 lines
1.5 KiB
GDScript
67 lines
1.5 KiB
GDScript
extends Path3D
|
|
class_name WaveManager
|
|
|
|
@export var level : Level
|
|
var current_wave : Wave
|
|
var current_troop : Troop
|
|
|
|
var enemies_to_spawn : int = 0
|
|
var wave_on_going : bool = false
|
|
|
|
@onready var waveTimer := $WaveTimer
|
|
@onready var troopTimer := $TroopTimer
|
|
|
|
|
|
func _ready() -> void:
|
|
troopTimer.timeout.connect(spawn_troop)
|
|
EventBus.player_defeated.connect(clearLevel)
|
|
EventBus.allowedTowerHasChange.emit.call_deferred(level.allowedTowers)
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
if GameManager.wave > 0 || level.auto_start:
|
|
spawn_manager()
|
|
|
|
|
|
func spawn_next_wave() -> void:
|
|
if level.waves.is_empty():
|
|
EventBus.player_has_won.emit()
|
|
return
|
|
|
|
current_wave = level.waves.pop_front()
|
|
GameManager.wave += 1
|
|
waveTimer.start(current_wave.wait_time_before_launch_wave)
|
|
|
|
|
|
func spawn_troop() -> void:
|
|
for n in current_troop.number_to_spawn:
|
|
add_child(current_troop.enemy.instantiate())
|
|
GameManager.enemies_alive += 1
|
|
|
|
|
|
func spawn_manager() -> void:
|
|
#Send next troop
|
|
if !waveTimer.is_stopped() || !troopTimer.is_stopped():
|
|
return
|
|
|
|
if !current_wave:
|
|
spawn_next_wave()
|
|
return
|
|
|
|
if !current_wave.troops.is_empty():
|
|
current_troop = current_wave.troops.pop_front()
|
|
if (current_troop.spawn_delay == 0):
|
|
spawn_troop()
|
|
else:
|
|
troopTimer.start(current_troop.spawn_delay)
|
|
|
|
wave_on_going = true
|
|
if GameManager.enemies_alive == 0 && current_wave.troops.is_empty():
|
|
wave_on_going = false
|
|
spawn_next_wave()
|
|
elif !current_wave.wait_for_enemy_kills:
|
|
spawn_next_wave()
|
|
|
|
|
|
func clearLevel() -> void:
|
|
queue_free()
|