Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ConsoleApp3
  6. {
  7.     class StateMachine
  8.     {
  9.         private enum State
  10.         {
  11.             NoPuck,
  12.             Puck,
  13.         }
  14.  
  15.         private Dictionary<State, Action> m_handleUpdate;
  16.  
  17.         private State m_currentState;
  18.  
  19.         public StateMachine()
  20.         {
  21.             m_currentState = State.NoPuck;
  22.             InitializeUpdateFunctions();
  23.         }
  24.  
  25.         private void InitializeUpdateFunctions()
  26.         {
  27.             // here we create a map of functions to state values and assign them to anonymous functions
  28.             m_handleUpdate = new Dictionary<State, Action>();
  29.  
  30.             m_handleUpdate[State.NoPuck] = () =>
  31.             {
  32.                 // This lambda handles the update functionality in the NoPuck state
  33.             };
  34.  
  35.             m_handleUpdate[State.Puck] = () =>
  36.             {
  37.                 // this lambda handles the update functionality in the Puck state
  38.             };
  39.         }
  40.  
  41.         public void Update()
  42.         {
  43.             // We can call this dictionary element as we could any function now
  44.             m_handleUpdate[m_currentState]();
  45.         }
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement