extends KinematicBody2D

const GRAVITY = 600
const MAX_RUN_SPEED = 100.0

export var run_force = 0.0
export var walk_speed = 0.0
export var jump_force = 0.0
export var armor_class = 3
export var ricocheting = true
export var placeholder = false
export var health = 0

var velocity = Vector2()
var is_grounded = false
var move_left = false
var jump = false
var run = false
var anim = "run"
var siding_left = false
var attacking = false
var atack_range = false 
var too_close = false
var attack_anim_changer = false

onready var gun = get_node("main/gun")
onready var head = get_node("main/body/head")
onready var anim_node = get_node("anim")
onready var ray = get_node("main/gun/ray")
onready var particle = get_node("hurt_particle")
onready var offset = Vector2(0, 0)

func _fixed_process(delta):
	
	var new_anim = anim
	
	var new_siding_left = siding_left
	
	var ppos = Globals.get("PLAYER").get_pos()
	var pos = get_pos()
	var rpos = pos - ppos
	var rpos_x = rpos.x
	
	atack_range = abs(rpos_x) < 70 and abs(rpos_x) > 60  
	too_close = abs(rpos_x) < 60
#	print(too_close)
#	print(atack_range)
#	print(move_left)

	head.look_at(ppos)
#	print(pos.distance_to(ppos))
#	print(rpos)
#	print(abs(rpos.x))
#	print(attacking)
#	gun.look_at(ppos)
	
	var force = Vector2(0, GRAVITY)
	
	if !atack_range and !attacking:
		if rpos_x > 0:
			if !too_close:
				new_siding_left = true
				move_left = true
				run = true
				if is_grounded:
					new_anim = "run"
			else:
				move_left = false
				run = false
				if is_grounded:
					new_anim = "walk"
		else:
			if !too_close:
				new_siding_left = false
				move_left = false
				run = true
				if is_grounded:
					new_anim = "run"
			else:
				move_left = true
				run = false
				if is_grounded:
					new_anim = "walk"
		if move_left: 
			if run and velocity.x > -MAX_RUN_SPEED:
				force.x -= run_force
			elif !run:
				velocity.x = -walk_speed
			
		else:
			if run and  velocity.x < MAX_RUN_SPEED:
				force.x += run_force
			elif !run:
				 velocity.x = walk_speed
				
	elif get_node("atack_timer").get_time_left() == 0:
		attacking = true
		if siding_left:
			move_to(Vector2(pos.x - 20, pos.y))
		else:
			move_to(Vector2(pos.x + 20, pos.y))
		get_node("atack_timer").start()
		velocity.x = 0
		if attack_anim_changer:
			new_anim = "atack_2"
			attack_anim_changer = !attack_anim_changer
		else:
			new_anim = "atack"
			attack_anim_changer = !attack_anim_changer
		audio.play_sound("stroke_sword")	
		if check_melee_area() !=0 :
			Globals.get("PLAYER").apply_damage(0,0,70,0)
	
	velocity += force*delta

	var motion = velocity * delta
	
	motion = move( motion )
		
	if is_colliding():
		var normal = get_collision_normal()
		if normal.y < 0:
			is_grounded = true
		motion = normal.slide( motion ) 
#		if normal.x !=0:	
#			get_node("legs").set_scale( Vector2(-get_node("legs").get_scale().x,1))
		if motion != Vector2(0,0):
			move(motion)
		velocity = normal.slide( velocity )
	
	if !get_node("legs/fall_prevent").is_colliding() and is_grounded:
		is_grounded = false
		if move_left:
			velocity.y = -jump_force
		else:
			velocity.y = -jump_force
		audio.play_sound("jump")
		new_anim = "jumping"
	
	if (new_siding_left!=siding_left):
		if (new_siding_left):
			get_node("main").set_scale(Vector2(-1,1))
			get_node("legs").set_scale( Vector2(-get_node("legs").get_scale().x,1))
		else:
			get_node("main").set_scale(Vector2(1,1))
			get_node("legs").set_scale( Vector2(-get_node("legs").get_scale().x,1))
		siding_left=new_siding_left
	
	if (new_anim !=anim):
		anim=new_anim
		anim_node.play(anim)

func _on_atack_timer_timeout():
	attacking = false

func apply_damage(var touch_pos, var touch_rot, var damage, var delay):
	if touch_pos.x < 0:
		move_to(get_pos() + Vector2(5, 0))
	else:
		move_to(get_pos() - Vector2(5, 0))
	particle.set_param(0, rad2deg(touch_rot + PI/2))
	particle.set_emissor_offset(Vector2(0, touch_pos.y))
	particle.set_emitting(true)
#	if get_node("anim").get_current_animation() == "walk":
#		get_node("anim").play("hurt")
	audio.play_sound("hurt")
	health -= damage
	if health <= 0:
		die()
	
#	if touch_pos.x >0:
#		get_node("anim").play("hurt_a")
#	else:
#		get_node("anim").play("hurt_b")

func check_melee_area():
	return get_node("main/melee_area").get_overlapping_bodies().size()

func die():
	queue_free()
	audio.play_sound("explosion_10")
	global.show_effect(get_pos(), 2, 1)
	global.show_effect(get_pos(), 1, 1)

	
func _ready():
	pass

func _on_notifer_enter_screen():
	get_node("anim").play(anim)
	set_fixed_process(true)
