1
This commit is contained in:
@@ -36,11 +36,38 @@ namespace BITKit.StateMachine
|
||||
bool Enabled { get; set; }
|
||||
T CurrentState { get; set; }
|
||||
event Action<T,T> OnStateChanged;
|
||||
event Action<T> OnStateRegistered;
|
||||
event Action<T> OnStateUnRegistered;
|
||||
|
||||
IDictionary<Type, T> StateDictionary { get; }
|
||||
void Initialize();
|
||||
void UpdateState(float deltaTime);
|
||||
void DisposeState();
|
||||
void TransitionState<State>() where State : T;
|
||||
void TransitionState(T state);
|
||||
void Register(T newState) => throw new NotImplementedException("未实现的接口");
|
||||
void UnRegister(T newState) => throw new NotImplementedException("未实现的接口");
|
||||
void InvokeOnStateRegistered(T state){}
|
||||
void InvokeOnStateUnRegistered(T state){}
|
||||
}
|
||||
|
||||
public static class StateMachineUtils
|
||||
{
|
||||
public static void Register<T>(IStateMachine<T> stateMachine, T newState) where T : IState
|
||||
{
|
||||
if (stateMachine.StateDictionary.ContainsKey(newState.GetType()))
|
||||
{
|
||||
throw new ArgumentException($"State {newState.GetType().Name} already registered");
|
||||
}
|
||||
stateMachine.StateDictionary.Add(newState.GetType(), newState);
|
||||
newState.Initialize();
|
||||
stateMachine.InvokeOnStateRegistered(newState);
|
||||
}
|
||||
public static void UnRegister<T>(this IStateMachine<T> stateMachine, T newState) where T : IState
|
||||
{
|
||||
if (!stateMachine.StateDictionary.ContainsKey(newState.GetType())) return;
|
||||
stateMachine.StateDictionary.Remove(newState.GetType());
|
||||
stateMachine.InvokeOnStateUnRegistered(newState);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user