cosmic/scripts/State Machine/tree.gd

51 lines
1.1 KiB
GDScript

extends Node
class_name StateTree
## Emitted when active state is transitioned
signal transitioned(to_state)
## Active state
@export var state : State
## Dictionary of all states
var cached_states = {}
func _ready():
for child in get_children():
child.tree = self
cached_states[child.name] = child
state.enter({})
## Make another state active. Optionally, properties can be provided with message
func transit(to : String, message : Dictionary = {}):
state.exit()
state = cached_states[to]
state.enter(message)
transitioned.emit(to)
## Provides properties to active state
func send_input(message : Dictionary = {}):
state.input(message)
## Provides properties to all states
func send_input_to_system(message : Dictionary = {}):
for state in cached_states.values():
state.input(message)
## Returns state with provided name
func get_state(state_name) -> State:
return cached_states[state_name]
#region Перегруженные виртуальные функции гойды
func _process(delta) -> void:
state.process(delta)
func _physics_process(delta) -> void:
state.physics_process(delta)
#endregion