extends CharacterBody2D

enum States {
	IDLE,
	PECKING
}

@export var minimum_pecking_schedule_gap := 8.0
@export var maximum_pecking_schedule_gap := 25.0
@export var should_log_pecking_schedule := false

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

var current_state := States.IDLE
var pecking_sched := randf_range(8.0, 25.0)
var pecking_timer := 0.0

func _ready() -> void:
	_print_sched()


func _process(delta: float) -> void:
	if current_state == States.IDLE:
		pecking_timer += delta
		if pecking_timer >= pecking_sched:
			current_state = States.PECKING
			animated_sprite_2d.play("pecking")
			await animated_sprite_2d.animation_finished
			animated_sprite_2d.play("idle")
			pecking_sched = randf_range(minimum_pecking_schedule_gap, maximum_pecking_schedule_gap)
			pecking_timer = 0
			current_state = States.IDLE
			_print_sched()


func _print_sched() -> void:
	if should_log_pecking_schedule:
		print("%s Pecking Schedule: %f" % [name, pecking_sched])
