コード例 #1
0
        public bool SwitchState(BetelgeuseState newState)
        {
            bool switchSuccess = false;

            Debug.Log("Switch state to " + newState);
            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);
        }
コード例 #2
0
        public BetelgeuseStateMachine(GameManager manager)
        {
            states = new Dictionary <BetelgeuseState, BetelgeuseState_Base <BetelgeuseState> >();

            Betelgeuse_Loading loading = new Betelgeuse_Loading(manager);
            Betelgeuse_InGame  inGame  = new Betelgeuse_InGame(manager);
            Betelgeuse_Exit    exit    = new Betelgeuse_Exit(manager);

            states.Add(loading.State, (BetelgeuseState_Base <BetelgeuseState>)loading);
            states.Add(inGame.State, (BetelgeuseState_Base <BetelgeuseState>)inGame);
            states.Add(exit.State, (BetelgeuseState_Base <BetelgeuseState>)exit);

            currentState = loading;
            currentState.Start();

            prevGameState = new List <BetelgeuseState>();
            prevGameState.Add(currentState.State);
        }