using System.Collections.Generic; namespace Koptilnya.StateMachine { public class StateMachine { protected Dictionary> mStates; protected State mCurrentState; public StateMachine() { mStates = new Dictionary>(); } public void Add(State state) { mStates.Add(state.ID, state); } public void Add(T stateID, State state) { mStates.Add(stateID, state); } public State GetState(T stateID) { if(mStates.ContainsKey(stateID)) return mStates[stateID]; return null; } public void SetCurrentState(T stateID) { State state = mStates[stateID]; SetCurrentState(state); } public State GetCurrentState() { return mCurrentState; } public void SetCurrentState(State state) { if (mCurrentState == state) { mCurrentState.ReEnter(); } if (mCurrentState != null) { mCurrentState.Exit(); } mCurrentState = state; if (mCurrentState != null) { mCurrentState.Enter(); } } public void Update() { if (mCurrentState != null) { mCurrentState.Update(); } } public void FixedUpdate() { if (mCurrentState != null) { mCurrentState.FixedUpdate(); } } } }