class_name RoomSpawn extends Area2D

var waves: Array[WaveNode] = []
var current_wave_index: int = 0
var active_enemies: int = 0
var has_triggered: bool = false

func _ready() -> void:
	body_entered.connect(_on_body_entered)
	
	for c in get_children():
		if c is WaveNode:
			waves.append(c)

func _on_body_entered(body: Node2D) -> void:
	if has_triggered:
		return
		
	if body.is_in_group("player"):
		has_triggered = true
		# cant spawn collision shape in body entered
		call_deferred("start_next_wave")

func start_next_wave() -> void:
	if current_wave_index >= waves.size():
		return
		
	var current_wave = waves[current_wave_index]
	current_wave.finished.connect(_on_finished)
	current_wave.spawn_wave()


func _on_finished() -> void:
	current_wave_index += 1
	call_deferred("start_next_wave")
