extends KinematicBody2D

var knockback_counter = 25
var knockback_counter_buff = 0


func damage(arg, id):
	vida += arg
	flash_spriter()
	knockback_counter_buff += arg
	if knockback_counter_buff < 0:
		state = 'knockback'
		clock = behaviours[state]
		knockback_counter_buff = knockback_counter
	get_tree().get_nodes_in_group('server')[0]._enemy_dmg(enemy_id)
	if vida <= 0:
		state = 'die'
		clock = behaviours[state]
		get_tree().get_nodes_in_group('enemy_authority')[0].death_count(id)
		queue_free()

var enemy_id = 0
export var type = 4
var fliph = 0
export var vida = 2
# Called when the node enters the scene tree for the first time.

var motion = Vector2(0,0)

var target
var target_position_buff

func find_target():
	var data = get_tree().get_nodes_in_group('players')
	if data.size() > 0:
		data.shuffle()
		target_position_buff = data[0].global_position
		return data[0]

func _ready(): 
	for x in get_tree().get_nodes_in_group('enemycount'):
		x.enemycount += 1
		enemy_id = x.enemycount
#		print(enemy_id)
		find_target()

var bullet = preload('res://bullet.tscn')
var firerate = 30
var fireratebuff = 30
var alert = false

var state = 'walk'
var behaviours = {
	'walkleft':120,
	'walkright':120,
	'idle':60,
	'knockback':60,
	'canon':60,
	'die':120,
	'followtarget':120,
	'blowup':60,
}

export var gen = ['walkleft','walkright','idle','canon']

var clock = 1
var onceacycle = true

func sprite_loader(anim,pos,id, auto = false):
	for x in get_tree().get_nodes_in_group('server'):
		x.spriter_request(anim,pos,id, auto)

func nuker_loader(time):
	for x in get_tree().get_nodes_in_group('server'):
		x.spriter_nuke_request(time, enemy_id) 

func flash_spriter():
	for x in get_tree().get_nodes_in_group('server'):
		x.flash_spriter(enemy_id) 

func _physics_process(delta):
	clock -= 1
	match state:
		'walkleft':
			sprite_loader('walk',global_position,enemy_id)
			motion.x = 75
			motion.y = 40
		'walkright':
			sprite_loader('walk',global_position,enemy_id)
			motion.x = -75
			motion.y = 40
		'idle':
			sprite_loader('idle',global_position,enemy_id)
			motion.x = 0
			motion.y = 40
		'knockback':
			sprite_loader('knockback',global_position,enemy_id)
			motion.x = 0
			motion.y = 40
		'canon':
			sprite_loader('canon',global_position,enemy_id)
			if clock == 30 and onceacycle:
				var instancer = load('res://enemie/tankcanonball.tscn').instance()
				add_child(instancer)
				instancer.global_position = $canonpoint.global_position
			motion.x = 0
			motion.y = 40
		'die':
			sprite_loader('die',global_position,enemy_id)
			motion.x = 0
		'followtarget':
			if target == null: 
				target = find_target()
				return
			if motion == Vector2(0,0): motion = (target_position_buff - global_position).normalized() * 150
	if clock == 0:
		gen.shuffle()
		state = gen[0]
		clock = behaviours[state]
#		print(state)
		onceacycle = true
	move_and_slide(motion)


func _on_Area2D_body_entered(body):
#	sprite_loader('explosion',global_position,enemy_id, true)
	if body.is_in_group('player'):
		sprite_loader('explosion',global_position,enemy_id, true)
		body.damage(-20, enemy_id)
#		nuker_loader(1.0)
		queue_free()
	if body.is_in_group('terrain'):
		sprite_loader('explosion',global_position,enemy_id, true)
#		nuker_loader(1.0)
		queue_free()
	pass # Replace with function body.

