/// <summary> /// 创建状态机 /// </summary> /// <param name="t_FSMName">状态机的名字</param> /// <param name="t_FSMStates">状态机的状态</param> /// <returns>创建出来的状态机实例</returns> public IFSM CreateFSM(string t_FSMName, params FSMState[] t_FSMStates) { var t_FSM = new FSM(this, t_FSMName, t_FSMStates); t_FSM.OnInit(this); t_FSM.OnStart(this); IListDataStructure.AddNode(t_FSM); return(t_FSM); }
/// <summary> /// 创建一个事件主题 /// </summary> /// <param name="t_EventTopicName">事件主题名字</param> /// <returns>返回事件主题接口</returns> public IEventTopic CreateEventTopic(string t_EventTopicName, bool t_IsMainThread = true) { if (null != GetEventTopic(t_EventTopicName)) { throw new GameBoxFrameworkException("已经存在此事件主题!"); } var t_EventTopic = new EventTopic(t_EventTopicName, t_IsMainThread); IListDataStructure.AddNode(t_EventTopic); return(t_EventTopic); }
/// <summary> /// 注册GameBox的组件 /// </summary> /// <typeparam name="T">GameBox的组件类型</typeparam> /// <returns>返回GamBox的组件</returns> public void RegisterComponent(IComponent t_BaseGameBoxComponent) { if (!IListDataStructure.Contains(t_BaseGameBoxComponent)) { if (null != ComponentRegisteredEventHandler) { ComponentRegisteredEventHandler(this, new ComponentRegisteredEventArgs() { Component = t_BaseGameBoxComponent }); } IListDataStructure.AddNode(t_BaseGameBoxComponent); } }
/// <summary> /// 获取管理的模块 /// </summary> /// <typeparam name="T">模块的类型</typeparam> /// <returns>返回指定模块实例</returns> public T GetModule <T>() where T : class { Type t_InterfaceType = typeof(T); if (!t_InterfaceType.IsInterface) //如果不是接口 { throw new GameBoxFrameworkException(string.Format("你只能通过接口来获取系统内建模块,{0} 不是内建模块对应的接口.", t_InterfaceType.FullName)); } if (!t_InterfaceType.FullName.StartsWith("GameBoxFramework.")) //如果接口的命名空间开头不为内建系统命名空间 { throw new GameBoxFrameworkException(string.Format("{0} 不是系统内建的模块.", t_InterfaceType.FullName)); } var t_NamespaceSplits = t_InterfaceType.Namespace.Split('.'); string t_ModuleName = string.Format("{0}.{1}.{2}", "GameBoxFramework", t_NamespaceSplits[t_NamespaceSplits.Length - 1], t_InterfaceType.Name.Substring(1)); Type t_ModuleType = Type.GetType(t_ModuleName); if (t_ModuleType == null) //如果存在这个内建模块类型 { throw new GameBoxFrameworkException(string.Format("找不到系统内建模块 '{0}'.", t_ModuleName)); } var t_TargetModule = IListDataStructure.GetNode(module => null != module && module.GetType() == t_ModuleType); if (null == t_TargetModule) //还没有存在这个模块 { t_TargetModule = (BaseModule)Activator.CreateInstance(t_ModuleType); //创建该模块 if (t_TargetModule == null) { throw new GameBoxFrameworkException(string.Format("创建 '{0}' 模块失败.", t_ModuleType.GetType().FullName)); } else { IListDataStructure.AddNode(t_TargetModule); //添加进数据结构 t_TargetModule.OnInit(this); //初始化模块 t_TargetModule.OnStart(this); //启动模块 } } else //已经存在这个模块 { t_TargetModule.Weight++; //该模块权值刷新 IListDataStructure.Sort(); //重新排序模块,已确保权值高的模块优先被搜索到,提高效率 } //return (T)Convert.ChangeType(t_TargetModule,typeof(T)); //将 BaseModlue 转换成 模块对应的接口 return(t_TargetModule as T); }