using System; namespace OM { /// /// a simple state machine that can be used to manage states in a generic way. /// /// public class OMSimpleStateMachine { public event Action OnStateChanged; /// /// Last state before the current state. /// public T LastState { get; private set; } /// /// Current state of the state machine. /// public T CurrentState { get; private set; } /// /// Constructor for the state machine. /// /// /// public OMSimpleStateMachine(T initialState, Action onStateChanged = null) { if (onStateChanged != null) OnStateChanged += onStateChanged; SetNewState(initialState, force: true); } /// /// Sets a new state for the state machine. /// /// /// public void SetNewState(T newState, bool force = false) { if (!force && CurrentState != null && CurrentState.Equals(newState)) return; LastState = CurrentState; CurrentState = newState; OnStateChanged?.Invoke(CurrentState, LastState); } /// /// Subscribes to the state changed event. /// /// public void OnChange(Action callback) { OnStateChanged += callback; } } }