Files
gobbo/gobbos_delivery/scripts/states/StateMachine.cs
2026-04-10 17:53:31 -05:00

54 lines
1.2 KiB
C#

using Godot;
using System.Collections.Generic;
public partial class StateMachine : State {
[Export]
public State InitialState { get; set; }
private State currentState;
private readonly Dictionary<string, State> 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;
}
}