Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
 /// <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();
 }
Ejemplo n.º 3
0
        /// <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));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取一个事件主题
        /// </summary>
        /// <param name="t_EventTopicName">事件主题名字</param>
        /// <returns>返回事件主题接口</returns>
        public IEventTopic GetEventTopic(string t_EventTopicName)
        {
            var         t_EventTopicArray = IListDataStructure.ToArray();
            IEventTopic t_Target          = null;

            for (int i = 0; i < t_EventTopicArray.Length; i++)
            {
                if (t_EventTopicArray[i].EventTopicName == t_EventTopicName)
                {
                    t_Target = t_EventTopicArray[i];
                    t_EventTopicArray[i].Weight++; //权重自加
                    IListDataStructure.Sort();     //重新排序
                    break;
                }
            }
            return(t_Target);
        }
        /// <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);
        }