Files
pomchronicles/fsm/machines/state_machine.gd

44 lines
1.0 KiB
GDScript

class_name StateMachine extends Node
@export var starting_state : String = ""
var states : Dictionary[String, StateNode] = {}
var curr_state : StateNode = null
var actor = null
func _ready() -> void:
for child in get_children():
if child is StateNode:
states[child.name] = child
child.state_machine = self
child.completed.connect(_on_state_completed)
func start() -> void:
if starting_state != "":
var state : StateNode = states.get(starting_state)
if state == null:
printerr("Starting state not found! Expected %s" % [starting_state])
else:
enter_state(state)
curr_state.execute(actor)
func exit_state(state : StateNode) -> void:
curr_state.exit()
curr_state = null
pass
func enter_state(state : StateNode) -> void:
curr_state = state
curr_state.enter()
pass
func advance_state() -> void:
pass
func _on_state_completed(state : StateNode) -> void:
if state == curr_state:
advance_state()
else:
printerr("Wrong state completed! %s when expecting %s" % [state.name, curr_state])