From 0438f1ac45c0c2f5c682613392038e937f4e9bb7 Mon Sep 17 00:00:00 2001 From: Nitsud Yarg Date: Mon, 27 May 2024 17:32:42 -0700 Subject: [PATCH] Created base state machine and setup existing one as one for animated sprites with a kinematic body as a parent. --- project.godot | 6 +++++ src/playerC/Player-KinematicBody2D.gd | 2 +- src/playerC/Player.tscn | 5 ++-- src/state_machine.gd | 20 ++-------------- src/state_machine_animated_actor.gd | 34 +++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 src/state_machine_animated_actor.gd diff --git a/project.godot b/project.godot index a5629f3..ad5fec3 100644 --- a/project.godot +++ b/project.godot @@ -34,6 +34,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://src/state_machine.gd" }, { +"base": "StateMachine", +"class": "StateMachineAnimatedActor", +"language": "GDScript", +"path": "res://src/state_machine_animated_actor.gd" +}, { "base": "Node", "class": "StateModifier", "language": "GDScript", @@ -45,6 +50,7 @@ _global_script_class_icons={ "PushdownStateMachine": "", "State": "", "StateMachine": "", +"StateMachineAnimatedActor": "", "StateModifier": "" } diff --git a/src/playerC/Player-KinematicBody2D.gd b/src/playerC/Player-KinematicBody2D.gd index b4a9030..38dbd53 100644 --- a/src/playerC/Player-KinematicBody2D.gd +++ b/src/playerC/Player-KinematicBody2D.gd @@ -18,7 +18,7 @@ onready var player_hurtbox = $Hurtbox_Component/CollisionShape2D onready var gun = $Gun func _ready() -> void: - movement_state_machine.init(self, movement_animations, player_move_component) + movement_state_machine.init_animated_actor(self, movement_animations, player_move_component) player_move_component.player_number = player_number func _unhandled_input(event: InputEvent) -> void: diff --git a/src/playerC/Player.tscn b/src/playerC/Player.tscn index 82b9d06..7c34b04 100644 --- a/src/playerC/Player.tscn +++ b/src/playerC/Player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=88 format=2] +[gd_scene load_steps=89 format=2] [ext_resource path="res://src/playerC/Player-KinematicBody2D.gd" type="Script" id=1] [ext_resource path="res://src/playerC/states/idle.gd" type="Script" id=2] @@ -13,6 +13,7 @@ [ext_resource path="res://src/playerC/states/none.gd" type="Script" id=11] [ext_resource path="res://src/components/Hurtbox_Component.tscn" type="PackedScene" id=12] [ext_resource path="res://src/state_modifier.gd" type="Script" id=13] +[ext_resource path="res://src/state_machine_animated_actor.gd" type="Script" id=14] [sub_resource type="StreamTexture" id=75] load_path = "res://.import/MegaMan.png-dc2c293e5a0f5d183ee961942ac90e4a.stex" @@ -439,7 +440,7 @@ shape = SubResource( 74 ) script = ExtResource( 6 ) [node name="state_machine" type="Node" parent="Controllers"] -script = ExtResource( 3 ) +script = ExtResource( 14 ) starting_state = NodePath("idle") [node name="idle" type="Node" parent="Controllers/state_machine"] diff --git a/src/state_machine.gd b/src/state_machine.gd index 632b115..639a77d 100644 --- a/src/state_machine.gd +++ b/src/state_machine.gd @@ -7,13 +7,10 @@ var state_modifiers: Array # Initialize the state machine by giving each child state a reference to the # parent object it belongs to and enter the default starting_state. -func init(parent: KinematicBody2D, animations: AnimatedSprite, move_component) -> void: +func init() -> void: for child in get_children(): if child is State: - print("Initializing State Node for ", parent.name, ": ", child.name) - child.parent = parent - child.animations = animations - child.move_component = move_component + print("Initializing State Node: ", child.name) child.modifier_stack_ref = state_modifiers # Initialize to the default state @@ -44,19 +41,6 @@ func process_input(event: InputEvent) -> void: change_state(new_state) func process_frame(delta: float) -> void: - # Check the state modifiers for timeouts, and maybe more conditions later - # If the modifier is no longer valid, pop it from the stack. - # We could iterate through a whole list of states but I'm not sure i want - # states to be that complex. - if state_modifiers.empty() == false: - 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_machine_animated_actor.gd b/src/state_machine_animated_actor.gd new file mode 100644 index 0000000..ed385bb --- /dev/null +++ b/src/state_machine_animated_actor.gd @@ -0,0 +1,34 @@ +class_name StateMachineAnimatedActor extends StateMachine + +# Initialize the state machine by giving each child state a reference to the +# parent object it belongs to and enter the default starting_state. +func init_animated_actor(parent: KinematicBody2D, animations: AnimatedSprite, move_component) -> void: + for child in get_children(): + if child is State: + print("Initializing State Node for ", parent.name, ": ", child.name) + child.parent = parent + child.animations = animations + child.move_component = move_component + child.modifier_stack_ref = state_modifiers + + # Initialize to the default state + change_state(get_node(starting_state)) + + +func process_frame(delta: float) -> void: + # Check the state modifiers for timeouts, and maybe more conditions later + # If the modifier is no longer valid, pop it from the stack. + # We could iterate through a whole list of states but I'm not sure i want + # states to be that complex. + if state_modifiers.empty() == false: + 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)