BITKit/Packages/Runtime~/Core/Interfaces/IStateMachine.cs

33 lines
963 B
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System;
using System.Collections.Generic;
2023-06-29 14:57:11 +08:00
2023-06-05 19:57:17 +08:00
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();
}
}