initial
This commit is contained in:
55
cutscene/actions/AnimationAction.gd
Normal file
55
cutscene/actions/AnimationAction.gd
Normal file
@@ -0,0 +1,55 @@
|
||||
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
|
||||
Reference in New Issue
Block a user