SupaLidlGame/State/StateMachine.cs

88 lines
2.2 KiB
C#
Raw Normal View History

2023-05-23 00:23:53 -07:00
using Godot;
2023-07-23 23:39:20 -07:00
using SupaLidlGame.Extensions;
2023-08-10 23:08:41 -07:00
using System.Collections.Generic;
2023-05-23 00:23:53 -07:00
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.State;
2023-07-21 02:54:13 -07:00
public abstract partial class StateMachine<T> : Node where T : Node, IState<T>
2023-05-23 00:23:53 -07:00
{
2023-08-08 00:54:00 -07:00
[Signal]
2023-08-10 23:08:41 -07:00
public delegate void StateChangedEventHandler(Node state);
2023-08-08 00:54:00 -07:00
2023-06-03 18:21:46 -07:00
public T CurrentState { get; protected set; }
public abstract T InitialState { get; set; }
public override void _Ready()
2023-05-23 00:23:53 -07:00
{
2023-06-03 18:21:46 -07:00
ChangeState(InitialState);
}
2023-05-23 00:23:53 -07:00
2023-08-10 23:08:41 -07:00
public virtual bool ChangeState(T nextState)
{
return ChangeState(nextState, out Stack<T> _);
}
public bool ChangeState(T nextState, out T finalState)
{
var status = ChangeState(nextState, out Stack<T> states);
finalState = states.Peek();
return status;
}
public bool ChangeState(T nextState, out Stack<T> states)
{
states = new Stack<T>();
states.Push(CurrentState);
var result = ChangeStateRecursive(nextState, states);
EmitSignal(SignalName.StateChanged, states.Peek());
return result;
}
protected virtual bool ChangeStateRecursive(
T nextState,
Stack<T> states)
2023-06-03 18:21:46 -07:00
{
if (nextState is null)
{
return false;
}
2023-05-23 00:23:53 -07:00
2023-07-21 02:54:13 -07:00
// NOTE: proxied states can call Exit() more than once
2023-06-03 18:21:46 -07:00
if (CurrentState is not null)
2023-05-23 00:23:53 -07:00
{
2023-06-03 18:21:46 -07:00
CurrentState.Exit(nextState);
2023-05-23 00:23:53 -07:00
}
2023-07-21 02:54:13 -07:00
var previousState = CurrentState;
2023-06-03 18:21:46 -07:00
CurrentState = nextState;
2023-08-10 23:08:41 -07:00
states.Push(nextState);
2023-06-03 18:21:46 -07:00
// if the next state decides it should enter a different state,
// then we enter that different state instead
2023-07-21 02:54:13 -07:00
var nextNextState = nextState.Enter(previousState);
2023-06-03 18:21:46 -07:00
if (nextNextState is T t)
2023-05-23 00:23:53 -07:00
{
2023-08-10 23:08:41 -07:00
return ChangeStateRecursive(t, states);
2023-05-23 00:23:53 -07:00
}
2023-06-03 18:21:46 -07:00
return true;
2023-05-23 00:23:53 -07:00
}
2023-07-23 23:39:20 -07:00
/// <summary>
/// Changes the current state to a state of type U which must inherit from T.
/// </summary>
public bool ChangeState<U>(out U state) where U : T
{
state = this.FindChildOfType<U>();
return ChangeState(state);
}
2023-08-05 23:50:08 -07:00
public bool ChangeState(string name, out T state)
{
state = GetNode<T>(name);
return ChangeState(state);
}
2023-05-23 00:23:53 -07:00
}