extends "res://scripts/enemy.gd"

var dir = Vector2()
var pos_x = 0
var offset = 0

func _process(delta):
	position.y += dir.y * speed * delta
	position.x = pos_x + offset 
	
func death():
	get_parent()._on_enemy_death(0)
	var e = get_parent().explossion_scene.instance()
	e.position = position
	get_tree().root.add_child(e)
	global.game.get_node("bg/anim").play("explossion")
	global.change_score(score_bonus)
	queue_free()

func _on_enemy_1_area_entered(area):
	if area.name == "home":
		atack()
		return
	elif area.name == "enemy_base":
		back()
		return
	elif area.name == "player":
		if area.shield == false:
			area.death()
		else:
			death()
		return
	else:
		death()
	
func back():
	set_process(false)
	var timer = Timer.new()
	add_child(timer)
	timer.wait_time = 2
	timer.start()
	yield(timer,"timeout")
	dir = Vector2(0, 1)
	speed = speed/4
	$sprite.flip_v = false
	set_process(true)

func atack():
	var warning = load("res://scenes/warning.tscn").instance()
	warning.position = Vector2(position.x, 288)
	get_parent().add_child(warning)
	set_process(false)
	var timer = Timer.new()
	add_child(timer)
	timer.wait_time = 1
	timer.start()
	yield(timer,"timeout")
	dir = Vector2(0, -1)
	speed = speed * 4 
	$sprite.flip_v = true
	set_process(true)
	
func _ready():
	dir = Vector2(0, 1)
