From aef17c3d811953c7e26b58d13a46393e3680ab99 Mon Sep 17 00:00:00 2001 From: Nitsud Yarg Date: Fri, 28 Jun 2024 14:24:02 -0700 Subject: [PATCH] What am I even doing. --- src/playerD/Player.tscn | 1 - src/playerD/states/attack.gd | 40 +++++++---- src/state_animated_actor.gd | 105 ++++++++++++++++++++++++++-- src/state_machine_animated_actor.gd | 7 +- src/state_modifier.gd | 23 +++++- 5 files changed, 150 insertions(+), 26 deletions(-) diff --git a/src/playerD/Player.tscn b/src/playerD/Player.tscn index 3185306..0ebb669 100644 --- a/src/playerD/Player.tscn +++ b/src/playerD/Player.tscn @@ -601,7 +601,6 @@ jump_force = 250.0 [node name="attack" type="Node" parent="movement_state_machine" index="4"] script = ExtResource( 10 ) -timeout_seconds = 2.0 jump_node = NodePath("../jump") idle_node = NodePath("../idle") fall_node = NodePath("../fall") diff --git a/src/playerD/states/attack.gd b/src/playerD/states/attack.gd index 410cef4..9d66e49 100644 --- a/src/playerD/states/attack.gd +++ b/src/playerD/states/attack.gd @@ -6,10 +6,10 @@ export (NodePath) var fall_node export (NodePath) var move_node export (NodePath) var draw_weapon_node -onready var jump_state: State = get_node(jump_node) -onready var idle_state: State = get_node(idle_node) -onready var fall_state: State = get_node(fall_node) -onready var move_state: State = get_node(move_node) +onready var jump_state: StateAnimatedActor = get_node(jump_node) +onready var idle_state: StateAnimatedActor = get_node(idle_node) +onready var fall_state: StateAnimatedActor = get_node(fall_node) +onready var move_state: StateAnimatedActor = get_node(move_node) #onready var weapon_state_modifier: StateModifier = get_node(draw_weapon_node) @@ -37,7 +37,6 @@ func enter() -> void: move_component.current_movement_state = self.name emit_signal("state_entered") - state_timeout.start() can_fire = true # if modifier_stack_ref.has($"../draw_weapon") == false: # $"../draw_weapon".enter() @@ -58,23 +57,31 @@ func enter() -> void: print("Warning!: Attack state encountered unhandled prev animation: ", previous_animation_name) draw_weapon_modifier = StateModifier.new() - var mod_timer :Timer = draw_weapon_modifier.ready( "idle_shoot" , draw_weapon_modifier.TYPE.ANIMATION_SUFFIX, 2.0) + var mod_timer :Timer = draw_weapon_modifier.ready( "_shoot" , draw_weapon_modifier.TYPE.ANIMATION_SUFFIX, 2.0) add_child(mod_timer) -# connect() - mod_timer.connect("timeout", draw_weapon_modifier, "_on_Timer_timeout") - mod_timer.connect("timeout", self, "_on_Timer_timeout") - draw_weapon_modifier.state_timeout.start() - #mod_timer.connect("timeout",) + + # An example where the modifier is left to alert on a timeout + #mod_timer.connect("timeout", draw_weapon_modifier, "_on_Timer_timeout") + # An example where the current node subscribes + #mod_timer.connect("timeout", self, "_on_Timer_timeout") + + animation_finished = false func _on_Timer_timeout(): - print("Oh Crap! I this worked too!") - animations.stop() + print("Modifier Timeout from attack! Forcing animation recall") + #animations.stop() func process_frame(delta: float) -> State: + if animation_finished: + print("WTF!?!! Finish already!") + draw_weapon_modifier.state_timeout.start() + modifier = draw_weapon_modifier + return idle_state + .process_frame(delta) # if animations.frame == 2: # animations.stop() - if state_timeout.time_left == 0 and animations.animation == "shoot": - return idle_state +# if state_timeout.time_left == 0 and animations.animation == "shoot": +# return idle_state return null @@ -93,6 +100,9 @@ func process_physics(delta: float) -> State: return idle_state if previous_state_name == "idle" and move_component.velocity.x != 0.0: + draw_weapon_modifier.state_timeout.start() + move_state.modifier = draw_weapon_modifier + #draw_weapon_modifier.transfer_owner(move_state) return move_state if move_component.get_movement_direction() != 0.0: diff --git a/src/state_animated_actor.gd b/src/state_animated_actor.gd index 85fe819..db3d9fb 100644 --- a/src/state_animated_actor.gd +++ b/src/state_animated_actor.gd @@ -74,6 +74,65 @@ func _ready(): #modifier = StateModifier.new() +# 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 update_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 + + # By Default, we build an animation sequence off of this state's before checking + # for modifiers + mod_animation_sequence.clear() + mod_animation_sequence.append_array(animation_sequence) + # Reset animation suffix in case there isn't one + animation_suffix = suffix + animation_index = index + current_animation_sequence = animation_index + #var enter_animation = '' + #var enter_frame = 0 + + if modifier != null: +# if modifier.modifier_type == modifier.TYPE.EXIT_ANIMATION: +# #print("OHHH Modifier Applies! ", modifier.animation_name) +# mod_animation_sequence.push_front(modifier.animation_name) +# enter_frame = modifier.starting_frame + match modifier.modifier_type: + modifier.TYPE.EXIT_ANIMATION: + mod_animation_sequence.push_front(modifier.animation_name) + enter_frame = modifier.starting_frame + + modifier.TYPE.ANIMATION_SUFFIX: + if animations.frames.has_animation(mod_animation_sequence[animation_index] + modifier.animation_name): + animation_suffix = modifier.animation_name + else: + print("Warning!: Modifier attempting to apply suffix that doesn't exist ", + mod_animation_sequence[animation_index] + modifier.animation_name) + + modifier.TYPE.REPLACE_ANIMATION: + #animations.play(modifier_stack_ref[i].animation_name) + #animations.frame = modifier_stack_ref[i].starting_frame + mod_animation_sequence.clear() + mod_animation_sequence.push_back(modifier.animation_name) + enter_frame = modifier.starting_frame + + if mod_animation_sequence.size() > 0: + if debug_state: + print("Starting Animation: " , mod_animation_sequence[animation_index] + animation_suffix) + animations.play(mod_animation_sequence[animation_index] + animation_suffix) + #TODO: maybe check current animatio has this frame + animations.frame = enter_frame + else: + print("Error! Resolved to empty animation sequence!?") + return + func enter() -> void: mod_animation_sequence.clear() @@ -93,10 +152,38 @@ func enter() -> void: #modifier_stack_ref = state_modifiers if modifier != null: - if modifier.modifier_type == modifier.TYPE.EXIT_ANIMATION: - #print("OHHH Modifier Applies! ", modifier.animation_name) - mod_animation_sequence.push_front(modifier.animation_name) - enter_frame = modifier.starting_frame +# if modifier.modifier_type == modifier.TYPE.EXIT_ANIMATION: +# #print("OHHH Modifier Applies! ", modifier.animation_name) +# mod_animation_sequence.push_front(modifier.animation_name) +# enter_frame = modifier.starting_frame + match modifier.modifier_type: + modifier.TYPE.EXIT_ANIMATION: + mod_animation_sequence.push_front(modifier.animation_name) + enter_frame = modifier.starting_frame + + modifier.TYPE.ANIMATION_SUFFIX: + if animations.frames.has_animation(mod_animation_sequence[animation_index] + modifier.animation_name): + animation_suffix = modifier.animation_name + else: + print("Warning!: Modifier attempting to apply suffix that doesn't exist ", + mod_animation_sequence[animation_index] + modifier.animation_name) + + modifier.TYPE.REPLACE_ANIMATION: + #animations.play(modifier_stack_ref[i].animation_name) + #animations.frame = modifier_stack_ref[i].starting_frame + mod_animation_sequence.clear() + mod_animation_sequence.push_back(modifier.animation_name) + enter_frame = modifier.starting_frame + + if mod_animation_sequence.size() > 0: + if debug_state: + print("Starting Animation: " , mod_animation_sequence[animation_index] + animation_suffix) + animations.play(mod_animation_sequence[animation_index] + animation_suffix) + #TODO: maybe check current animatio has this frame + animations.frame = enter_frame + else: + print("Error! Resolved to empty animation sequence!?") + return # A new attempt at animation modifiers @@ -181,6 +268,14 @@ func process_input(_event: InputEvent) -> State: return null func process_frame(_delta: float) -> State: + + if modifier != null: + if modifier.state_timeout and modifier.state_timeout.time_left == 0: + print("Expired Modifier, updating animation.") + modifier = null + update_animation() + + #if animation_sequence.size() > 0 and current_animation_sequence != animation_index: if mod_animation_sequence.size() > animation_index and current_animation_sequence != animation_index: #TODO: why was I doing this @@ -192,11 +287,13 @@ func process_frame(_delta: float) -> State: print("An animation sequence: ", mod_animation_sequence[animation_index - 1], " -> ", animations.animation , " (", animation_index, ")") current_animation_sequence = animation_index + # We have no more sequence animations and the current one doesn't loop # we do this to prevent the default idle animation from playing. if animation_index >= mod_animation_sequence.size() and animations.frames.get_animation_loop( animations.animation) == false: animations.stop() + animation_finished = true # Singal based frame call. if emitter_frame_subscriptions.has(animations.frame): diff --git a/src/state_machine_animated_actor.gd b/src/state_machine_animated_actor.gd index b281a0a..3eded61 100644 --- a/src/state_machine_animated_actor.gd +++ b/src/state_machine_animated_actor.gd @@ -86,8 +86,8 @@ func process_frame(delta: float) -> void: func _on_AnimatedSprite_animation_finished(): -# if debug_state_machine: -# print("Stop!!!!!") + if debug_state_machine: + print("Stop!!!!!") ##TODO: # It's hard to pop an exit animation off when it's stacked with another kind of animation. # for i in state_modifiers.size(): @@ -100,7 +100,8 @@ func _on_AnimatedSprite_animation_finished(): # current_state.animation_index += 1 # break - + if current_state is StateAnimatedActor: + current_state.animation_finished = true if current_state.animations.frames.get_animation_loop( current_state.animations.animation) == false: if current_state.animation_sequence.size() > 0: diff --git a/src/state_modifier.gd b/src/state_modifier.gd index d3c52ec..71dc353 100644 --- a/src/state_modifier.gd +++ b/src/state_modifier.gd @@ -34,8 +34,8 @@ var timeout_seconds: float = 0.0 # Attempting to use this as an animation suffix that can linger after # state transition like 'run-fire' to 'idle-fire' 'jump-fire' etc. -var animation_suffix: bool = false -var pre_append_animation: bool = false +#var animation_suffix: bool = false +#var pre_append_animation: bool = false # not sure if I can do the array thing from state change. Maybe? # disabling this sequencing for now. @@ -49,18 +49,24 @@ var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity") var state_timeout: Timer +# Meant to be called on timeout or other counters this modifier may have. +# Should be updated whenever modifier owner transfers +var state_call_function: FuncRef + #func copy_modifier() -> StateModifier: # var new_modifier = StateModifier.new() # new_modifier.ready(animation_name, modifier_type, timeout_seconds) # # return StateModifier.new() -func ready(anim_name:String = '', type = TYPE.NONE, timeout : float = 0) -> Timer: +func ready(anim_name:String = '', type = TYPE.NONE, timeout : float = 0, parent:Node = null, function_name : String = "") -> Timer: # If somebody forgot to specify the type if anim_name != '' and type == TYPE.NONE: modifier_type = TYPE.REPLACE_ANIMATION animation_name = anim_name modifier_type = type +# if parent != null and function_name != "": +# state_call_function = funcref(parent, function_name) if timeout > 0: state_timeout = Timer.new() state_timeout.wait_time = timeout @@ -70,8 +76,19 @@ func ready(anim_name:String = '', type = TYPE.NONE, timeout : float = 0) -> Time #add_child(state_timeout) return null +func transfer_owner(parent:Node): + print("Modifier owner will now be: ", parent.name) + state_call_function = funcref(parent, 'update_animation') + func _on_Timer_timeout(): print("Oh Crap! I can't beleive it worked") + if state_call_function: + if state_call_function.is_valid(): + state_call_function.call_func() + else: + print("Warning! Modifier timed out with no callback to alert.") + else: + print("Warning! no function applies.") func enter() -> void: if debug_state: