示例#1
0
        public void ChangeState(T newState, StateTransition transition)
        {
            if (stateLookup == null)
            {
                throw new Exception("States have not been configured, please call initialized before trying to set state");
            }

            if (!stateLookup.ContainsKey(newState))
            {
                throw new Exception("No state with the name " + newState.ToString() + " can be found. Please make sure you are called the correct type the statemachine was initialized with");
            }

            var nextState = stateLookup[newState];

            if (currentState == nextState)
            {
                return;
            }

            //Cancel any queued changes.
            if (queuedChange != null)
            {
                engine.StopCoroutine(queuedChange);
                queuedChange = null;
            }

            switch (transition)
            {
            //case StateMachineTransition.Blend:
            //Do nothing - allows the state transitions to overlap each other. This is a dumb idea, as previous state might trigger new changes.
            //A better way would be to start the two couroutines at the same time. IE don't wait for exit before starting start.
            //How does this work in terms of overwrite?
            //Is there a way to make this safe, I don't think so?
            //break;
            case StateTransition.Safe:
                if (isInTransition)
                {
                    if (exitRoutine != null)                             //We are already exiting current state on our way to our previous target state
                    {
                        //Overwrite with our new target
                        destinationState = nextState;
                        return;
                    }

                    if (enterRoutine != null)                             //We are already entering our previous target state. Need to wait for that to finish and call the exit routine.
                    {
                        //Damn, I need to test this hard
                        queuedChange = WaitForPreviousTransition(nextState);
                        engine.StartCoroutine(queuedChange);
                        return;
                    }
                }
                break;

            case StateTransition.Overwrite:
                if (currentTransition != null)
                {
                    engine.StopCoroutine(currentTransition);
                }
                if (exitRoutine != null)
                {
                    engine.StopCoroutine(exitRoutine);
                }
                if (enterRoutine != null)
                {
                    engine.StopCoroutine(enterRoutine);
                }

                //Note: if we are currently in an EnterRoutine and Exit is also a routine, this will be skipped in ChangeToNewStateRoutine()
                break;
            }


            if ((currentState != null && currentState.hasExitRoutine) || nextState.hasEnterRoutine)
            {
                isInTransition    = true;
                currentTransition = ChangeToNewStateRoutine(nextState, transition);
                engine.StartCoroutine(currentTransition);
            }
            else             //Same frame transition, no coroutines are present
            {
                if (currentState != null)
                {
                    currentState.ExitCall();
                    currentState.Finally();
                }

                lastState    = currentState;
                currentState = nextState;
                if (currentState != null)
                {
                    currentState.EnterCall();
                    if (Changed != null)
                    {
                        Changed((T)currentState.state);
                    }
                }
                isInTransition = false;
            }
        }
        private void ChangeToNewState(T i_NewState)
        {
            if (m_StateLookup == null)
            {
                throw new Exception("States have not been configured, please call initialized before trying to set state.");
            }

            if (!m_StateLookup.ContainsKey(i_NewState))
            {
                throw new Exception("No state with the name " + i_NewState.ToString() + " can be found. Please make sure you are called the correct type the statemachine was initialized with.");
            }

            StateMapping nextState = m_StateLookup[i_NewState];

            if (m_CurrentState == nextState)
            {
                return;
            }

            // Cancel any queued changes.

            if (m_QueuedChange != null)
            {
                m_StateMachineRunner.StopCoroutine(m_QueuedChange);
                m_QueuedChange = null;
            }

            if (m_IsInTransition)
            {
                if (m_ExitRoutine != null)          // We are already exiting current state on our way to our previous target state.
                {
                    m_DestinationState = nextState; // Overwrite with our new target.
                    return;
                }

                if (m_EnterRoutine != null) // We are already entering our previous target state. Need to wait for that to finish and call the exit routine.
                {
                    m_QueuedChange = WaitForPreviousTransition(nextState);
                    m_StateMachineRunner.StartCoroutine(m_QueuedChange);
                    return;
                }
            }

            if ((m_CurrentState != null && m_CurrentState.hasExitRoutine) || nextState.hasEnterRoutine)
            {
                m_IsInTransition = true;

                m_CurrentTransition = ChangeToNewStateRoutine(nextState);
                m_StateMachineRunner.StartCoroutine(m_CurrentTransition);
            }
            else // Same frame transition, no coroutines are present.
            {
                if (m_CurrentState != null)
                {
                    m_CurrentState.ExitCall();
                    m_CurrentState.Finally();
                }

                m_PrevState    = m_CurrentState;
                m_CurrentState = nextState;

                if (m_CurrentState != null)
                {
                    m_CurrentState.EnterCall();

                    if (m_OnStateChanged != null)
                    {
                        m_OnStateChanged((T)m_CurrentState.state);
                    }
                }

                m_IsInTransition = false;
            }
        }