Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. public class StackFSM : IDisposable {
  2.  
  3. // Default state
  4. private readonly Action<float> _defaultState;
  5.  
  6.  
  7. // FSM Stack
  8. private readonly Stack<Action<float>> _actionStack = new Stack<Action<float>>();
  9.  
  10. // Disposing bool
  11. private bool _disposing;
  12.  
  13. // Constructor
  14. public StackFSM(Action<float> defaultState) {
  15. _defaultState = defaultState;
  16. }
  17.  
  18. // Get the current state from the stack
  19. public Action<float> GetCurrentState() {
  20. // Return the state at the top of the stack or return the default state if empty
  21. return _actionStack.Count > 0 ? _actionStack.Peek() : _defaultState;
  22. }
  23.  
  24. // Push a new state to the stack
  25. public void PushState(Action<float> actionState) {
  26. _actionStack.Push(actionState);
  27. }
  28.  
  29. // Pop a state from the Stack
  30. public void PopState() {
  31. _actionStack.Pop();
  32. }
  33.  
  34. // Update the state machine
  35. public void Update(float deltaTime) {
  36. var state = GetCurrentState();
  37. state?.Invoke(deltaTime);
  38. }
  39.  
  40. // Dispose of the class
  41. public void Dispose() {
  42. // Exit if already disposing
  43. if (_disposing) return;
  44.  
  45. // Clear all actions from the stack
  46. _actionStack.Clear();
  47.  
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement