/// <summary> /// 销毁所有的GameBox的组件 /// </summary> /// <typeparam name="T">GameBox的组件类型</typeparam> /// <returns>返回GamBox的组件</returns> public void DestroyComponents <T>() where T : IComponent { var t_ComponentArray = IListDataStructure.ToArray(); for (int i = 0; i < t_ComponentArray.Length; i++) { if (t_ComponentArray[i] is T) { IListDataStructure.RemoveNode(component => component.Equals(t_ComponentArray[i])); } } }
/// <summary> /// 模块管家销毁 /// </summary> public void Destroy() { var t_ModuleArray = IListDataStructure.ToArray(); for (int i = 0; i < t_ModuleArray.Length; i++) { if (null != t_ModuleArray[i]) { t_ModuleArray[i].OnDestroy(this); } } }
/// <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> /// 获取注册GameBox的组件数组 /// </summary> /// <typeparam name="T">GameBox的组件类型</typeparam> /// <returns>返回GamBox的组件</returns> public T[] GetComponents <T>() where T : IComponent { List <T> t_Components = new List <T>(); var t_ComponentArray = IListDataStructure.ToArray(); for (int i = 0; i < t_ComponentArray.Length; i++) { if (t_ComponentArray[i] is T) { t_Components.Add((T)t_ComponentArray[i]); } } IListDataStructure.Sort(); return(0 < t_Components.Count ? t_Components.ToArray():null); }
/// <summary> /// 获取GameBox的组件 /// </summary> /// <typeparam name="T">GameBox的组件类型</typeparam> /// <returns>返回GamBox的组件</returns> public T GetComponent <T>() where T : IComponent { var t_ComponentArray = IListDataStructure.ToArray(); for (int i = 0; i < t_ComponentArray.Length; i++) { if (t_ComponentArray[i] is T) { t_ComponentArray[i].Weight++; return((T)t_ComponentArray[i]); } } IListDataStructure.Sort(); return(default(T)); }
/// <summary> /// 销毁指定的GameBox的组件 /// </summary> /// <typeparam name="T">GameBox的组件类型</typeparam> /// <returns>返回GamBox的组件</returns> public void DestroyComponent(IComponent t_BaseGameBoxComponent) { if (!IListDataStructure.Contains(t_BaseGameBoxComponent)) { if (null != ComponentDestroyedEventHandler) { ComponentDestroyedEventHandler(this, new ComponentDestroyedEventArgs() { Component = t_BaseGameBoxComponent }); } IListDataStructure.RemoveNode(component => component.Equals(t_BaseGameBoxComponent)); } IListDataStructure.Sort(); }
/// <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); }
/// <summary> /// 初始化 IListDataStructure 接口的构造方法 /// </summary> /// <param name="t_IListDataStructure"></param> public GameBoxFrameworkModuleManager(IListDataStructure <BaseModule> t_IListDataStructure) : base(t_IListDataStructure) { }
/// <summary> /// 初始化 IListDataStructure 接口的构造方法 /// </summary> /// <param name="t_IListDataStructure"></param> public BaseModuleManager(IListDataStructure <BaseModule> t_IListDataStructure) { IListDataStructure = t_IListDataStructure; }
/// <summary> /// 初始化数据结构类型的构造方法 /// </summary> /// <param name="t_IListDataStructure"></param> public GameBoxComponentManager(IListDataStructure <IComponent> t_IListDataStructure) : base(t_IListDataStructure) { }
/// <summary> /// 初始化数据结构类型的构造方法 /// </summary> /// <param name="t_IListDataStructure"></param> public BaseComponentManager(IListDataStructure <IComponent> t_IListDataStructure) { IListDataStructure = t_IListDataStructure; }