TowerDefense/Scripts/WaveManager.gd

67 lines
1.4 KiB
GDScript3
Raw Normal View History

2025-08-27 01:40:22 +02:00
extends Path3D
class_name WaveManager
2025-08-29 12:11:51 +02:00
@export var level : Level
2025-08-27 01:40:22 +02:00
var current_wave : Wave
var current_troop : Troop
var enemies_to_spawn : int = 0
var wave_on_going : bool = false
2025-08-29 12:11:51 +02:00
@onready var waveTimer := $WaveTimer
@onready var troopTimer := $TroopTimer
func _ready() -> void:
troopTimer.timeout.connect(spawn_troop)
EventBus.player_defeated.connect(clearLevel)
2025-08-27 01:40:22 +02:00
func _process(delta: float) -> void:
2025-08-29 12:11:51 +02:00
if GameManager.wave > 0 || level.auto_start:
2025-08-27 01:40:22 +02:00
spawn_manager()
func spawn_next_wave() -> void:
2025-08-29 12:11:51 +02:00
if level.waves.is_empty():
2025-08-27 01:40:22 +02:00
EventBus.player_has_won.emit()
return
2025-08-29 12:11:51 +02:00
current_wave = level.waves.pop_front()
2025-08-27 01:40:22 +02:00
GameManager.wave += 1
2025-08-29 12:11:51 +02:00
waveTimer.start(current_wave.wait_time_before_launch_wave)
2025-08-27 01:40:22 +02:00
func spawn_troop() -> void:
for n in current_troop.number_to_spawn:
2025-08-29 12:11:51 +02:00
add_child(current_troop.enemy.instantiate())
2025-08-27 01:40:22 +02:00
GameManager.enemies_alive += 1
func spawn_manager() -> void:
#Send next troop
2025-08-29 12:11:51 +02:00
if !waveTimer.is_stopped() || !troopTimer.is_stopped():
return
if !current_wave:
spawn_next_wave()
2025-08-27 01:40:22 +02:00
return
if !current_wave.troops.is_empty():
current_troop = current_wave.troops.pop_front()
if (current_troop.spawn_delay == 0):
spawn_troop()
else:
2025-08-29 12:11:51 +02:00
troopTimer.start(current_troop.spawn_delay)
2025-08-27 01:40:22 +02:00
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()
2025-08-29 12:11:51 +02:00
func clearLevel() -> void:
queue_free()