extends Node2D

# Wave stuff.
@export_category("Waves")
@export var WaveWaits: Array[float]
@export var WaveScenes: Array[PackedScene]
var WaveTimer: float = 0
var CurrentWave: int = 0
# Start to make sure no other waves spawn when fading.
var Start: bool = false

# Fading happens at the beginning and end of the level.
@export_category("Fading")
@export var BeginningFades: bool = true
@export var BeginningFadeTime: float = 3
@export var BeginningFade: PackedScene
@export var EndingFades: bool = true
@export var EndingFadeTime: float = 3
@export var EndingFade: PackedScene

func _ready():
	# This addition here is to make sure that the fade completes.
	if WaveWaits.size() != WaveScenes.size():
		push_error("The WaveWaits and the WaveScenes don't line up! Are you missing something?")
	if BeginningFades == true:
		Spawn(BeginningFade)

func _process(delta: float):
	WaveTimer += delta
	if Start == false and WaveTimer >= BeginningFadeTime:
		Start = true
		WaveTimer = 0
	if CurrentWave != WaveScenes.size() and Start == true:
		if WaveTimer >= WaveWaits[CurrentWave]:
			Spawn(WaveScenes[CurrentWave])
			CurrentWave += 1
	if CurrentWave == WaveScenes.size() and WaveTimer >= EndingFadeTime:
		Spawn(EndingFade)

func Spawn(Wave: PackedScene):
	var thing = Wave.instantiate()
	self.add_child(thing)
	thing.transform = self.global_transform
	WaveTimer = 0
