PokemonLike/Library/Utilities/StateMachine.cs

38 lines
958 B
C#
Raw Normal View History

2025-04-11 13:35:55 -05:00
using Godot;
using PokemonLike.Library.Support;
namespace PokemonLike.Library.Utilities;
[GlobalClass]
public partial class StateMachine : Node
{
[ExportCategory("State Machine Vars")]
[Export] public Node Customer;
[Export] public State CurrentState;
public override void _Ready()
{
Logger.Info("Loading state machine...");
foreach (Node child in GetChildren())
{
if (child is State state)
{
state.StateOwner = Customer;
state.SetProcess(false);
}
}
}
public string GetCurrentState() => CurrentState.Name.ToString();
public void ChangeState(State newState)
{
CurrentState?.ExitState();
CurrentState = newState;
CurrentState?.EnterState();
foreach (Node child in GetChildren())
if (child is State state)
state.SetProcess(state == CurrentState);
}
}