Chelimbalo/scripts/state_machine/machine.gd
2025-11-26 16:51:39 +05:00

45 lines
1 KiB
GDScript

extends Node
class_name StateMachine
@export var current_state: State
var states: Dictionary[StringName,State] = {}
func _ready() -> void:
for child in get_children():
if child is State:
states[child.name] = child
child.transition.connect(on_transition_required.rpc)
else:
push_warning("Child of state machine is not state")
@rpc("authority","call_local","reliable")
func on_transition_required(to: StringName):
if states.has(to) == false:
push_warning("Incorrect state request: " + to)
return
change_state(states[to])
func change_state(to_state: State) -> void:
if current_state != null:
current_state.exit()
current_state = to_state
current_state.enter()
@rpc("authority","call_local","unreliable")
func clear_state():
if current_state == null:
return
current_state.exit()
current_state = null
func _process(delta: float) -> void:
if current_state == null:
return
current_state.update(delta)
func _physics_process(delta: float) -> void:
if current_state == null:
return
current_state.physics_update(delta)