Example #1
0
    /// <summary>
    /// 添加切换的状态.
    /// </summary>
    /// <param name="trans">Trans.</param>
    /// <param name="stateID">State I.</param>
    public void AddTransition(EBehaviourTransition trans, EBehaviourStateID stateID)
    {
        // 不要传空的进来哦.
        if (trans == EBehaviourTransition.None)
        {
            Debug.LogError("FSMState ERROR: NullTransition is not allowed for a real transition");
            return;
        }

        if (stateID == EBehaviourStateID.None)
        {
            Debug.LogError("FSMState ERROR: NullStateID is not allowed for a real ID");
            return;
        }

        // Since this is a Deterministic FSM,
        // check if the current transition was already inside the map
        if (TransitionMap.ContainsKey(trans))
        {
            Debug.LogError("FSMState ERROR: State " + stateID.ToString() + " already has transition " + trans.ToString() +
                           "Impossible to assign to another state");
            return;
        }

        TransitionMap.Add(trans, stateID);
    }
Example #2
0
 /// <summary>
 /// This method returns the new state the FSM should be if
 /// this state receives a transition and
 /// </summary>
 public EBehaviourStateID GetOutputState(EBehaviourTransition trans)
 {
     // Check if the map has this transition
     if (TransitionMap.ContainsKey(trans))
     {
         return(TransitionMap [trans]);
     }
     return(EBehaviourStateID.None);
 }
Example #3
0
    /// <summary>
    /// 删除转换.
    /// </summary>
    /// <param name="trans">Trans.</param>
    public void DeleteTransition(EBehaviourTransition trans)
    {
        // 不要传空的进来哦.
        if (trans == EBehaviourTransition.None)
        {
            Debug.LogError("FSMState ERROR: NullTransition is not allowed for a real transition");
            return;
        }

        // Check if the pair is inside the map before deleting
        if (TransitionMap.ContainsKey(trans))
        {
            TransitionMap.Remove(trans);
            return;
        }
        Debug.LogError("FSMState ERROR: Transition " + trans.ToString() + " passed to " + _StateID.ToString() +
                       " was not on the state's transition list");
    }
    public bool CanTransition(EBehaviourTransition trans)
    {
        // Check for NullTransition before changing the current state
        if (trans == EBehaviourTransition.None)
        {
            Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition");
            return(false);
        }

        EBehaviourStateID id = _CurState.GetOutputState(trans);

        if (id == EBehaviourStateID.None)
        {
//			Debug.LogWarning("FSM ERROR: State " + _CurStateID.ToString() +  " does not have a target state " +
//				" for transition " + trans.ToString());
            return(false);
        }
        return(true);
    }
    /// <summary>
    /// 执行转换,外部调用关键方法。
    /// </summary>
    /// <param name="trans">Trans.</param>
    public void PerformTransition(EBehaviourTransition trans, object param = null)
    {
        if (!CanTransition(trans))
        {
            return;
        }

        _CurStateID = _CurState.GetOutputState(trans);
        foreach (BehaviourState state in _States)
        {
            if (state.ID == _CurStateID)
            {
                _CurState.OnExit();
                _CurState = state;
                _CurState.OnEnterBefore();
                _CurState.OnEnter(param);
                break;
            }
        }
    }