summaryrefslogtreecommitdiff
path: root/godot/scripts/fsm/StateMachine.gd
diff options
context:
space:
mode:
authorAnson Bridges <bridges.anson@gmail.com>2022-10-11 00:15:48 -0400
committerAnson Bridges <bridges.anson@gmail.com>2022-10-11 00:15:48 -0400
commite7fb9bacf3ebb5209f90f412757c35276af51e85 (patch)
tree2dfac9d1273bf5efa1da5cfe82b4d8e64ae0bf3a /godot/scripts/fsm/StateMachine.gd
parent7dbec964a375598d454e04719576eb6c469a5d7b (diff)
ai cannon-manning state machine
Diffstat (limited to 'godot/scripts/fsm/StateMachine.gd')
-rw-r--r--godot/scripts/fsm/StateMachine.gd35
1 files changed, 35 insertions, 0 deletions
diff --git a/godot/scripts/fsm/StateMachine.gd b/godot/scripts/fsm/StateMachine.gd
new file mode 100644
index 0000000..834f036
--- /dev/null
+++ b/godot/scripts/fsm/StateMachine.gd
@@ -0,0 +1,35 @@
+class_name StateMachine
+extends Node
+
+# Emitted when transitioning to a new state.
+signal transitioned(state_name)
+
+# Path to the initial active state. We export it to be able to pick the initial state in the inspector.
+export var initial_state := NodePath()
+
+# The current active state. At the start of the game, we get the `initial_state`.
+onready var state: State = get_node(initial_state)
+
+
+func _ready() -> void:
+ yield(owner, "ready")
+ # The state machine assigns itself to the State objects' state_machine property.
+ for child in get_children():
+ child.state_machine = self
+ state.enter()
+
+func _process(delta: float) -> void:
+ state.update(delta)
+
+func _physics_process(delta: float) -> void:
+ state.physics_update(delta)
+
+func transition_to(target_state_name: String, msg: Dictionary = {}) -> void:
+ if not has_node(target_state_name):
+ print("no state")
+ return
+
+ state.exit()
+ state = get_node(target_state_name)
+ state.enter(msg)
+ emit_signal("transitioned", state.name)