extends Node2D

const no_key_sheet = preload("res://Rooms/Outdoors2/bird_sheet.png")

onready var global: Node = get_node("/root/Global")

onready var sprite: Sprite = $Sprite
onready var animation_player: AnimationPlayer = $AnimationPlayer
onready var timer: Timer = $Timer

var sing: bool = false

func _ready():
	if global.game_vars.has("note_used"):
		sing = true
		timer.start()
	if global.game_vars.has("key_grabbed"):
		sprite.texture = no_key_sheet

func _on_Area_input_event(_viewport, event, _shape_idx) -> void:
	if !event is InputEventMouseButton:
		return
	if !event.pressed:
		return
	if event.button_index == BUTTON_LEFT:
		if global.game_vars.has("note_used") \
		&& !global.game_vars.has("key_grabbed"):
			sprite.texture = no_key_sheet
			var inv = global.inventory
			inv.add_item(inv.Items.key)
			global.game_vars["key_grabbed"] = true
			global.play_sound("key_grab")
			return
		fly_away()

func fly_away():
	timer.stop()
	global.play_sound("bird_fly_away")
	animation_player.play("Prepare")
	yield(animation_player, "animation_finished")
	animation_player.play("FlyAway")

func _on_Timer_timeout():
	if sing:
		timer.wait_time = 5
		#animation_player.
		global.play_sound("bird_sing")
		animation_player.play("Sing")
		yield(animation_player, "animation_finished")
		animation_player.play("Idle")
