Files
experiment-adventure-ai/cutscene/actions/AnimationAction.gd
2025-08-01 08:34:51 -07:00

56 lines
1.7 KiB
GDScript
Executable File

class_name AnimationAction
extends "res://cutscene/Action.gd"
# Properties
var character: Node2D # The character to animate
var animation_name: String # Name of the animation to play
var loop: bool = false # Whether to loop the animation
func _init(character_node: Node2D, anim_name: String, should_loop: bool = false) -> void:
character = character_node
animation_name = anim_name
loop = should_loop
name = "AnimationAction"
func start() -> void:
if character == null:
self._set_failed("Character is null")
return
# Check if character has an AnimationPlayer
var anim_player = _get_animation_player()
if anim_player == null:
self._set_failed("Character has no AnimationPlayer")
return
# Connect to animation finished signal for non-looping animations
if not loop:
if not anim_player.is_connected("animation_finished", _on_animation_finished):
anim_player.connect("animation_finished", _on_animation_finished.bind(anim_player))
# Play the animation
anim_player.play(animation_name)
self._set_running()
# For looping animations, we complete immediately
# (they would be stopped by another action)
if loop:
self._set_completed()
func _get_animation_player() -> AnimationPlayer:
# Try to find AnimationPlayer as a child
for child in character.get_children():
if child is AnimationPlayer:
return child
# Try to find AnimationPlayer in the scene tree
return character.get_node_or_null("AnimationPlayer") as AnimationPlayer
func _on_animation_finished(anim_name: String, anim_player: AnimationPlayer) -> void:
# Make sure this is for our animation
if anim_name == animation_name and not loop:
self._set_completed()
func is_completed() -> bool:
return state == State.COMPLETED