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 health = 0
export var follow_player = false

var velocity = Vector2()
var is_grounded = false
var move_left = false
var move_right = false
var jump = false
var anim = "walk"
var siding_left = false
var too_close = false
var bullet = preload("res://scenes/items/plasma_bullet.scn")

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

func _fixed_process(delta):
	
	var force = Vector2(0, GRAVITY)
	
	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
	
	gun.look_at(ppos)
	head.look_at(ppos)
	
	too_close = abs(rpos_x) < 60
	
	velocity += force*delta
	var motion = velocity * delta
	motion = move( motion )
	
	if follow_player:
	
		if rpos_x > 0:
				new_siding_left = true
				move_left = true
				move_right = false
				new_anim = "walk"
		else:
				new_siding_left = false
				move_right = true
				move_left = false
				new_anim = "walk"
				
		if !too_close:
			if move_left and velocity.x > -MAX_RUN_SPEED:
				force.x -= run_force
			elif move_right and velocity.x < MAX_RUN_SPEED:
				force.x += run_force
		else:
			velocity.x = 0
			new_anim = "idle"
			
		if is_colliding():
			var normal = get_collision_normal()
			if normal.y < 0:
				is_grounded = true
			motion = normal.slide( motion )
			if motion != Vector2(0,0):
				move(motion)
			velocity = normal.slide( velocity )
		
		if (new_siding_left!=siding_left):
			if (new_siding_left):
				get_node("main").set_scale(Vector2(-1,1))
				get_node("legs").set_scale(Vector2(-1,1))
			else:
				get_node("main").set_scale(Vector2(1,1))
				get_node("legs").set_scale(Vector2(1,1))
			siding_left=new_siding_left
			
		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")
	else:
		
		if rpos_x > 0:
				new_siding_left = true
		else:
				new_siding_left = false
		
		if move_left:
			velocity.x = -walk_speed
		else:
			velocity.x = walk_speed
		
		if !get_node("legs/fall_prevent").is_colliding():
			if move_left:
				get_node("legs").set_scale(Vector2(1,1))
				move_left = false
			else:
				get_node("legs").set_scale(Vector2(-1,1))
				move_left= true
				
		if is_colliding():
			var normal = get_collision_normal()
			if normal.x != 0:
				if move_left:
					get_node("legs").set_scale(Vector2(1,1))
					move_left = false
				else:
					get_node("legs").set_scale(Vector2(-1,1))
					move_left= true
			motion = normal.slide( motion )
			if motion != Vector2(0,0):
				move(motion)
			velocity = normal.slide( velocity )
			
		if (new_siding_left!=siding_left):
			if (new_siding_left):
				get_node("main").set_scale(Vector2(-1,1))
			else:
				get_node("main").set_scale(Vector2(1,1))
			siding_left=new_siding_left
	if (new_anim !=anim):
		anim=new_anim
		anim_node.play(anim)
		
	var can_shoot = ray.is_colliding() and ray.get_collider() == Globals.get("PLAYER")
	
	if can_shoot and get_node("bullet_timer").get_time_left() == 0:
		call_deferred("_shoot")
		
	if check_melee_area() !=0 :
		Globals.get("PLAYER").apply_damage(0,0,5,0)

func check_melee_area():
	return get_node("main/gun/melle_area").get_overlapping_bodies().size()
		
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()
		
func die():
	queue_free()
	audio.play_sound("explosion_10")
	global.show_effect(get_pos(), 2, 1)
	global.show_effect(get_pos(), 1, 1)
		
func _shoot():
	get_node("bullet_timer").start()
	var b = bullet.instance()
	var b_pos = get_node("main/gun/bullet_shoot_1").get_global_pos()
	var rot = gun.get_rot()+PI/2
	b.set_pos(b_pos)
	if siding_left:
		b.set_rot(-rot)
	else:
		b.set_rot(rot-PI)
	get_parent().add_child(b)
	gun_partice.set_emitting(true)
	gun.rotate(0.5)
	audio.play_sound("shoot_3")
		
func _ready():
	print(particle)
	pass
	
func _on_notifier_enter_screen():
	get_node("anim").play(anim)
	set_fixed_process(true)
