extends Node3D
var chompyboy = preload("res://enemy_chompyboy.tscn")

# Called when the node enters the scene tree for the first time.
func _ready():
	spawnwave(8)
	$wavetimer.start(10)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass
	
func spawnchompy(tramsorm):
	var newchomp = chompyboy.instantiate()
	newchomp.player = $player
	var offset = false
	for lad in $player.enemies:
		if lad.global_position.distance_to(tramsorm.origin) < 3:
			return false
	
	newchomp.set_global_transform(tramsorm)
	add_child(newchomp)
	return true
	
func spawnwave(numtospawn):
	var numspawned : int = 0
	var spawnpoints = $spawnpoints.get_children()
	spawnpoints.shuffle()
	
	if numtospawn > len(spawnpoints):
		numtospawn = len(spawnpoints)
	
	while numspawned < numtospawn:
		var point = spawnpoints.pop_back().global_transform
		while spawnchompy(point) == false:
			point.origin.y += 3
		numspawned += 1

func _on_wavetimer_timeout():
	$wavetimer.stop()
	spawnwave(6)
	$wavetimer.start(10)
