extends Node

var enemy = preload('res://Enemy.tscn')
export var wave_num = 10
export var spawn_speed = 10
export var limit = 1024

var dict = {
	"arraypos":[],
	"arrayvida":[],
	"arraytype":[],
	"arraytarget":[],
	"arraymotion":[],
	"arraydirtime":[],
	'attacker':[],
}

var collisions = []
var areas = preload('res://Enemy2.tscn')

func _ready():
	
	yield(get_tree().create_timer(1.0),"timeout")
	while true:
		if dict.arraypos.size() < limit:
			for x in clamp(limit-dict.arraypos.size(),0,wave_num):
				var rot = rand_range(-PI,PI)
				var pos = (Vector2(sin(rot), cos(rot)) * 400) + Vector2(320,180)
				var vida = 100
				var array = get_node('../players').get_children()
				array.shuffle()
				if array.size() > 0:
					dict.arraytarget.append(array[0])
				
				dict.arraypos.append(pos)
				dict.arrayvida.append(vida)
				dict.arraytype.append('goblin')
				dict.attacker.append('')
				
				dict.arraymotion.append(Vector2.ZERO)
				dict.arraydirtime.append(int(rand_range(60,120)))
				
		print('there is a total of: ' + str(dict.arraypos.size()) +' enemmies')
		yield(get_tree().create_timer(spawn_speed),"timeout")

var motion = Vector2()
var speed = 2.3
var tick = 0
export var tickrate = 4

var blockmapenemy = {}

func _physics_process(delta):

#setting tickrate, to reduce executions per second
	tick += 1
	if fmod(tick,tickrate) == 0:

#dict of aprox positions of players
		var blockmap = {}
		if get_node('../players').get_child_count() == 0: return
		for z in get_node('../players').get_children():
			var x = int(z.global_position.x/20)
			var y = int(z.global_position.y/20)
			blockmap[Vector2(x,y)] = z

#yes this abomination controls the enemies, ECS code looks ugly as fuck in this engine
		for i in dict.arraypos.size():

# death checking
			if dict.arrayvida[i] > 0:

#picking new targets if the last one is invalid
				if dict.arraytarget[i] == null:
					if get_node('../players').get_child_count() == 0: return
					var array = get_node('../players').get_children()
					array.shuffle()
					if array.size() > 0:
						dict.arraytarget[i] = array[0]

#exclusive movmenet code
				if fmod(tick, dict.arraydirtime[i]) == 0:
					dict.arraymotion[i] = (dict.arraytarget[i].global_position - dict.arraypos[i]).normalized() * speed
				dict.arraypos[i] += dict.arraymotion[i]

# enemy attak on players
				var x = int(dict.arraypos[i].x/20)
				var y = int(dict.arraypos[i].y/20)
				if blockmap.has(Vector2(x,y)):
					blockmap[Vector2(x,y)].damage( -0.5, '')

#hitboxes
				var x2 = int( (dict.arraypos[i].x-50) /100)
				var y2 = int( (dict.arraypos[i].y-50) /100)
				if blockmapenemy.has(Vector2(x2,y2)):
					blockmapenemy[Vector2(x2,y2)].append(i)
				else:
					blockmapenemy[Vector2(x2,y2)] = [i]

#died
			else:
				var rot = rand_range(-PI,PI)
				dict.arraypos[i] = (Vector2(sin(rot), cos(rot)) * 400) + Vector2(320,180)
				dict.arrayvida[i] = 100
				death_count(dict.attacker[i])
				

func damage_detection(arg,dmg, attacker):
	var x = int((arg.x-50)/100)
	var y = int((arg.y-50)/100)
	if blockmapenemy.has(Vector2(x,y)):
		for i in blockmapenemy[Vector2(x,y)]:
			dict.arrayvida[i] -= dmg
			if dict.arrayvida[i] <= 0:
				dict.attacker[i] = attacker
				

var counter = 0
var score = {}

func death_count(id):
	if score.has(id):
		score[id] += 1
	else:
		score[id] = 1
	get_parent().kill_update(score[id],id)
