extends AnimatedSprite2D

@onready var hitbox_2: Area2D = $Hitbox2
@onready var hitbox: Area2D = $Hitbox
@onready var wait_to_die: Timer = $WaitToDie
@onready var hitting: Timer = $Hitting
@onready var wait_to_hit: Timer = $WaitToHit
@onready var die_audio: AudioStreamPlayer = $DieAudio
@onready var hitsound: AudioStreamPlayer = $Hit

var is_dying = false
	
func start_dancing():
	play("dance")

func hit():
	if is_dying:
		return
	
	hitbox.monitorable = true
	hitbox.monitoring = true
	play("hit")
	hitsound.play()
	hitting.start()
	
func stop_hitting():
	if is_dying:
		return
	
	hitbox.monitorable = false
	hitbox.monitoring = false 
	
	play("idle")
	wait_to_hit.start()

func _on_damageable_die() -> void:
	hitbox.set_deferred("monitorable", false)
	hitbox.set_deferred("monitoring", false)
	hitbox_2.set_deferred("monitorable", false)
	hitbox_2.set_deferred("monitoring", false)
	is_dying = true
	play("die")
	die_audio.play()
	wait_to_die.start()

func _on_wait_to_die_timeout() -> void:
	queue_free()
