Compare commits
4 commits
fe15b62976
...
13ae274106
| Author | SHA1 | Date | |
|---|---|---|---|
| 13ae274106 | |||
| 594605fe70 | |||
| fae50f0a56 | |||
| c1aaa9492f |
6 changed files with 86 additions and 0 deletions
24
scenes/classes/state.gd
Normal file
24
scenes/classes/state.gd
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
## Virtual base class for all states.
|
||||
## Extend this class and override its methods to implement a state.
|
||||
@abstract class_name State extends Node
|
||||
|
||||
## Emitted when the state finishes and wants to transition to another state.
|
||||
@warning_ignore("unused_signal")
|
||||
signal finished(next_state_path: String, data: Dictionary)
|
||||
|
||||
## Called by the state machine when receiving unhandled input events.
|
||||
@abstract func handle_input(_event: InputEvent) -> void
|
||||
|
||||
## Called by the state machine on the engine's main loop tick.
|
||||
@abstract func state_update(_delta: float) -> void
|
||||
|
||||
## Called by the state machine on the engine's physics update tick.
|
||||
@abstract func state_physics_update(_delta: float) -> void
|
||||
|
||||
## Called by the state machine upon changing the active state. The `data` parameter
|
||||
## is a dictionary with arbitrary data the state can use to initialize itself.
|
||||
@abstract func enter(previous_state_path: String, data: Dictionary = {}) -> void
|
||||
|
||||
## Called by the state machine before changing the active state. Use this function
|
||||
## to clean up the state.
|
||||
@abstract func exit() -> void
|
||||
1
scenes/classes/state.gd.uid
Normal file
1
scenes/classes/state.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bpjerj832q2gf
|
||||
6
scenes/classes/state.tscn
Normal file
6
scenes/classes/state.tscn
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://kyeowhvglgj2"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bpjerj832q2gf" path="res://scenes/classes/state.gd" id="1_m4c3p"]
|
||||
|
||||
[node name="State" type="Node"]
|
||||
script = ExtResource("1_m4c3p")
|
||||
48
scenes/classes/state_machine.gd
Normal file
48
scenes/classes/state_machine.gd
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
## Virtual base class for all nodes that deal directly with states.
|
||||
## Extend this class and override its methods to implement a state machine.
|
||||
@abstract class_name StateMachine extends Node
|
||||
|
||||
## The initial state of the state machine. If not set, the first child node is used.
|
||||
@export var initial_state: State
|
||||
|
||||
## The state machine's current loaded state
|
||||
@onready var state: State = _get_initial_state()
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
for state_node: State in find_children("*", "State"):
|
||||
state_node.finished.connect(_transition_to_next_state)
|
||||
|
||||
await owner.ready
|
||||
state.enter("")
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
state.handle_input(event)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
state.update(delta)
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
state.physics_update(delta)
|
||||
|
||||
|
||||
## Called when initial state is not specified.
|
||||
## Returns the first child node by default.
|
||||
## Override if necessary, but don't call it directly.
|
||||
func _get_initial_state() -> State:
|
||||
return initial_state if initial_state != null else get_child(0)
|
||||
|
||||
|
||||
## Transitions the active state out after receiving a finished signal.
|
||||
func _transition_to_next_state(target_state_path: String, data: Dictionary = {}) -> void:
|
||||
if not has_node(target_state_path):
|
||||
printerr(owner.name + ": Trying to transition to state " + target_state_path + " but it does not exist.")
|
||||
return
|
||||
|
||||
var previous_state_path: StringName = state.name
|
||||
state.exit()
|
||||
state = get_node(target_state_path)
|
||||
state.enter(previous_state_path, data)
|
||||
1
scenes/classes/state_machine.gd.uid
Normal file
1
scenes/classes/state_machine.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dqjaxgmyxq3rx
|
||||
6
scenes/classes/state_machine.tscn
Normal file
6
scenes/classes/state_machine.tscn
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://cy4h0xcy2kscg"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dqjaxgmyxq3rx" path="res://scenes/classes/state_machine.gd" id="1_rnohx"]
|
||||
|
||||
[node name="StateMachine" type="Node"]
|
||||
script = ExtResource("1_rnohx")
|
||||
Loading…
Add table
Reference in a new issue