コード例 #1
0
        /// <summary>
        /// This method places new states inside the FSM,
        /// or prints an ERROR message if the state was already inside the List.
        /// First state added is also the initial state.
        /// </summary>
        public void AddState(DCFSMState s)
        {
            // Check for Null reference before deleting
            if (s == null)
            {
                Debug.LogError("FSM ERROR: Null reference is not allowed");
            }

            // First State inserted is also the Initial state,
            //   the state the machine is in when the simulation begins
            if (states.Count == 0)
            {
                states.Add(s.ID, s);
                currentState   = s;
                currentStateID = s.ID;
                return;
            }

            // Add the state to the List if it's not inside it
            if (states.ContainsKey(s.ID))
            {
                Debug.LogError("FSM ERROR: Impossible to add state " + s.ID.ToString() +
                               " because state has already been added");
                return;
            }
            states.Add(s.ID, s);
        }
コード例 #2
0
        public void BackToPreviousState()
        {
            if (mStateStack.Count > 1)
            {
                var top = mStateStack.Pop();
                top.DoBeforeLeaving();

                var previous = mStateStack.Pop();
                currentStateID = previous.ID;
                currentState   = previous;

                previous.DoBeforeEntering();
            }
        }
コード例 #3
0
        /// <summary>
        /// This method tries to change the state the FSM is in based on
        /// the current state and the transition passed. If current state
        ///  doesn't have a target state for the transition passed,
        /// an ERROR message is printed.
        /// </summary>
        public void PerformTransition(int trans)
        {
            // Check for NullTransition before changing the current state
            if (trans == DCFSMState.common_none_trans)
            {
                Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition");
                return;
            }

            // Check if the currentState has the transition passed as argument
            var id = currentState.GetOutputState(trans);

            if (id == DCFSMState.common_none_state)
            {
                Debug.LogError("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " +
                               " for transition " + trans.ToString());
                return;
            }

            // Update the currentStateID and currentState
            currentStateID = id;
            if (states.TryGetValue(id, out var state))
            {
                // Do the post processing of the state before setting the new one
                currentState.DoBeforeLeaving();

                currentState = state;

                //如果栈里面已经有这个状态,删除这个状态
                Toolkit.RemoveFromStack(mStateStack, currentState);
                //加到状态栈
                mStateStack.Push(currentState);

                // Reset the state to its desired condition before it can reason or act
                currentState.DoBeforeEntering();
            }
        }