Пример #1
0
    /// <summary>
    /// 查找并切换对应id的状态
    /// </summary>
    /// <param name="stateId"></param>
    public void ChangeState(SoldierStateID stateId)
    {
        if (stateId == SoldierStateID.NullState)
        {
            Debug.LogError("SoldierFSM ERROR: 不允许切换空状态");
        }

        //遍历此状态容器
        foreach (SoldierFSMState state in _states)
        {
            if (state.StateID == stateId)
            {
                if (null != _currentState)
                {
                    _currentState.DoBeforeLeaving(this);
                }
                // 设置前置状态
                SourceStateID = _currentStateId;
                //只允许在这里切换状态
                _currentState   = state;
                _currentStateId = state.StateID;
                //Debug.Log("执行状态切换-------------------" + state.StateID);
                if (null != _currentState)
                {
                    _currentState.DoBeforeEntering(this);
                }
                // TODO 同步状态切换操作
                // 同步数据包括 状态, FSM数据, 单位属性, 单位位置, 单位方向, 目标点列表(路径), 目标(技能/攻击目标)
                break;
            }
        }
    }
Пример #2
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(SoldierTransition trans)
    {
        // Check for NullTransition before changing the current state
        if (trans == SoldierTransition.NullTransition)
        {
            Debug.LogError("SoldierFSM ERROR: NullTransition is not allowed for a real transition");
            return;
        }

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

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

        // Update the currentStateID and currentState
        currentStateID = id;
        foreach (SoldierFSMState state in states)
        {
            if (state.ID == currentStateID)
            {
                // Do the post processing of the state before setting the new one
                currentState.DoBeforeLeaving();

                currentState = state;

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