extends Node3D

var gateArray: Array = []
var spawnTimer: float = 0.0
@export var leftSpawnPoint: Node3D = null
@export var rightSpawnPoint: Node3D = null
@export var ground: MeshInstance3D = null

func _ready() -> void:
	GameEngine.getGE().gateSpawner = self
	
func generateGateStats(spawnFrom: Node3D) -> void:
	var newGate: Node3D = (load("res://Objects/gate_base.tscn") as PackedScene).instantiate()
	newGate.position = spawnFrom.position
	self.add_child(newGate)
	gateArray.push_back(newGate)

func _process(delta: float) -> void:
	for gate in gateArray:
		if gate.position.z <= -300:
			gateArray.erase(gate)
			gate.queue_free()
		else:
			gate.moveObsticle(GameEngine.getGE().gateMovementSpeed * GameEngine.getGE().getGameSpeed(delta))
	
	if spawnTimer >= GameEngine.getGE().gateSpawnTime:
		var spawnLeft: bool = ((randi() % GameEngine.getGE().gateSpawnChance) == 0)
		var spawnRight: bool = ((randi() % GameEngine.getGE().gateSpawnChance) == 0)
		spawnTimer = 0.0
		
		if spawnLeft:
			generateGateStats(leftSpawnPoint)
		if spawnRight:
			generateGateStats(rightSpawnPoint)
	else:
		spawnTimer += delta
