class_name CaterpillarHead
extends Node2D

var is_paused: bool = false

@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var audio_stream_player: CaterpillarAudioStreamPlayer = $CaterpillarAudioStreamPlayer
@onready var spit_vfx: SpitVFX = $SpitFX


func _ready() -> void:
	idle()


func idle() -> void:
	if randf() < 0.8:
		animated_sprite.play("idle")
	else:
		animated_sprite.play("idle_blink")


func eat() -> void:
	animated_sprite.play("eat")
	audio_stream_player.eat()
	await animated_sprite.animation_finished


func spit() -> void:
	animated_sprite.play("spit")
	spit_vfx.play()
	await animated_sprite.animation_finished


func _on_animation_finished() -> void:
	if not is_paused:
		idle()
	else:
		animated_sprite.set_frame_and_progress(0, 0)


func pause_animation() -> void:
	is_paused = true
	animated_sprite.pause()


func resume_animation() -> void:
	is_paused = false
	idle()
