/// <summary> /// 更新现有的状态 /// </summary> /// <param name="t_FSMStateName">要更新的现有状态的键索引</param> /// <param name="t_BaseFSMState">要替换的状态实例</param> public void UpdateState(string t_FSMStateName, BaseFSMState t_BaseFSMState) { if (IStateMapDataStructure.ContainsKey(t_FSMStateName)) { IStateMapDataStructure.Update(t_FSMStateName, t_BaseFSMState); } }
/// <summary> /// 添加状态 /// </summary> /// <param name="t_StateType">状态的抽象基类类型</param> public void AddState(BaseFSMState t_BaseFSMState) { if (null == t_BaseFSMState) { throw new GameBoxFrameworkException(string.Format("添加的状态不能为空!")); } if (!string.IsNullOrEmpty(t_BaseFSMState.StateName)) { if (IStateMapDataStructure.ContainsKey(t_BaseFSMState.StateName)) { throw new GameBoxFrameworkException(string.Format("状态'{0}'已经存在!", t_BaseFSMState.GetType().Name)); } IStateMapDataStructure.Add(t_BaseFSMState.StateName, t_BaseFSMState); } else { if (IStateMapDataStructure.ContainsKey(t_BaseFSMState.GetType().Name)) { throw new GameBoxFrameworkException(string.Format("状态'{0}'已经存在!", t_BaseFSMState.GetType().Name)); } IStateMapDataStructure.Add(t_BaseFSMState.GetType().Name, t_BaseFSMState); // 如果改状态没有名字,那么改状态就采用状态的类名 } t_BaseFSMState.OnStateInit(this); //初始化状态机 }
/// <summary> /// 添加状态 /// </summary> /// <typeparam name="T">状态的类型</typeparam> public void AddState <T>() where T : BaseFSMState { var t_State = Activator.CreateInstance(typeof(T)) as BaseFSMState; // 如果改状态没有名字,那么改状态就采用状态的类名 if (IStateMapDataStructure.ContainsKey(typeof(T).Name)) { throw new GameBoxFrameworkException(string.Format("状态'{0}'已经存在!", typeof(T).Name)); } IStateMapDataStructure.Add(typeof(T).Name, t_State); t_State.OnStateInit(this); //初始化状态机 }
/// <summary> /// 移除状态机条件 /// </summary> /// <param name="t_FSMName">状态机条件的类名</param> public void RemoveCondition(string t_ConditionName) { BaseFSMCondition value; IConditionMapDataStructure.TryGetValue(t_ConditionName, out value); if (!IStateMapDataStructure.ContainsKey(t_ConditionName)) { IStateMapDataStructure.Remove(t_ConditionName); } if (null != value) { value.OnConditionDestroy(this); } }
/// <summary> /// 移除状态 /// </summary> /// <param name="t_FSMName"></param> public void RemoveState(string t_FSMName) { BaseFSMState value; IStateMapDataStructure.TryGetValue(t_FSMName, out value); if (!IStateMapDataStructure.ContainsKey(t_FSMName)) { IStateMapDataStructure.Remove(t_FSMName); } if (null != value) { value.OnStateDestroy(this); } }
/// <summary> /// 添加状态 /// </summary> /// <param name="t_StateType">状态的抽象基类类型的Type类</param> public void AddState(Type t_StateType) { if (IStateMapDataStructure.ContainsKey(t_StateType.Name)) { throw new GameBoxFrameworkException(string.Format("状态'{0}'已经存在!", t_StateType.Name)); } AddState(Activator.CreateInstance(t_StateType) as BaseFSMState); //var t_Ctor = t_StateType.GetConstructors(); //if (0 < t_Ctor.Length) //有构造方法 //{ // AddState(Activator.CreateInstance(t_StateType) as BaseFSMState); //} //else //无默认构造方法 //{ // AddState(FormatterServices.GetSafeUninitializedObject(t_StateType) as BaseFSMState); //} }