
extends KinematicBody2D

const SPEED = 25.0
const GRAVITY = 600
const ROTATION_SPEED = 2

export var health = 0
export var ricocheting = true
export var armor_class = 10
export var walk_speed = 0.0

var velocity = Vector2()
var anim = "idle"
var move_left = false
var move_right = false
var siding_left = false
var is_grounded = false
var shoot = true
var bullet_shell = preload("res://scenes/items/shotgun_bullet_shell.scn")
var bullet = preload("res://scenes/shot.scn")
var target

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

func _fixed_process(delta):
	
	var new_anim = anim
	var new_siding_left = siding_left
	
	var ppos = target.get_pos()
	var pos = get_pos()	
	
	var ang = gun.get_angle_to(ppos)
	var s = sign(ang)
	ang = abs(ang)
	var rot = min(ang, ROTATION_SPEED*delta)*s
	gun.rotate(rot)
	
	if !shoot: 
		var rpos = pos - ppos
		if rpos.x > 0:
			move_left = true
			move_right = false
			new_siding_left = true
		else:
			move_right = true
			move_left= false
			new_siding_left = false
	else:
		move_right = false
		move_left= false
		
	
	velocity.y += delta * GRAVITY
	
	if move_left:
		new_anim = "walk"
		velocity.x = -walk_speed
	elif move_right:
		new_anim = "walk"
		velocity.x = walk_speed
	else:
		new_anim = "idle"
		velocity.x = 0
		
		
	shoot = ray.is_colliding() and ray.get_collider() == target
		
	if shoot and get_node("bullet_timer").get_time_left() == 0:
		gun_partice.set_emitting(true)
		audio.play_sound("shotgun_shoot")
		get_node("bullet_timer").start()
		var bs = bullet_shell.instance()
		var b = bullet.instance()
		var bs_pos = get_node("main/gun/extractor").get_global_pos()
		var b_pos
		b_pos = get_node("main/gun/bullet_shoot").get_global_pos()
		var rot = get_node("main/gun/").get_rot()+PI/2
		bs.set_pos(bs_pos)
		b.set_pos(b_pos)
		if siding_left:
			b.set_rot(-rot)
			bs.set_linear_velocity(Vector2(-40,rand_range(100,140)).rotated(-rot))
			bs.set_angular_velocity(rand_range(-2,2))
		elif !siding_left:
			bs.set_rot(rot)
			b.set_rot(rot-PI)
			bs.set_linear_velocity(Vector2(40,rand_range(100,140)).rotated(rot))
			bs.set_angular_velocity(rand_range(-2,2))
		get_parent().add_child(bs)
		get_parent().add_child(b)
		PS2D.body_add_collision_exception(bs.get_rid(),get_rid())
		ray.add_exception_rid(bs.get_rid())
		gun.rotate(0.5)

	var motion = velocity * delta
	if motion != Vector2(0,0):
		motion = move( motion )
		
	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))
		else:
			get_node("main").set_scale(Vector2(1,1))
		siding_left=new_siding_left
		
	if (new_anim !=anim):
		anim=new_anim
		get_node("anim").play(anim)
		
func apply_damage(var touch_pos, var touch_rot, var damage, var delay):
	move_to(get_pos() + Vector2(8, 0).rotated(touch_rot))
	if damage>0:
		particle.set_param(0, rad2deg(touch_rot + PI/2))
		particle.set_emissor_offset(Vector2(0, touch_pos.y))
		particle.set_emitting(true)
		audio.play_sound("hurt")
		health -= damage
	if health <= 0:
		die()
		
func die():
	audio.play_sound("explosion_1")
	global.show_effect(get_pos(), 3, 1)
	queue_free()

func _ready():
	pass

func _on_notifer_enter_screen():
	target = Globals.get("PLAYER")
	get_node("anim").play(anim)
	set_fixed_process(true)