Advertisement
Guest User

gamestatemachine

a guest
Jul 6th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. /// <summary>
  2. /// Represents a hierarchical state machine to manage macro flow control in the game.
  3. /// </summary>
  4. public class GameStateMachine
  5. {
  6.     Stack<GameState> states;
  7.  
  8.     /// <summary>
  9.     /// Gets the active game state.
  10.     /// </summary>
  11.     public GameState Current
  12.     {
  13.         get
  14.         {
  15.             if (states.Count == 0) return null;
  16.             else return states.Peek();
  17.         }
  18.     }
  19.  
  20.     public void Update()
  21.     {
  22.         Current.Update();
  23.     }
  24.  
  25.     public void Draw()
  26.     {
  27.         Current.Draw();
  28.     }
  29. }
  30.  
  31. /// <summary>
  32. /// Represents a logical state of the game within the structure of a <see cref="GameStateMachine"/>.
  33. /// </summary>
  34. public abstract class GameState
  35. {
  36.     /// <summary>
  37.     /// Allows the state to implement custom logic.
  38.     /// </summary>
  39.     public virtual void Update() { }
  40.     /// <summary>
  41.     /// Allows the state to implement custom rendering.
  42.     /// </summary>
  43.     public virtual void Draw() { }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement