Esempio n. 1
0
        /// <summary>
        /// 创建游戏模块
        /// </summary>
        /// <typeparam name="T">模块类</typeparam>
        /// <param name="createParam">创建参数</param>
        /// <param name="priority">运行时的优先级,优先级越大越早执行。如果没有设置优先级,那么会按照添加顺序执行</param>
        public static T CreateModule <T>(System.Object createParam, int priority = 0) where T : class, IModule
        {
            if (priority < 0)
            {
                throw new Exception("The priority can not be negative");
            }

            if (Contains(typeof(T)))
            {
                throw new Exception($"Game module {typeof(T)} is already existed");
            }

            // 如果没有设置优先级
            if (priority == 0)
            {
                int minPriority = GetMinPriority();
                priority = --minPriority;
            }

            MotionLog.Log($"Create game module : {typeof(T)}");
            T             module  = Activator.CreateInstance <T>();
            ModuleWrapper wrapper = new ModuleWrapper(module, priority);

            wrapper.Module.OnCreate(createParam);
            _coms.Add(wrapper);
            _isDirty = true;
            return(module);
        }
        /// <summary>
        /// 获取游戏模块
        /// </summary>
        /// <typeparam name="T">模块类</typeparam>
        public static T GetModule <T>() where T : class, IModule
        {
            System.Type type = typeof(T);
            for (int i = 0; i < _coms.Count; i++)
            {
                if (_coms[i].Module.GetType() == type)
                {
                    return(_coms[i].Module as T);
                }
            }

            MotionLog.Log(ELogLevel.Warning, $"Not found game module {type}");
            return(null);
        }