From 89208475fe21d9ce25772246aff7c888dbe18b45 Mon Sep 17 00:00:00 2001 From: Nitsud Yarg Date: Sun, 26 May 2024 22:56:30 -0700 Subject: [PATCH] Add fire/attack state modifier for idle and run animations. --- src/playerC/Player.tscn | 26 +++++++++++++++++++------- src/playerC/states/fire.gd | 17 ++++++++++++++++- src/playerC/states/idle.gd | 1 + src/state.gd | 35 +++++++++++++++++++++++++++++------ src/state_machine.gd | 5 +++++ src/state_modifier.gd | 22 ++++++++++++++++++---- 6 files changed, 88 insertions(+), 18 deletions(-) diff --git a/src/playerC/Player.tscn b/src/playerC/Player.tscn index d58131c..6f0537e 100644 --- a/src/playerC/Player.tscn +++ b/src/playerC/Player.tscn @@ -323,8 +323,8 @@ animations = [ { "name": "climb_fire", "speed": 10.0 }, { -"frames": [ SubResource( 86 ), SubResource( 87 ), SubResource( 88 ), SubResource( 86 ) ], -"loop": true, +"frames": [ SubResource( 86 ), SubResource( 87 ), SubResource( 88 ), SubResource( 86 ), SubResource( 86 ) ], +"loop": false, "name": "fire", "speed": 10.0 }, { @@ -338,6 +338,11 @@ animations = [ { "name": "idle", "speed": 10.0 }, { +"frames": [ SubResource( 86 ) ], +"loop": true, +"name": "idle_fire", +"speed": 5.0 +}, { "frames": [ SubResource( 93 ), SubResource( 94 ), SubResource( 95 ), SubResource( 96 ), SubResource( 97 ), SubResource( 98 ), SubResource( 98 ), SubResource( 97 ), SubResource( 96 ), SubResource( 95 ), SubResource( 94 ), SubResource( 93 ) ], "loop": true, "name": "jump", @@ -379,8 +384,8 @@ animations = [ { "speed": 10.0 }, { "frames": [ SubResource( 133 ) ], -"loop": true, -"name": "stepfire", +"loop": false, +"name": "step_fire", "speed": 10.0 }, { "frames": [ SubResource( 134 ), SubResource( 135 ), SubResource( 136 ), SubResource( 135 ) ], @@ -408,8 +413,7 @@ script = ExtResource( 1 ) [node name="AnimatedSprite" type="AnimatedSprite" parent="."] frames = SubResource( 147 ) -animation = "idle" -frame = 1 +animation = "idle_fire" playing = true flip_h = true __meta__ = { @@ -442,7 +446,7 @@ starting_state = NodePath("idle") script = ExtResource( 2 ) animation_name = "idle" move_speed = 90.0 -transition_state_nodes = [ NodePath("../jump") ] +animation_sequence = [ "idle" ] jump_node = NodePath("../jump") fall_node = NodePath("../fall") move_node = NodePath("../move") @@ -452,6 +456,7 @@ fire_node = NodePath("../fire") script = ExtResource( 7 ) animation_name = "jump" move_speed = 90.0 +animation_sequence = [ "jump" ] idle_node = NodePath("../idle") fall_node = NodePath("../fall") move_node = NodePath("../move") @@ -470,6 +475,7 @@ dash_node = NodePath(".") script = ExtResource( 4 ) animation_name = "jump" move_speed = 90.0 +animation_sequence = [ "jump" ] idle_node = NodePath("../idle") move_node = NodePath("../move") @@ -478,17 +484,23 @@ script = ExtResource( 8 ) animation_name = "hit" move_speed = -25.0 timeout_seconds = 1.0 +animation_sequence = [ "hit" ] idle_node = NodePath("../idle") [node name="fire" type="Node" parent="Controllers/state_machine"] script = ExtResource( 10 ) animation_name = "fire" timeout_seconds = 1.0 +animation_sequence = [ "fire" ] idle_node = NodePath("../idle") +fall_node = NodePath("../fall") +move_node = NodePath("../move") [node name="attack" type="Node" parent="Controllers/state_machine"] script = ExtResource( 13 ) +animation_name = "_fire" timeout_seconds = 2.0 +animation_suffix = true [node name="AttackStateMachine" type="Node" parent="Controllers"] script = ExtResource( 3 ) diff --git a/src/playerC/states/fire.gd b/src/playerC/states/fire.gd index ab35306..92fcbe1 100644 --- a/src/playerC/states/fire.gd +++ b/src/playerC/states/fire.gd @@ -1,8 +1,13 @@ extends State export (NodePath) var idle_node +export (NodePath) var fall_node +export (NodePath) var move_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) + var can_fire = true func enter() -> void: @@ -11,7 +16,7 @@ func enter() -> void: state_timeout.start() can_fire = true if modifier_stack_ref.has($"../attack") == false: - $"../attack".state_timeout.start() + $"../attack".enter() modifier_stack_ref.push_front($"../attack") func process_input(_event: InputEvent) -> State: @@ -39,6 +44,14 @@ func process_physics(delta: float) -> State: parent.velocity.y += gravity * delta parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1)) + parent.velocity.x = move_component.desired_movement_vector.x * move_speed + parent.velocity = parent.move_and_slide(parent.velocity, Vector2(0, -1)) + + if parent.velocity.x != 0.0: + return move_state + + if !parent.is_on_floor(): + return fall_state # if move_component.flipped_sprite == true: # animations.flip_h = parent.direction.x > 0 @@ -49,6 +62,8 @@ func process_physics(delta: float) -> State: return null func exit() -> void: + # force timer reset + $"../attack".state_timeout.start() for mods in modifier_stack_ref: print(mods.name) diff --git a/src/playerC/states/idle.gd b/src/playerC/states/idle.gd index 739298d..6703fba 100644 --- a/src/playerC/states/idle.gd +++ b/src/playerC/states/idle.gd @@ -21,6 +21,7 @@ func process_input(_event: InputEvent) -> State: if move_component.wants_jump() and parent.is_on_floor(): return jump_state + # if move_component.get_movement_direction() != 0.0: # return move_state if move_component.wants_shoot(): diff --git a/src/state.gd b/src/state.gd index 028a7b5..9ade050 100644 --- a/src/state.gd +++ b/src/state.gd @@ -9,16 +9,24 @@ extends Node ## ## @WIP +# export (NodePath) var jump_node + +# onready var jump_state: State = get_node(jump_node) export var animation_name: String export var move_speed: float = 60 export var timeout_seconds: float = 0.0 export(Array, String) var animation_sequence -export(Array, NodePath) var transition_state_nodes +# 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 = ''; + var modifier_stack_ref: Array # Well this didn't work var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity") @@ -42,14 +50,28 @@ func _ready(): func enter() -> void: + # Reset animation suffix in case there isn't one + animation_suffix = '' print(parent.name, " entering State: ", self.name) #modifier_stack_ref = state_modifiers - if animation_name != '': - animations.play(animation_name) - elif animation_sequence.size() > 0: + if modifier_stack_ref.empty() == false: + if modifier_stack_ref[-1].animation_name != '': + if modifier_stack_ref[-1].animation_suffix: + if animation_sequence.size() > 0: + animation_index = 0 + current_animation_sequence = 0 + # Guess I don't need this one anymore + #if animations.frames.get_animation_names().has(animation_sequence[animation_index] + modifier_stack_ref[-1].animation_name): + if animations.frames.has_animation(animation_sequence[animation_index] + modifier_stack_ref[-1].animation_name): + animation_suffix = modifier_stack_ref[-1].animation_name + else: + animations.play(modifier_stack_ref[-1].animation_name) + return + if animation_sequence.size() > 0: animation_index = 0 current_animation_sequence = 0 - animations.play(animation_sequence[animation_index]) + animations.play(animation_sequence[animation_index] + animation_suffix) + return func exit() -> void: pass @@ -61,7 +83,8 @@ func process_frame(_delta: float) -> State: if animation_sequence.size() > 0 and current_animation_sequence != animation_index: if animation_index >= animation_sequence.size(): animation_index = 0 - animations.play(animation_sequence[animation_index]) + ##TODO: Add a safety check here. + animations.play(animation_sequence[animation_index] + animation_suffix) print("An animation sequence: ", animations.animation , animation_index) current_animation_sequence = animation_index return null diff --git a/src/state_machine.gd b/src/state_machine.gd index 2628123..632b115 100644 --- a/src/state_machine.gd +++ b/src/state_machine.gd @@ -52,6 +52,11 @@ func process_frame(delta: float) -> void: if state_modifiers[-1].state_timeout.time_left == 0: print("Pop State Modifier: ", state_modifiers[-1].name) state_modifiers.pop_back() + # Reset animation suffix + current_state.animation_suffix = '' + var current_frame = current_state.animations.frame + current_state.animations.play(current_state.animation_sequence[current_state.animation_index]) + current_state.animations.frame = current_frame; var new_state = current_state.process_frame(delta) if new_state: change_state(new_state) diff --git a/src/state_modifier.gd b/src/state_modifier.gd index 3b2ea51..6faf39b 100644 --- a/src/state_modifier.gd +++ b/src/state_modifier.gd @@ -15,10 +15,15 @@ export var animation_name: String export var move_speed: float = 60 export var timeout_seconds: float = 0.0 -export(Array, String) var animation_sequence -export(Array, NodePath) var transition_state_nodes -var animation_index: int = 0 -var current_animation_sequence: int = 0 +# Attempting to use this as an animation suffix that can linger after +# state transition like 'run-fire' to 'idle-fire' 'jump-fire' etc. +export var animation_suffix: bool = false + +# not sure if I can do the array thing from state change. Maybe? +# disabling this sequencing for now. +# export(Array, String) var animation_sequence +# var animation_index: int = 0 +# var current_animation_sequence: int = 0 var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity") @@ -33,5 +38,14 @@ func _ready(): state_timeout.autostart = false add_child(state_timeout) +func enter() -> void: + print("Apply State Modifier: ", self.name) + state_timeout.start() + #modifier_stack_ref = state_modifiers + return + +func exit() -> void: + pass + func _update(): pass