class_name Grave
extends PuzzleElement

@export var pair: Grave
@export var jingle: AudioStream

@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var audio_player: AudioStreamPlayer2D = $AudioStreamPlayer2D
@onready var manager: GravePuzzle = $"../"

var is_singing = false
var is_finished = false

func _ready():
	pass # Replace with function body.

func _process(delta):
	pass
	
func _physics_process(delta):
	pass

func interact(player: Player):
	manager.activate_grave(self)

func sing():
	if is_finished:
		return
		
	animation_player.play(&"sing")
	if !audio_player.is_playing():
		audio_player.stream = jingle
		audio_player.play()

func complete():
	if is_finished:
		return
		
	animation_player.play(&"complete")
	if !audio_player.is_playing():
		audio_player.stream = jingle
		audio_player.play()
	is_finished = true

func _on_animation_player_animation_finished(anim_name):
	if anim_name == &"sing":
		animation_player.play(&"RESET")
		is_singing = false
