extends Area2D

onready var bullet_scene = load("res://scenes/enemie_bullet.tscn")

var destroyed = false 

var damage = 10

var health = 30
var health_0 = 12
var health_1 = 12
var health_2 = 12
var health_3 = 12

var part_0_destroyed = false
var part_1_destroyed = false
var part_2_destroyed = false
var part_3_destroyed = false

var parts = 4

var y_speed = 0
var x_speed = 4

var x = 0
var y = 0
var new_x = 0
var new_y = 0
var dir_x = 1
var dir_y = 1

var mod = false

func _process(delta):
	new_y += y_speed * delta
	if ceil(y) != ceil(new_y):
		y = new_y
		position.y += 1
		
	new_x += x_speed * delta
	if ceil(x) != ceil(new_x):
		x = new_x
		position.x += 1 * dir_x
		
	if position.x < 5 or position.x > 26:
		dir_x *= -1
		position.x += 1 * dir_x
		
#	if position.y < 5 or position.y > 10:
#		dir_y *= -1
#		position.y += 1 * dir_y
		
func shoot():
	var r
	if mod:
		r = range (3, -1, -1)
	else:
		r = range (0, 4, 1)
	mod = !mod
	for i in r:
		if self.get("health_" + str(i)) > 0:
			var bullet = bullet_scene.instance()
			bullet.global_position = $bullet_poses.get_child(i).global_position
			get_parent().add_child(bullet)
			$rapid_time.start()
			yield($rapid_time, "timeout")
			$shot.play()
	$bullet_time.start()
	
func _on_area_entered(area):
	if destroyed:
		return
	var t = area.type
	if t == "bullet" and parts <= 0:
		area.queue_free()
		if apply_damage(area.damage):
			destroy()
	elif t == "rocket" and parts <= 0:
		area.queue_free()
		var _exp = load("res://scenes/explossion.tscn").instance()
		_exp.global_position = global_position
		get_parent().add_child(_exp)
		get_parent().flash()
		if apply_damage(area.damage):
			yield(get_tree().create_timer(0.15), "timeout")
			destroy()
	elif t == "body":
		area.destroy()
		
func apply_damage(d):
	health -= d
	if health <= 0:
		return true
	else:
		$anim.play("damage")
		$hit.play()
		return false
	
func destroy():
	set_deferred("monitoring", false)
	$areas.visible = false
	$anim.play("destroy")
	var _exp = load("res://scenes/explossion.tscn").instance()
	_exp.global_position = Vector2(global_position.x - 6, global_position.y)
	get_parent().add_child(_exp)
	get_parent().flash()
	yield(get_tree().create_timer(0.15), "timeout")
	_exp = load("res://scenes/explossion.tscn").instance()
	_exp.global_position = Vector2(global_position.x - 2, global_position.y)
	get_parent().add_child(_exp)
	yield(get_tree().create_timer(0.15), "timeout")
	_exp = load("res://scenes/explossion.tscn").instance()
	_exp.global_position = Vector2(global_position.x + 2, global_position.y)
	get_parent().add_child(_exp)
	yield(get_tree().create_timer(0.15), "timeout")
	_exp = load("res://scenes/explossion.tscn").instance()
	_exp.global_position = Vector2(global_position.x + 6, global_position.y)
	get_parent().add_child(_exp)
	get_parent().n -= 1

	
func destroy_part(p):
	var part = get_node("areas/area_part_" + str(p))
	var _exp = load("res://scenes/explossion.tscn").instance()
	_exp.global_position = part.global_position
	get_parent().add_child(_exp)
	get_parent().flash()
	part.set_deferred("monitoring", false)
	part.set_deferred("monitorable", false)
	part.get_node("pol").visible = false
	part.get_node("anim").play("destroy")
	parts -= 1
	$bullet_time.wait_time -= 0.1
	x_speed += 1
	
func apply_part_damage(i, d):
	set("health_" + str(i), get("health_" + str(i)) - d)
	if get("health_" + str(i)) <= 0:
		return true
	else:
		get_node("areas/area_part_" + str(i) + "/anim").play("part_damage")
		get_node("././hit").play()
		return false
	
func _on_area_entered_0(area):
	var t = area.type
	if t == "bullet":
		area.queue_free()
		if apply_part_damage(0, area.damage):
			$areas/area_part_0.set_deferred("monitoring", false)
			destroy_part(0)
	elif t == "rocket":
		area.queue_free()
		var _exp = load("res://scenes/explossion.tscn").instance()
		_exp.global_position = $areas/area_part_0.global_position
		get_parent().add_child(_exp)
		get_parent().flash()
		if apply_part_damage(0, area.damage):
			$areas/area_part_0.set_deferred("monitoring", false)
			yield(get_tree().create_timer(0.15), "timeout")
			destroy_part(0)
	elif t == "body":
		area.destroy()
		
func _on_area_entered_1(area):
	var t = area.type
	if t == "bullet":
		area.queue_free()
		if apply_part_damage(1, area.damage):
			$areas/area_part_1.set_deferred("monitoring", false)
			destroy_part(1)
	elif t == "rocket":
		area.queue_free()
		var _exp = load("res://scenes/explossion.tscn").instance()
		_exp.global_position = get_node("areas/area_part_1").global_position
		get_parent().add_child(_exp)
		get_parent().flash()
		if apply_part_damage(1, area.damage):
			$areas/area_part_1.set_deferred("monitoring", false)
			yield(get_tree().create_timer(0.15), "timeout")
			destroy_part(1)
	elif t == "body":
		area.destroy()

func _on_area_entered_2(area):
	var t = area.type
	if t == "bullet":
		area.queue_free()
		if apply_part_damage(2, area.damage):
			$areas/area_part_2.set_deferred("monitoring", false)
			destroy_part(2)
	elif t == "rocket":
		area.queue_free()
		var _exp = load("res://scenes/explossion.tscn").instance()
		_exp.global_position = $areas/area_part_2.global_position
		get_parent().add_child(_exp)
		get_parent().flash()
		if apply_part_damage(2, area.damage):
			$areas/area_part_2.set_deferred("monitoring", false)
			yield(get_tree().create_timer(0.15), "timeout")
			destroy_part(2)
	elif t == "body":
		area.destroy()
	
func _on_area_entered_3(area):
	var t = area.type
	if t == "bullet":
		area.queue_free()
		if apply_part_damage(3, area.damage):
			$areas/area_part_3.set_deferred("monitoring", false)
			destroy_part(3)	
	elif t == "rocket":
		area.queue_free()
		var _exp = load("res://scenes/explossion.tscn").instance()
		_exp.global_position = $areas/area_part_3.global_position
		get_parent().add_child(_exp)
		get_parent().flash()
		if apply_part_damage(3, area.damage):
			$areas/area_part_3.set_deferred("monitoring", false)
			yield(get_tree().create_timer(0.15), "timeout")
			destroy_part(3)
	elif t == "body":
		area.destroy()
			
func activate():
	var i = 0
	for a in $areas.get_children():
		a.connect("area_entered", self, "_on_area_entered_" + str(i))
		a.monitoring = true
		i += 1
	$bullet_time.connect("timeout", self, "shoot")
	$bullet_time.start()
	set_process(true)

func _ready():
	set_process(false)
	self.connect("area_entered", self, "_on_area_entered")
	$anim.play("fly_in")
	yield($anim,"animation_finished") 
	activate()
