/// <summary> /// 这个方法用来在状态字典中删除transition-state对儿 /// 如果过渡并不存在于状态字典中,那么将会打印出一个错误 /// </summary> public void DeleteTransition(RoleTransition trans) { if (trans == RoleTransition.NullTransition) { Debug.LogError("FSMState ERROR: NullTransition is not allowed"); return; } //再删除之前确认该键值对是否存在于状态字典中(键值对集合) if (map.ContainsKey(trans)) { map.Remove(trans); return; } Debug.LogError("FSMState ERROR: Transition " + trans.ToString() + " passed to " + stateID.ToString() + " was not on the state's transition list"); }
/// <summary> /// 状态改变 /// </summary> public void SwitchTransition(RoleTransition trans, Entity entity) { //在改变当前状态前检测NullTransition if (trans == RoleTransition.NullTransition) { Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition"); return; } //在改变当前状态前检测当前状态是否可作为过渡的参数 RoleStateID id = CurrentState.GetOutputState(trans); if (id == RoleStateID.NullStateID) { Debug.LogError("FSM ERROR: State " + CurrentStateID.ToString() + " does not have a target state " + " for transition " + trans.ToString()); return; } if (id == CurrentStateID) { //Debug.LogError("FSM ERROR:Current State is same state " + CurrentStateID.ToString()); return; } //更新当前的状态个和状态编号 CurrentStateID = id; for (int i = 0; i < states.Count; i++) { if (states[i].ID == CurrentStateID) { //离开旧状态执行方法 CurrentState.OnExit(entity); CurrentState = states[i]; //进入新的状态 CurrentState.OnEnter(entity); break; } } }
public void AddTransition(RoleTransition trans, RoleStateID id) { //验证每个参数是否合法 if (trans == RoleTransition.NullTransition) { Debug.LogError("FSMState ERROR: NullTransition is not allowed for a real transition"); return; } if (id == RoleStateID.NullStateID) { Debug.LogError("FSMState ERROR: NullStateID is not allowed for a real ID"); return; } //要知道这是一个确定的有限状态机(每个状态后对应一种状态,而不能产生分支) //检查当前的过渡是否已经在字典中了 if (map.ContainsKey(trans)) { Debug.LogError("FSMState ERROR: State " + stateID.ToString() + " already has transition " + trans.ToString() + "Impossible to assign to another state"); return; } map.Add(trans, id); }