extends "res://Flying.gd"

var entity
var canShoot = true
var fireRate = 1.6

export(PackedScene) var bulletScn

func _ready():
	health = 1
	moveSpeed = 0.4
	$Timer.wait_time = fireRate

func _process(delta):
	if !fall:
		entity = $DetectionArea.getClosestPlayer()
		if entity != null:
			nextPosition = entity.position
		shouldFlip()

func movement(distance):
	var length = distance.length()
	if length < 140:
		shoot(entity)
		return Vector2(0,0)
	else:
		return distance * moveSpeed

func shoot(entity):
	if canShoot:
		if(entity != null):
			var bullet = bulletScn.instance()
			get_parent().get_parent().add_child(bullet)
			bullet.position = position
			bullet.direction = (entity.position - position).normalized()
			entity = null
			canShoot = false

func _on_Timer_timeout():
	canShoot = true

func recieveDamage(damage):
	health -= damage
	destroyBalloon()

func _on_Enemy_tree_exiting():
	GameManager.enemiesDestroyed += 1
	GameManager.score += 100
