public void PerformTransition(Transition trans) { if (trans == Transition.Null) { Debug.LogWarning("trans为null无法发生转换"); return; } StateID id = FSMstate.GetStateID(trans); if (id == StateID.Null) { Debug.LogWarning("id为null,无法发生转换"); return; } if (StateDic.ContainsKey(id)) { FSMState state = StateDic[id]; FSMstate.DoAfterLeaving(); FSMstate = state; stateId = id; FSMstate.DoBeforeEntering(); } else { Debug.LogWarning("要转换的" + id + "不存在"); } }
// 该方法基于当前状态和过渡状态是否通过来尝试改变状态机的状态,当前状态没有目标状态过渡时则打印错误信息 // 切换当前状态到下一个要转换的状态 public void PerformTransition(Transition trans) { if (trans == Transition.NullTransition) { Debug.LogError("FSM error:NullTransition is not allowed"); return; } StateId id = _currentFsmState.GetOutputState(trans); if (id == StateId.NullStateId) { Debug.LogError("FSM error: State " + _currentFsmState.ID.ToString() + "does not allowed"); return; } //更新当前的状态机和状态编号 _currentStateId = id; foreach (FSMState state in states) { if (state.ID == _currentStateId) { _currentFsmState.DoBeforeLeaving(); _currentFsmState = state; _currentFsmState.DoBeforeEntering(); break; } } }
/// <summary> /// 该方法基于当前的状态和transition来尝试去改变FSM的状态,如果当前状态没有一个可以通过transition到达的状态,就会报错 /// </summary> /// <param name="trans"></param> public void PerformTransition(Transition trans) { //检验空状态 if (trans == Transition.NullTransition) { //打印错误信息 Debug.LogError($"FSM ERROR NUllTransition is not allowed for a real transition"); return; } //检查当前状态能不能通过transition到达其他状态 StateID id = currentState.GetOutputStateID(trans); if (id == StateID.NullStateId) {//表示在currentState的dic中没有trans可以去 Debug.Log($"FSM ERROR State {currentStateID.ToString()} does not have a target state for transition {trans.ToString()}"); return; } //更新currentState和currentStateId currentStateID = id; foreach (var state in states) { if (state.ID == currentStateID) //如果在states中找到了我们要转向的状态 { currentState.DoBeforeLeaving(); //当前状态更新前的处理 currentState = state; //更新到该状态 currentState.DoBeforeEntering(); //当前状态进入前的处理 break; } } }
/// <summary> /// 执行转换条件 /// </summary> /// <param name="trans"></param> public void PerformTransition(Transition trans) { if (trans == Transition.NullTRansition) { Debug.LogError("无法执行空的转换条件"); return; } StateID id = currentState.GetOutputState(trans); Debug.Log(id); if (id == StateID.NullStateID) { Debug.LogWarning("当前状态" + currentState + "无法发生条件转换" + trans); return; } if (states.ContainsKey(id) == false) { Debug.LogError("在状态机里不存在状态" + id); } FSMState state = states[id]; currentState.DoAfterLeaving(); currentState = state; currentStateID = state.ID; currentState.DoBeforeEntering(); }
//添加状态到系统 public void AddState(FSMState s) { if (s == null) { Debug.LogError("FSM ERROR: Null reference is not allowed"); return; } //首次添加状态时,设置系统的初始数据, //执行状态的DoBeforeEntering方法 if (states.Count == 0) { states.Add(s); currentState = s; currentStateID = s.ID; currentState.DoBeforeEntering(); return; } if (states.Any(state => state.ID == s.ID)) { Debug.LogError("FSM ERROR: Impossible to add state " + s.ID + " because state has already been added"); return; } states.Add(s); }
// next method tries to chage the state the fsm is in based on the current state //and the transition passed in. If the current state doesnt have a target state for the transition passed, // prints an error message public void PerformTransition(Transition trans) { if (trans == Transition.NullTransition) { Debug.LogError("FSM error, NullTransition is not allowed for a real transition"); return; } // look at current state // see if it has the transition passed as an argument StateID id = currentState.GetOutputState(trans); // using method created above if (id == StateID.NullStateID) { Debug.LogError("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " + " for transition " + trans.ToString()); return; } // update the currentStateID and the currentState currentStateID = id; foreach (FSMState state in states) { if (state.ID == currentStateID) { // post processes to do to the state before setting a new one currentState.DoBeforeLeaving(); currentState = state; // reset the state to its condition before is can be enacted (reasoning steps included) currentState.DoBeforeEntering(); break; } } // performTranition() } // class FSMSystem()
public void PerformTransition(Transition trans) { if (trans == Transition.NullTransition) { // Debug.LogError("trans is NullTransition"); return; } StateID id = currentState.GetOutputState(trans); if (id == StateID.NullStateID) { // Debug.LogError("id is nullStateID"); return; } currentStateID = id; foreach (FSMState state in states) { if (state.ID == currentStateID) { currentState.DoBeforeLeaving(); currentState = state; currentState.DoBeforeEntering(); break; } } }
/// <summary> /// 通过转换,改变FSM的状态 /// </summary> public void PerformTransition(Transition trans) { if (trans == Transition.NullTransition) { Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition"); return; } //获取转换对应的状态ID StateID id = CurrentState.GetOutputState(trans); if (id == StateID.NullStateID) { Debug.LogError("FSM ERROR: State " + CurrentStateID.ToString() + " does not have a target state " + " for transition " + trans.ToString()); return; } // 更新当前状态ID,currentStateID CurrentStateID = id; foreach (FSMState state in m_States) { if (state.ID == CurrentStateID) { // 离开状态时的变量重置 CurrentState.DoBeforeLeaving(); // 更新当前状态currentState CurrentState = state; // 进入状态前,设置进入的状态条件 CurrentState.DoBeforeEntering(); break; } } }
/// <summary> /// Tries to change the state of the FSM based on the current state /// and the given transition. /// If the current state does not have a target state for the passed transition, /// no transition will be performed. /// </summary> /// <param name="transition">Transition</param> public void PerformTransition(Transition transition) { // StateID of the desired transition. StateID id = currentState.GetOutputState(transition); if (transition == Transition.NullTransition) { Debug.LogError("FSMState: NullTransition is not allowed."); } else if (id == StateID.NullStateID) { Debug.LogError("FSMState: State " + currentStateID.ToString() + " does not have a target state for transition " + transition.ToString()); } else { // Change current state currentStateID = id; foreach (FSMState state in states) { if (state.ID == currentStateID) { // Call processing before leaving. currentState.DoBeforeLeaving(); //Change current state. currentState = state; //Call proceccing after entering. currentState.DoBeforeEntering(); break; } } } }
//执行转换 public void PerformTransition(Transition trans) { if (trans == Transition.NullTransition) { Debug.LogError("无法执行NullTransition"); } //得到StateID StateID id = currentState.GetOutputState(trans); if (id == StateID.NullStateID) { Debug.LogWarning("当前状态" + currentState + "无法根据" + trans + "状态发生转换"); } if (states.ContainsKey(id) == false) { Debug.LogError(id + "不存在"); } //根据StateID得到state FSMState state = states[id]; currentState.DoAfterLeaving(); //将当前状态设为state currentState = state; //将当前状态ID设为id currentStateID = id; currentState.DoBeforeEntering(); }
public void PerformTransition(FSMTransition trans) { if (trans == FSMTransition.NullTransition) { return; } FSMStateId id = currentState.GetOutputStateId(trans); if (id == FSMStateId.NullState) { return; } currentStateId = id; foreach (FSMState state in stateList) { if (state.StateID == id) { currentState.DoBeforeLeaving(); currentState = state; currentState.DoBeforeEntering(); break; } } }
public void PerformTransition(Transition trans) { string errorMsg = ""; if (trans == Transition.NullTransition) { errorMsg = "NullTransition is not allowed for a real transition."; FSMSystemError(errorMsg); return; } StateID id = currentState.GetOutputState(trans); if (id == StateID.NullStateID) { errorMsg = "State " + currentStateID.ToString() + " does not have a target state for transition " + trans.ToString(); FSMSystemError(errorMsg); return; } currentStateID = id; foreach (FSMState state in states) { if (state.ID == currentStateID) { currentState.DoBeforeLeaving(); currentState = state; currentState.DoBeforeEntering(); break; } } }
//执行状态转换 public void PerformTransition(Transition trans) { if (trans == Transition.NullTransition) { Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition"); return; } StateID id = currentState.GetOutputState(trans); if (id == StateID.NullStateID) { Debug.LogError("FSM ERROR: State " + currentStateID + " does not have a target state " + " for transition " + trans); return; } currentStateID = id; foreach (FSMState state in states) { if (state.ID != currentStateID) { continue; } currentState.DoBeforeLeaving();//转换前执行现有状态的DoBeforeLeaving方法 currentState = state; currentState.DoBeforeEntering();//转换完成,执行转换后状态的DoBeforeEntering方法 break; } }
public void PerformTransition(int _transition) { if (_transition == FSMState.NULL_TRANSITION) { return; } int stateID = m_currentState.GetOutputState(_transition); if (stateID == FSMState.NULL_STATE_ID) { return; } if (!m_states.ContainsKey(stateID)) { return; } FSMState state = m_states[stateID]; m_currentState.DoAfterLeaving(); m_currentState = state; m_currentStateID = stateID; m_currentState.DoBeforeEntering(); }
/// <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 DoTransition(FSMState targetState) { _currentState.DoBeforeLeaving(); _currentState = targetState; _owner.UpdateStateView(_currentState); _currentState.DoBeforeEntering(); }
/// <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(Transition trans) { // Check for NullTransition before changing the current state if (trans == Transition.NullTransition) { Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition"); return; } // Check if the currentState has the transition passed as argument StateID id = currentState.GetOutputState(trans); if (id == StateID.NullStateID) { 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; foreach (FSMState 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; } } }
public void PerformTransition(Transition trans) { if (trans == Transition.NullTransition) { Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition"); return; } StateID id = currentState.GetOutputState(trans); if (id == StateID.NullState) { Debug.LogError("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " + " for transition " + trans.ToString()); return; } currentStateID = id; foreach (FSMState state in states) { if (state.ID == currentStateID) { currentState.DoBeforeLeaving(); currentState = state; currentState.DoBeforeEntering(); break; } } }
public void PerformTransition(Transition transition) { if (transition == Transition.NA) { Debug.LogWarning("(FSMSystem) NullTransition isn't allowed for a real transition."); return; } StateID id = CurrentState.GetOutputState(transition); if (id == StateID.NA) { Debug.LogWarning("(FSMSystem) State " + CurrentID.ToString() + " doesn't have a target state for transition " + transition.ToString()); return; } CurrentID = id; foreach (FSMState s in _states) { if (s.ID != CurrentID) { continue; } CurrentState.DoBeforeLeaving(); CurrentState = s; CurrentState.DoBeforeEntering(); break; } }
public void SetCurrentState(FSMState s) { currentState = s; currentStateID = s.ID; s.DoBeforeEntering(); }
} // PerformTransition() /// <summary> /// 设置默认状态 /// </summary> /// <param name="state"></param> public void SetDefaultState(FSMState state) { if (state != null) { currentState = state; currentStateID = state.ID; // 手动执行默认的第一个状态的进入 state.DoBeforeEntering(); } }
// for AI controled FSM transfer public void PerformAITransition(StateID NextStateID) { currentStateID = NextStateID; foreach (FSMState state in states) { if (state.ID == currentStateID) { currentState.DoBeforeLeaving(); currentState = state; currentState.DoBeforeEntering(); break; } } }
public override void DoBeforeEntering() { currentStateID = InitialState; foreach (FSMState state in states) { if (state.ID == currentStateID) { currentState = state; // Reset the state to its desired condition before it can reason or act currentState.DoBeforeEntering(); currentState.enabled = true; break; } } }
/// <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(FSMState 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); if(InitialState == s.ID){ currentState = s; currentStateID = s.ID; currentState.DoBeforeEntering(); currentState.enabled = true; }else{ s.enabled = false; } return; } // Add the state to the List if it's not inside it foreach (FSMState state in states) { if (state.ID == s.ID) { Debug.LogError("FSM ERROR: Impossible to add state " + s.ID.ToString() + " because state has already been added"); return; } } states.Add(s); if(InitialState == s.ID){ currentState = s; currentStateID = s.ID; currentState.DoBeforeEntering(); s.enabled = true; }else{ s.enabled = false; } }
public void Init(Type[] types, T holder) { for (int i = 0; i < types.Length; i++) { if (!types[i].IsSubclassOf(typeof(FSMState <T>))) { Debug.LogError("Could not create FSM beacause types in constructor not allowed."); return; } var obj = Activator.CreateInstance(types[i], new object[] { this, holder }); statesList.Add(types[i], obj as FSMState <T>); } currentState = statesList[types[0]]; currentState.DoBeforeEntering(); }
/// <summary> /// 执行转换 /// </summary> /// <param name="trans">转换条件</param> public void PerformTransition(Transition trans) { if (trans == Transition.NullTransition) { Debug.LogError(trans + "为空"); return; } StateID targetID = currentState.GetTargetStateID(trans); if (states.ContainsKey(targetID) == false) { Debug.LogError(targetID + "不存在"); return; } FSMState targetState = states[targetID]; currentState.DoAfterLeaving(); targetState.DoBeforeEntering(); currentState = targetState; }
} // PerformTransition() public void PerformState(FSMState state) { currentStateID = state.ID; foreach (FSMState _state in states) { if (_state.ID == currentStateID) { // Do the post processing of the state before setting the new one currentState.DoBeforeLeaving(); //attr.StopCoroutine(currentState.DoLogic()); //currentState.RunLogic(); currentState = state; // Reset the state to its desired condition before it can reason or act currentState.DoBeforeEntering(); attr.StartCoroutine(currentState.DoLogic()); break; } } }
public void ChangeState(Type newState) { if (!newState.IsSubclassOf(typeof(FSMState <T>))) { Debug.LogError("Trying to transition to incorrect type state"); return; } if (statesList.ContainsKey(newState)) { currentState.DoBeforeLeave(); currentState = statesList[newState]; currentState.DoBeforeEntering(); } else { Debug.LogError("There is no state " + newState.ToString() + " in dictionary"); } }
/// <summary> /// 基于转换参数,改变现在的状态 /// </summary> /// <param name="trans"></param> public void PerformTransition(Transition trans) { // 根绝当前的状态类,以Trans为参数调用它的 GetOutputState 方法 StateID id = currentState.GetNextState(trans); // 更新现在的状态 currentStateID = id; FSMState fs; if (states.TryGetValue(currentStateID, out fs)) { currentState.DoBeforeLeaving(); currentState = fs; currentState.DoBeforeEntering(); } else { Debug.LogError("FSM CHANGE ERROR: The FsmSystem is not contain the FsmState."); return; } }
/// <summary> /// 该方法基于当前状态和过渡状态是否通过来尝试改变状态机的状态,当当前状态没有目标状态用来过渡时则打印错误信息 /// 切换当前状态到下一个要转换的状态 /// </summary> /// <param name="trans"></param> public void ChangeState(int id) { if (!states.ContainsKey(id)) { Debug.LogError("FSM error: State " + id + "not exist."); return; } //更新当前的状态机和状态编号 currentStateID = id; //在状态变为新状态前执行后处理 if (currentFSMState != null) { currentFSMState.DoBeforeLeaving(); } currentFSMState = states[id]; //在状态可以使用Reason(动机)或者Act(行为)之前为它的决定条件重置它自己 currentFSMState.DoBeforeEntering(); }
//控制状态之间的转换 public void PreformTransition(Transition trans) { if (trans == Transition.NullTransition) { Debug.LogError("NullTransition is not allowed for real transition "); return; } StateID id = currentState.GetOutputState(trans); if (id == StateID.NullStateID) { Debug.Log("Transition is not to be happened!没有符合条件的转换"); return; } FSMState state; states.TryGetValue(id, out state); currentState.DoBeforeLeaving(); currentState = state; currentState.DoBeforeEntering(); }
public void PerformTransition(FSMTransition trans) { if (trans == FSMTransition.NullTransition) { return; } FSMStateId id = currentState.GetOutputStateId(trans); if (id == FSMStateId.NullState) { return; } currentStateId = id; foreach(FSMState state in stateList) { if(state.StateID == id) { currentState.DoBeforeLeaving(); currentState = state; currentState.DoBeforeEntering(); break; } } }
/// <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(Transition trans) { // Check for NullTransition before changing the current state if (trans == Transition.NullTransition) { Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition"); return; } // Check if the currentState has the transition passed as argument StateID id = currentState.GetOutputState(trans); if (id == StateID.NullStateID) { 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; foreach (FSMState 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()
/// <summary> /// Tries to change the state of the FSM based on the current state /// and the given transition. /// If the current state does not have a target state for the passed transition, /// no transition will be performed. /// </summary> /// <param name="transition">Transition</param> public void PerformTransition(Transition transition) { // StateID of the desired transition. StateID id = currentState.GetOutputState(transition); if (transition == Transition.NullTransition) Debug.LogError("FSMState: NullTransition is not allowed."); else if (id == StateID.NullStateID) Debug.LogError("FSMState: State " + currentStateID.ToString() + " does not have a target state for transition " + transition.ToString()); else { // Change current state currentStateID = id; foreach (FSMState state in states) { if (state.ID == currentStateID) { // Call processing before leaving. currentState.DoBeforeLeaving(); //Change current state. currentState = state; //Call proceccing after entering. currentState.DoBeforeEntering(); break; } } } }
public void PerformTransition(Transition trans) { if (trans == Transition.NullTransition) { Debug.Log("Null Transition not allowed"); return; } StateID id = currentState.GetOutputState(trans); if (id == StateID.NullState) { Debug.Log("Transition to Null State not allowed"); return; } currentStateID = id; foreach (FSMState state in states) { // Debug.Log(state.ID.ToString() + " " + currentStateID.ToString()); if (state.ID == currentStateID) { Debug.Log("Performing Transition to: " + state.ID.ToString()); currentState.DoBeforeLeaving(); currentState = state; currentState.DoBeforeEntering(); return; } } Debug.Log("Transition Error: Output State does not exist"); return; }
public void PerformTransition(Transition trans) { if(trans == Transition.NullTransition) { Debug.LogError("FSM Error: NullTransition is not allowed for a real transition"); return; } StateID id = currentState.GetOutputState(trans); if(id == StateID.NullStateID) { Debug.LogError("FSM Error: State " + currentStateID.ToString() + " does not have a target state for transition " + trans.ToString()); return; } currentStateID = id; foreach(FSMState state in states) { if(state.ID == currentStateID) { currentState.DoBeforeLeaving(); currentState = state; currentState.DoBeforeEntering(); break; } } }
public void PerformTransition(Transition trans) { StateID id = currentState.GetNextState (trans); currentStateID = id; foreach (FSMState state in states) { if(state.ID == currentStateID) { currentState.DoBeforeExiting(); currentState = state; currentState.DoBeforeEntering(); break; } } }
// next method tries to chage the state the fsm is in based on the current state //and the transition passed in. If the current state doesnt have a target state for the transition passed, // prints an error message public void PerformTransition(Transition trans) { if (trans == Transition.NullTransition) { Debug.LogError("FSM error, NullTransition is not allowed for a real transition"); return; } // look at current state // see if it has the transition passed as an argument StateID id = currentState.GetOutputState(trans); // using method created above if (id == StateID.NullStateID) { Debug.LogError("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " + " for transition " + trans.ToString()); return; } // update the currentStateID and the currentState currentStateID = id; foreach (FSMState state in states) { if (state.ID == currentStateID) { // post processes to do to the state before setting a new one currentState.DoBeforeLeaving(); currentState = state; // reset the state to its condition before is can be enacted (reasoning steps included) currentState.DoBeforeEntering(); break; } } // performTranition() }