Created base state machine and setup existing one as one for animated sprites with a kinematic body as a parent.

This commit is contained in:
Nitsud Yarg 2024-05-27 17:32:42 -07:00
parent a7bc6c6349
commit fbcb5af32d
5 changed files with 46 additions and 21 deletions

View File

@ -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": ""
}

View File

@ -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:

View File

@ -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"]

View File

@ -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)

View File

@ -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)