public void AddState(FsmState new_state) { // 安全检测 if (new_state == null) { LogManager.Error("FSM ERROR: 新增加的状态为null"); return; } // 第一个状态做为默认的初始状态 if (states.Count == 0) { states.Add(new_state); _current_state = new_state; _current_state_id = new_state.Id; return; } // 检测状态是否重复 int length = states.Count; for (int i = 0; i < length; i++) { if (states[i].Id == new_state.Id) { LogManager.Error("FSM ERROR: 添加状态[{0}]失败, 因为状态已经存在了Index:[{1}]", new_state.Id, i); return; } } states.Add(new_state); }
/// <summary> /// 内部触发事件,改变人物状态 /// </summary> public static void ChangeInEntityState(I_EntityInTrigger entity, E_StateId state, bool force = false) { ChangeEntityStateEventData data = EventDataFactory.Pop <ChangeEntityStateEventData>(); data.state_id = state; data.force = force; entity.RaiseEvent(E_EntityInTrigger.CHANGE_STATE, data); }
public static void AddState <T>(BaseEntity entity, FsmSystem system, E_StateId stateId) where T : FsmState, new() { T t = new T(); t.entity = entity; system.AddState(t); }
public void DeleteState(E_StateId id) { // 遍历状态列表,如果存在则移除 int length = states.Count; for (int i = 0; i < length; i++) { if (states[i].Id == id) { states.Remove(states[i]); return; } } LogManager.Error("删除错误: 删除状态[{0}]失败,因为不存在于列表中 ", id); }
/// <summary> /// 根据状态进行转换 /// </summary> public void PerformTransition(E_StateId id) { if (id == _current_state_id) { return; } _current_state_id = id; int length = states.Count; for (int i = 0; i < length; i++) { if (states[i].Id == _current_state_id) { // 在设置新的状态之前进行状态的离开处理 _current_state.DoBeforeLeaving(); // 改变状态 _current_state = states[i]; // 设置新状态之后,进行状态进入处理 _current_state.DoBeforeEntering(); break; } } }