extends Area2D

export (int) var SPEED
export (int) var HEALTH
export (int) var TARGETX
export (int) var TARGETY
export (int) var LEVEL
export (String) var shieldsready
#export (bool) var shieldsAvailable

signal playerhit
signal shieldson
signal shieldsoff


var shieldsAvailable = true
var shielding = false
var velocity = Vector2()
var screensize
var accel = 2
var dirX = accel
var dirY = accel

var beinghit = false
var timerShield
var timerRecharge
var currentlevel 

func _ready():
	# Called every time the node is added to the scene.

	currentlevel = 1
	screensize = get_viewport_rect().size
	beinghit = false
	$Shields.get_node("CollisionShape2D").disabled = true 
	$Shields.hide()
	shielding = false
	shieldsready = "READY"
	#$Shields.get_node("CollisionShape2D").disabled = true
	set_process_input(true)
	timerShield = get_node("ShieldsTimer")
	timerShield.set_wait_time(5)
	timerShield.connect("timeout", self, "_on_ShieldsTimer_timeout")
	timerRecharge = get_node("ShieldRechargeTimer")
	timerRecharge.set_wait_time(5)
	timerRecharge.connect("timeout", self, "_on_ShieldRechargeTimer_timeout")
	
func _process(delta):

	if HEALTH > 0:
		self.show()
		velocity = Vector2()
		if Input.is_action_pressed("ui_right"):
		#	velocity.x += 1
			dirX += accel
		if Input.is_action_pressed("ui_left"):
		#	velocity.x -= 1
			dirX -= accel
		if Input.is_action_pressed("ui_down"):
		#	velocity.y += 1
			dirY += accel
		if Input.is_action_pressed("ui_up"):
		#	velocity.y -= 1
			dirY -= accel
		
	

		velocity.x += dirX
		velocity.y += dirY
			
		position += velocity * delta
		if position.x > screensize.x || position.x < 0 :
			dirX = dirX * -1
		if position.y > screensize.y  || position.y < 20 :
			dirY = dirY * -1

		var aoa = getAOA(position.x,position.y, TARGETX, TARGETY)
		$Sprite.rotation = aoa + PI/2
		
		position.x = clamp(position.x, 0 ,screensize.x)
		position.y = clamp(position.y, 0 ,screensize.y)
		if beinghit:
			emit_signal("playerhit")
	else:
		self.hide()
		shields_off()

func _input(event):
   # Mouse in viewport coordinates
	if event is InputEventMouseButton:
		if event.button_index == 2 and HEALTH > 0 and shieldsAvailable:
			shields_on()

func getAOA(x,y,tx,ty):
	#baseAngle = 0
	#hypopp =  abs(x-tx)
	var a = Vector2(x,y)
	var b = Vector2(tx,ty)
	return a.angle_to_point(b)



func shields_on():
	print ("Shields ON!!")
	$Shields.get_node("CollisionShape2D").disabled = false
	$Shields.show()
	timerShield.start()
	shielding = true 
	emit_signal("shieldson")
	
	
	
func shields_off():
	$Shields.get_node("CollisionShape2D").disabled = true 
	$Shields.hide()
	shielding = false 
	shieldsAvailable = false
	shieldsready = "N/A"
	timerRecharge.start()
	emit_signal("shieldsoff")
	

func _on_Player_body_entered(body):
	#print ("Enemyname:" + body.get_name())
	if !shielding:
		if body.get_name().matchn("*enemy*"):
	#	print ("player hit")
			beinghit = true
			emit_signal("playerhit");

func _on_Player_body_exited(body):
	if body.get_name().matchn("*enemy*"):
		beinghit = false
	
func _on_ShieldsTimer_timeout():
	shields_off()
	
func _on_ShieldRechargeTimer_timeout():
	shieldsAvailable = true
	shieldsready = "READY"
	#timerRecharge.stop()
	
	