/// <summary> /// 执行状态基类 /// </summary> /// <param name="_stateBase">State base.</param> public void PerformTransition(FSMStateBase _stateBase) { Debug.Log(currentState.ToString() + " -> " + _stateBase.ToString()); currentState.DoBeforeLeaving(); currentState = _stateBase; currentState.DoBeforeEntering(); }
/// <summary> /// 执行过渡条件 /// </summary> /// <param name="_transition">Transition.</param> public void PerformTransition(Transition _transition) { if (_transition == Transition.None) { Debug.LogError("PerformTransition _transition error : transition is none"); return; } FSMState stateBase = currentState.GetOutPutState(_transition); if (stateBase == FSMState.None) { Debug.LogError("PerformTransition _transition error : State does not have a target state for transition"); return; } for (int i = 0; i < statesList.Count; i++) { if (statesList[i].FSMState == stateBase) { currentState.DoBeforeLeaving(); //只有这样切换才给上一个状态赋值,当前状态已经变成了上一个状态 BeforeState = currentState; //当前状态已经被转换 currentState = statesList[i]; currentState.DoBeforeEntering(); } } }
// 清除状态 public void ClearAllState() { _allStates.Clear(); // TODO 看看是否需要OnExit _currentState = null; _currentStateId = StateID.STATE_NONE; }
public void pushState(FSMStateEnum fsmStateEnum) { FSMStateBase fsmStateBase = null; if (FSMStateDic.TryGetValue(fsmStateEnum, out fsmStateBase)) { stateBaseStack.Push(fsmStateBase); } }
/// <summary> /// 移除状态 /// </summary> /// <param name="_stateBase">State base.</param> public void RemoveState(FSMStateBase _stateBase) { for (int i = 0; i < statesList.Count; i++) { if (statesList[i] == _stateBase) { statesList.Remove(_stateBase); return; } } Debug.LogError("RemoveState error: It was not on the list of states"); }
public void Update() { if (stateBaseStack.Count <= 0) { return; } FSMStateBase fsmStateBase = stateBaseStack.Peek(); if (fsmStateBase != null) { fsmStateBase.Update(); } }
// 切换状态 public void ChangeState(StateID nextState, params object[] param) { if (nextState == _currentStateId) { // 是同一状态,刷新状态数据 if (_currentState != null) { _currentState.OnRefresh(param); } return; } // 不能进行状态切换 if (_currentState != null && !_currentState.CheckTransition(nextState)) { return; } List <StateID> list; if (_connections.TryGetValue(nextState, out list)) // 获取与指定的键相关联的值 // 如果目标状态有添加链接,则判定当前状态是否有链接到目标状态,如果没有的话,则禁止迁移状态 { if (list.IndexOf(_currentStateId) == -1) { return; } } FSMStateBase state; if (_allStates.TryGetValue(nextState, out state)) { if (_currentState != null) { _currentState.OnExit(); _currentState = null; } _currentStateId = nextState; _currentState = state; _currentState.OnEnter(param); } else { Debug.LogError("State Not Found: " + nextState); } }
/// <summary> /// 添加状态 /// </summary> /// <param name="_stateBase">State base.</param> public void AddState(FSMStateBase _stateBase) { if (_stateBase == null) { Debug.LogError("AddState error stateBase is null"); } if (statesList.Count == 0) { statesList.Add(_stateBase); currentState = _stateBase; return; } for (int i = 0; i < statesList.Count; i++) { if (statesList[i] == _stateBase) { Debug.LogError("AddState error: Impossible to add state."); return; } } statesList.Add(_stateBase); }
// 添加状态 public void AddState(StateID stateId, FSMStateBase state) { state.SetFSM(this); // 设置当前状态机 _allStates[stateId] = state; }