Created
May 31, 2011 08:45
-
-
Save AngryAnt/1000180 to your computer and use it in GitHub Desktop.
A simple state machine using a dictionary of delegates indexed by an enum.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections.Generic; | |
public class MyStateMachine : MonoBehaviour | |
{ | |
public delegate void StateHandlerDelegate (); | |
public enum MyStateType | |
{ | |
Idle, | |
Combat, | |
Juggling | |
} | |
private Dictionary<MyStateType, StateHandlerDelegate> m_StateHandlers; | |
private MyStateType m_State; | |
void Start () | |
{ | |
m_StateHandlers = new Dictionary<MyStateType, StateHandlerDelegate> (); | |
m_StateHandlers[MyStateType.Idle] = IdleHandler; | |
m_StateHandlers[MyStateType.Combat] = CombatHandler; | |
m_StateHandlers[MyStateType.Juggling] = JugglingHandler; | |
} | |
void Update () | |
{ | |
m_StateHandlers[m_State] (); | |
} | |
void IdleHandler () | |
{ | |
// Handle the state and set m_State to change to another state | |
} | |
void CombatHandler () | |
{ | |
// Handle the state and set m_State to change to another state | |
} | |
void JugglingHandler () | |
{ | |
// Handle the state and set m_State to change to another state | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment