extends Area2D

var type = "body"

var health = 16

var support_left = false
var support_right = false

var rocket_left = false 
var rocket_right = false

onready var bullet_scene = load("res://scenes/bullet.tscn")
onready var rocket_scene = load("res://scenes/rocket.tscn")

func _process(delta):
	var rl = Input.is_mouse_button_pressed(1)
	var rr = Input.is_mouse_button_pressed(2)
	var mpos = get_global_mouse_position()
	position = mpos.snapped(Vector2(1,1))
	position.x = clamp(position.x, 0, 31)
	
	if rl:
		shoot_special(0)
	if rr:
		shoot_special(1)
	
func shoot_special(r):
	if r == 0 and rocket_left:
		rocket_left = false
		var rocket = rocket_scene.instance()
		rocket.global_position = $left_rocket_pos.global_position
		$left_rocket_pos.visible = false
		get_parent().add_child(rocket)
	elif r == 1 and rocket_right:
		rocket_right = false
		var rocket = rocket_scene.instance()
		rocket.global_position = $right_rocket_pos.global_position
		$right_rocket_pos.visible = false
		get_parent().add_child(rocket)
	
func shoot():
	var bullet = bullet_scene.instance()
	bullet.global_position = $bullet_pos.global_position
	get_parent().add_child(bullet)
	if support_left:
		var bullet_left = bullet_scene.instance()
		bullet_left.global_position = $support_left/bullet_pos.global_position
		get_parent().add_child(bullet_left)
	if support_right:
		var bullet_right = bullet_scene.instance()
		bullet_right.global_position = $support_right/bullet_pos.global_position
		get_parent().add_child(bullet_right)
	$bullet_time.start()

func _on_area_enter(area):
	var t = area.type
	if t == "bonus":
		var bn = area.bonus_name
		if bn == "support":
			support_bonus_collect(area.global_position.x)
		elif bn == "rockets":
			rockets_bonus_collect()
		elif bn == "hp":
			hp_bonus_collect()
		area.collect()
	elif t == "bullet":
		area.queue_free()
		apply_bullet_damage(area.damage)
		
func apply_bullet_damage(d):
	health -= d
	if health < 0:
		get_parent().get_node("gul/healthbar").value = 0
		destroy()
	else:
		get_parent().get_node("gul/healthbar").value = health
		$anim.play("damage")
	
func apply_damage(d):
	health -= d
	if health < 0:
		get_parent().get_node("gul/healthbar").value = 0
		return true
	else:
		get_parent().get_node("gul/healthbar").value = health
		$hit.play()
		return false
		
func destroy():
	var _exp = load("res://scenes/explossion.tscn").instance()
	_exp.global_position = global_position
	get_parent().add_child(_exp)
	get_parent().restart()
	queue_free()
	
func support_bonus_collect(pos_x):
	if pos_x < global_position.x:
		if !support_left:
			$support_left.activate()
			support_left = true
		elif !support_right:
			$support_right.activate()
			support_right = true
		else:
			pass
	else: 
		if !support_right:
			$support_right.activate()
			support_right = true
		elif !support_left:
			$support_left.activate()
			support_left = true
		else:
			pass
			
func rockets_bonus_collect():
	$left_rocket_pos.visible = true
	rocket_left = true
	$right_rocket_pos.visible = true
	rocket_right = true
	
func hp_bonus_collect():
	health = 16
	get_parent().get_node("gul/healthbar").value = health
			
func _ready():
	self.connect("area_entered", self, "_on_area_enter")
	$bullet_time.connect("timeout", self, "shoot")
	$bullet_time.start()
