extends Node2D

@onready var anim: AnimationPlayer = $anim
@onready var skeleton: Skeleton2D = $skeleton

func _ready() -> void:
    var t := create_tween()
    t.tween_property(skeleton, "position", Vector2(skeleton.position.x - 30.0, skeleton.position.y), 3.0) \
        .set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_QUAD)
    t.chain().tween_property(skeleton, "position", Vector2(skeleton.position.x + 30.0, skeleton.position.y), 3.0) \
        .set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_QUAD)
    t.set_loops()

    anim.play("idle")

func _input(event: InputEvent) -> void:
    if event.is_action("talk"):
        _talk()

func _talk() -> void:
    var p := randf()
    if p < .2:
        anim.play("talk3")
    elif p < .5:
        anim.play("talk1")
    else:
        anim.play("talk2")

func _on_animation_finished(_anim_name: StringName) -> void:
    anim.play("idle")

func _on_area_input_event(_viewport: Node, event: InputEvent, _shape_idx: int) -> void:
    if event is InputEventMouseButton and event.pressed and anim.current_animation == "idle":
        _talk()
