示例#1
0
        public void ReplaceState(FSMState newState)
        {
            canUpdate = false;

            if (currentState != null)
            {
                currentState.OnExit();

                if (currentState.IsBasicState == false)
                {
                    currentStates.Pop();
                }
            }

            if (newState != null)
            {
                currentState = newState;
                currentStates.Push(currentState);
                currentState.OnEnter(this, () => canUpdate = true);
            }
            else
            {
                if (currentStates.Count > 0)
                {
                    currentState = currentStates.Peek();
                    currentState.OnEnter(this, () => canUpdate = true);
                }
                else
                {
                    Debug.Log("States empty");
                }
            }
        }
示例#2
0
        public virtual void OnUpdate()
        {
            if (debug)
            {
                DebugWithFSM();
            }

            //===================================
            // 更新Transition

            // 更新anyState的Transition
            if (anyState != null)
            {
                foreach (var transition in anyState.transitions)
                {
                    if (transition.IsValid())
                    {
                        activeState.OnExit();
                        activeState = transition.GetNextState();
                        activeState.OnEnter();
                        return;
                    }
                }
            }

            // 遍历当前活动状态的所有转换条件,进行状态转换
            foreach (var transition in activeState.transitions)
            {
                if (transition.IsValid())
                {
                    activeState.OnExit();
                    activeState = transition.GetNextState();
                    activeState.OnEnter();
                    return;
                }
            }


            //===================================
            // 更新State

            // 更新anyState
            if (anyState != null)
            {
                anyState.OnUpdate();
            }

            // 没有状态可以转换的时候,执行当前状态要进行的事件
            activeState.OnUpdate();
        }