public bool SwitchState(GameState newState) { Debug.Log("SWITCH STATE " + currentState.State.ToString() + " to " + newState.ToString()); bool switchSuccess = false; if (states != null && states.ContainsKey(newState)) { if (currentState == null) { currentState = states [newState]; currentState.Start(); switchSuccess = true; } else if (currentState.AllowTransition(newState)) { currentState.End(); currentState = states [newState]; currentState.Start(); switchSuccess = true; } else { Debug.Log(string.Format("{0} does not allow transition to {1}", currentState.State, newState)); } } if (switchSuccess) { // Updating state history #if UNITY_EDITOR if (prevGameState != null) { prevGameState.Add(newState); if (prevGameState.Count > 20) { prevGameState.RemoveAt(0); } } #endif if (this.OnStatePreSwitchEvent != null) { this.OnStatePreSwitchEvent(newState); } } else { Debug.Log("States dictionary not ready for switching to " + newState); } return(switchSuccess); }
public GameStateMachine(GameManager manager) { states = new Dictionary <GameState, GameState_Base <GameState> > (); GameState_Loading loading = new GameState_Loading(manager); GameState_InGame inGame = new GameState_InGame(manager); GameState_Exit exit = new GameState_Exit(manager); states.Add(loading.State, (GameState_Base <GameState>)loading); states.Add(inGame.State, (GameState_Base <GameState>)inGame); states.Add(exit.State, (GameState_Base <GameState>)exit); currentState = loading; currentState.Start(); prevGameState = new List <GameState> (); prevGameState.Add(currentState.State); manager.BeginStateUpdate(); }