Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. public interface IState
  2. {
  3.     void OnEnter();
  4.  
  5.     void OnExecute();
  6.  
  7.     void OnExit();
  8. }
  9.  
  10. public class StateMachine
  11. {
  12.     private IState currentState;
  13.     private IState previousState;
  14.  
  15.     // call OnUpdate in actor OnUpdate class, which is called by ActorManager which is called by GameManager xD
  16.     // trying to make this whole thing explicit, otherwise running game logic on a different thread will be hard
  17.     // with different thread, I mean a different CPUthread, not Coroutine
  18.     public void OnUpdate()
  19.     {
  20.         if (currentState != null)
  21.         {
  22.             currentState.OnExecute();
  23.         }
  24.     }
  25.  
  26.     public void ChangeState(IState state)
  27.     {
  28.         if (currentState != null)
  29.         {
  30.             currentState.OnExit();
  31.         }
  32.  
  33.         previousState = currentState;
  34.         currentState = state;
  35.  
  36.         currentState.OnEnter();
  37.     }
  38.  
  39.     public IState GetState()
  40.     {
  41.         return currentState;
  42.     }
  43.  
  44.     public void RevertToPreviousState()
  45.     {
  46.         if (currentState != null)
  47.         {
  48.             currentState = previousState;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement