extends Node2D

export var _bullet_time = 2
export var dir = -1

var dead = false

onready var projectile_scene = load("res://scenes/projectile.tscn")

func atack():
	if !dead:
		$bullet_time.start()
		$AnimationPlayer.play("throw")
		yield($bullet_time, "timeout")
		atack()
	
func throw():
	var projectile = projectile_scene.instance()
	projectile.global_position = $sprite/Position2D.global_position
	projectile.velocity = Vector2(1300 * dir, -1000)
	get_parent().call_deferred("add_child", projectile)
	Audio.play_sound("baski_throw", false)
	
func _on_area_body_entered(body):
	if position.y - body.position.y < 20:
		body.apply_damage(1,"damage")
	else:
		body.bound()
		death()
		
func death():
	dead = true
	$bullet_time.stop()
	$damage_area.call_deferred("set_monitoring", false)
	$AnimationPlayer.play("death")
	Audio.play_sound("baski_death", false)
	
func _ready():
	$damage_area.connect("body_entered",self,"_on_area_body_entered")
	$bullet_time.wait_time = _bullet_time
	$sprite.scale.x = dir
	atack()
