From 591c37dd60161f75813a5e12efc7d38c0093b67e Mon Sep 17 00:00:00 2001 From: Dustin Date: Sun, 23 Mar 2025 19:28:18 -0700 Subject: [PATCH] Start of some working animation modifiers applied. --- lib/classes/animated_sprite_state_receiver.gd | 66 ++++++++----------- lib/classes/state_machine.gd | 1 + .../PlayerE-AnimatedSprite_StateReceiver.gd | 10 +-- 3 files changed, 32 insertions(+), 45 deletions(-) diff --git a/lib/classes/animated_sprite_state_receiver.gd b/lib/classes/animated_sprite_state_receiver.gd index ac0ae6f..c3d562d 100644 --- a/lib/classes/animated_sprite_state_receiver.gd +++ b/lib/classes/animated_sprite_state_receiver.gd @@ -1,26 +1,15 @@ class_name AnimatedSprite_StateReceiver extends AnimatedSprite -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" - export var callable_state_machine :NodePath onready var current_state = StateAnimatedActor.new() -#var state_machine_modifiers: Array var modifier: StateModifierAnimatedActor -#var animation_finished: bool = false var request_state_change: FuncRef var add_state_modifier: FuncRef -# this just wouldn't work well. Maybe I can try a resource -# export(Array, NodePath) var transition_state_nodes - -#TODO: I think these two variables accidentally serve the same purpose -# I think one was intended to be a string of the current animation name. var animation_index: int = 0 var current_animation_sequence: int = 0 var animation_suffix: String = ''; @@ -32,7 +21,6 @@ var previous_state_frame_number : int var previous_state_name: String var previous_speed_multiplier: float - # Called when the node enters the scene tree for the first time. func _ready(): request_state_change = funcref(get_node(callable_state_machine), 'change_to_known_state') @@ -50,15 +38,6 @@ func _ready(): # Sprites should only have a process function # but if I have this will the sprite still animate? func _process(delta): -# Shouldn't need this anymore! -# if current_state.state_timeout != null: -# if current_state.state_timeout.time_left == 0: -# print("You're out of time! I saw it!") -# request_state_change.call_func('idle') -# #play() -# else: -# print("what, no current_state?") - # Not sure what should be called first here. if has_method('_state_process_' + current_state.name): call('_state_process_' + current_state.name) @@ -76,22 +55,12 @@ func _on_animation_finished(): current_state.animation_finished = true # TODO: Allow a way to stop animation even if in a looped sequence -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass # If called, this will attempt to make the most appropriate animation change # would likely be triggered from an animation sequence finishing or a # modifier being applied/removed outside of enter/exit transitions. func change_animation(enter_frame := 0, index := 0, suffix := ''): ## TODO: # Need to find a way to attempt to keep and transfer the Index or frame -# -# if clear_modifier: -# print("Goodbye Modifiers!") -# modifier = null -# if modifier: -# if modifier.state_timeout and modifier.timeout_seconds == 0: -# modifier = null if modifier: #print("hey you got one! -> ", modifier.animation_name) @@ -109,6 +78,23 @@ func change_animation(enter_frame := 0, index := 0, suffix := ''): animation_suffix = suffix animation_index = index current_animation_sequence = animation_index + + if modifier: + #TODO: Apply the animation starting frame + #modifier.animation_starting_frame + match modifier.modifier_type: + modifier.TYPE.ANIMATION_SUFFIX: + # Check that this animation actually exists + if frames.has_animation(mod_animation_sequence[animation_index] + modifier.animation_suffix): + animation_suffix = modifier.animation_suffix + else: + push_warning("Modifier attempting to apply suffix that doesn't exist" + + (mod_animation_sequence[animation_index] + modifier.animation_suffix)) + modifier.TYPE.REPLACE_ANIMATION: + if frames.has_animation(modifier.animation_name): + mod_animation_sequence.clear() + mod_animation_sequence.push_back(modifier.animation_name) + enter_frame = modifier.animation_starting_frame #var enter_animation = '' #var enter_frame = 0 # var modifier :StateModifier = current_state.modifier @@ -218,15 +204,15 @@ func change_animation(enter_frame := 0, index := 0, suffix := ''): # else: # frame_signal_emitted = false -func set_previous_animation( old_state: StateAnimatedActor, new_state: StateAnimatedActor ) -> void: - # added this to help prevent nul index crashes. (Usually because of signal based state changes.) - if old_state.mod_animation_sequence: - new_state.previous_animation_name = old_state.mod_animation_sequence[old_state.current_animation_sequence] - new_state.previous_state_frame_number = old_state.animations.frame - new_state.previous_state_name = old_state.name - new_state.previous_speed_multiplier = old_state.speed_multiplier - #animation_name :String, frame_number : int, animation_sequence :Array - return +#func set_previous_animation( old_state: StateAnimatedActor, new_state: StateAnimatedActor ) -> void: +# # added this to help prevent nul index crashes. (Usually because of signal based state changes.) +# if old_state.mod_animation_sequence: +# new_state.previous_animation_name = old_state.mod_animation_sequence[old_state.current_animation_sequence] +# new_state.previous_state_frame_number = old_state.animations.frame +# new_state.previous_state_name = old_state.name +# new_state.previous_speed_multiplier = old_state.speed_multiplier +# #animation_name :String, frame_number : int, animation_sequence :Array +# return # Handle state change from subscribed state machine. #From signal state_changed(old_state_name, new_state) diff --git a/lib/classes/state_machine.gd b/lib/classes/state_machine.gd index 96feb6e..7bbf910 100644 --- a/lib/classes/state_machine.gd +++ b/lib/classes/state_machine.gd @@ -77,6 +77,7 @@ func change_state(new_state: State) -> void: current_state = new_state current_state.enter() +# Seems like I don't have to do this every state change! # if(state_modifiers.size() > 0): # print("Active Modifiers:") # for mods in state_modifiers: diff --git a/src/actors/players/playerE/PlayerE-AnimatedSprite_StateReceiver.gd b/src/actors/players/playerE/PlayerE-AnimatedSprite_StateReceiver.gd index e24862b..04f45d0 100644 --- a/src/actors/players/playerE/PlayerE-AnimatedSprite_StateReceiver.gd +++ b/src/actors/players/playerE/PlayerE-AnimatedSprite_StateReceiver.gd @@ -14,7 +14,7 @@ func _ready(): attack_sword.setup('sword_drawn',StateModifier.TYPE.ANIMATION_SUFFIX,5.0) add_child(attack_sword.timeout) attack_sword.animation_name = 'PoopenStein' - attack_sword.animation_suffix = '-attack_sword' + attack_sword.animation_suffix = '-attack-sword' add_state_modifier.call_func(attack_sword) # I don't need this right now but I proved it can be done! @@ -41,10 +41,10 @@ func _ready(): # frame = 2 # stop() -func _on_state_change_idle(): - change_animation() - if previous_state_name == 'attack_sword': - animation = 'idle-attack-sword' +#func _on_state_change_idle(): +# change_animation() +# if previous_state_name == 'attack_sword': +# animation = 'idle-attack-sword' func _on_state_change_fall(): change_animation()