33 lines
963 B
C#
33 lines
963 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BITKit.StateMachine
|
|
{
|
|
public interface IState
|
|
{
|
|
void Initialize();
|
|
void OnStateEnter(IState old);
|
|
void OnStateUpdate();
|
|
void OnStateExit(IState old, IState newState);
|
|
|
|
}
|
|
public interface IStateMachine<T> where T : IState
|
|
{
|
|
T CurrentState { get; set; }
|
|
IDictionary<Type, T> StateDictonary { get; }
|
|
void TransitionState<State>() where State : T
|
|
{
|
|
var nextState = StateDictonary.GetOrCreate(typeof(State));
|
|
TransitionState(nextState);
|
|
}
|
|
void TransitionState<State>(State nextState) where State : T
|
|
{
|
|
var currentState = CurrentState;
|
|
currentState?.OnStateExit(currentState, nextState);
|
|
nextState?.OnStateEnter(currentState);
|
|
CurrentState = nextState;
|
|
}
|
|
void ExitState();
|
|
void RecoveryState();
|
|
}
|
|
} |