34 lines
1002 B
C#
34 lines
1002 B
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using BITKit;
|
||
|
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();
|
||
|
}
|
||
|
}
|