using Godot; using System.Collections.Generic; public partial class StateMachine : State { [Export] public State InitialState { get; set; } private State currentState; private readonly Dictionary stateList = []; public override void _Ready() { foreach (var child in GetChildren()) { if (child is State state) { stateList.Add(child.Name.ToString().ToLower(), state); state.Transitioned += OnChildTransition; } } if (InitialState != null) { InitialState.Enter(); currentState = InitialState; } } public void Process(float delta) { currentState?.Update(delta); } public override void PhysicsUpdate(double delta) { currentState?.PhysicsUpdate(delta); } public void OnChildTransition(Node sender, string newStateName) { if (sender != currentState) { return; } var newState = stateList[newStateName.ToLower()]; if (newState == null) { return; } currentState?.Exit(); newState.Enter(); currentState = newState; } }