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

34 lines
1021 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; }
2023-08-11 23:57:37 +08:00
IDictionary<Type, T> StateDictionary { get; }
2023-06-05 19:57:17 +08:00
void TransitionState<State>() where State : T
{
2023-08-11 23:57:37 +08:00
var nextState = StateDictionary.GetOrCreate(typeof(State));
2023-06-05 19:57:17 +08:00
TransitionState(nextState);
}
void TransitionState<State>(State nextState) where State : T
{
2023-08-11 23:57:37 +08:00
if (nextState.Equals(CurrentState)) return;
2023-06-05 19:57:17 +08:00
var currentState = CurrentState;
currentState?.OnStateExit(currentState, nextState);
nextState?.OnStateEnter(currentState);
CurrentState = nextState;
}
void ExitState();
void RecoveryState();
}
}