Advertisement
uurha

Untitled

Nov 27th, 2022
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. namespace EstotyWorld.StateMachines
  2. {
  3.  
  4.     public class StateMachine<TContext>
  5.     {
  6.  
  7.         private Dictionary<Type, BaseState<TContext>> map;
  8.  
  9.         public TContext Context { get; private set; }
  10.         public BaseState<TContext> CurrentState { get; private set; }
  11.  
  12.         public StateMachine(TContext context)
  13.         {
  14.             Context = context;
  15.  
  16.             map = new Dictionary<Type, BaseState<TContext>>();
  17.             CurrentState = null;
  18.         }
  19.  
  20.         public void AddState(BaseState<TContext> state)
  21.         {
  22.             Type stateType = state.GetType();
  23.  
  24.             if (map.ContainsKey(stateType))
  25.             {
  26.                 throw new InvalidOperationException(
  27.                     $"[StateMachine] AddState: State {stateType.Name} is already in map"
  28.                 );
  29.             }
  30.  
  31.             if (!state.ValidateMachine(this))
  32.             {
  33.                 state.Setup(this);
  34.             }
  35.  
  36.             map.Add(stateType, state);
  37.         }
  38.  
  39.         public T AddState<T>() where T : BaseState<TContext>, new()
  40.         {
  41.             T state = new T();
  42.             AddState(state);
  43.  
  44.             return state;
  45.         }
  46.  
  47.         public void ChangeStateTo<T>() where T : BaseState<TContext>
  48.         {
  49.             if (CurrentState != null)
  50.             {
  51.                 CurrentState.OnExit();
  52.             }
  53.  
  54.             Type stateType = typeof(T);
  55.  
  56.             if (map.TryGetValue(stateType, out BaseState<TContext> state))
  57.             {
  58.                 CurrentState = state;
  59.                 CurrentState.OnEnter();
  60.             }
  61.             else
  62.             {
  63.                 throw new InvalidOperationException(
  64.                     $"[StateMachine] ChangeStateTo<{stateType.Name}>: State not found in map"
  65.                 );
  66.             }
  67.         }
  68.  
  69.         public bool InState<T>() where T : BaseState<TContext>
  70.         {
  71.             return CurrentState != null
  72.             && CurrentState.GetType() == typeof(T);
  73.         }
  74.  
  75.     }
  76.  
  77. }
  78.  
  79. namespace EstotyWorld.StateMachines
  80. {
  81.  
  82.     public abstract class BaseState<TContext>
  83.     {
  84.  
  85.         private StateMachine<TContext> machine;
  86.  
  87.         protected StateMachine<TContext> Machine => machine;
  88.         protected TContext Context => machine.Context;
  89.  
  90.         public virtual void Setup(StateMachine<TContext> machine)
  91.         {
  92.             this.machine = machine;
  93.         }
  94.  
  95.         public bool ValidateMachine(object refMachine)
  96.         {
  97.             return machine != null
  98.             && machine == refMachine;
  99.         }
  100.  
  101.         /// <summary> Called once, when the StateMachine enters this state </summary>
  102.         public abstract void OnEnter();
  103.  
  104.         /// <summary> Called once, when the State Machine exits from this state </summary>
  105.         public abstract void OnExit();
  106.  
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement