extends "res://Flying.gd"
var startPosition
var activeTowers = []
var towerScene
var avaliableTurrets

func _ready():
	GameManager.playAudio("res://Assets/Audio/032_1.WAV")
	health = 3
	startPosition = position
	towerScene = load("res://tower2.tscn")

func _process(delta):
	if Input.is_action_just_pressed("drop") and (GameManager.avaliableTowers > 0):
#		if activeTowers.size() >= 3:
#			activeTowers.remove(activeTowers.size())
#			activeTowers.front().queue_free()
		var towerInstance = towerScene.instance()
		towerInstance.position = position
		get_parent().add_child(towerInstance)
		activeTowers.append(towerInstance)
		GameManager.subbTowers()

func _input(event):
	if !fall:
		if event is InputEventMouseButton:
			nextPosition = get_global_mouse_position()
			shouldFlip()
			distance = (nextPosition - startPosition).normalized()

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

func recieveDamage(damage):
	if $AnimationPlayer2.current_animation != "blink":
		health -= damage
		destroyBalloon()
		GameManager.ui.healthBar(health)
		$AnimationPlayer2.play("blink")

func destroyBalloon():
	if(balloons.size() > 0):
		var toDestroy = balloons.front()
		toDestroy.destroy()
		balloons.remove(0)


func _on_Player_tree_exiting():
	get_node("../Gameover").gameover()
