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); }
public void EndCurrentStateToOtherState(RoleStateID id) { if (!IsAlive) { return; } if (RoleEntityControl.Fsm.CurrentStateID != id) { switch (id) { case RoleStateID.Idle: RoleEntityControl.SetTransition(RoleTransition.Idle, this); break; case RoleStateID.Walk: RoleEntityControl.SetTransition(RoleTransition.FreeWalk, this); break; case RoleStateID.Acct: RoleEntityControl.SetTransition(RoleTransition.Acct, this); break; case RoleStateID.Skill: RoleEntityControl.SetTransition(RoleTransition.Skill, this); break; case RoleStateID.Switch: RoleEntityControl.SetTransition(RoleTransition.Switch, this); break; case RoleStateID.CrashPlayer: RoleEntityControl.SetTransition(RoleTransition.CrashPlayer, this); break; case RoleStateID.Dead: ItemDropMgr.Instance.DropRareItem(CacheModel.position, Attribute.Score, GameMgr.Instance.ItemRoot); RoleEntityControl.SetTransition(RoleTransition.Dead, this); break; } } }
/// <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; } } }
/// <summary> /// 该方法删除一个已存在以状态几个中的状态 /// 在它不存在时打印错误信息 /// </summary> public void DeleteState(RoleStateID id) { //在删除前检查其是否为空状态 if (id == RoleStateID.NullStateID) { Debug.LogError("FSM ERROR: NullStateID is not allowed for a real state"); return; } //遍历集合如果存在该状态则删除它 for (int i = 0; i < states.Count; i++) { if (states[i].ID == id) { states.Remove(states[i]); return; } } Debug.LogError("FSM ERROR: Impossible to delete state " + id.ToString() + ". It was not on the list of states"); }