Exemplo n.º 1
0
        /// <summary>
        /// Registers system by its type and performs all necessary initialization routine
        /// </summary>
        public void RegisterSystem(Type type)
        {
            if (!typeof(BaseComponentSystem).IsAssignableFrom(type))
            {
                throw new ArgumentException($"EC System of type {type.Name} is not inherited from {nameof(BaseComponentSystem)}");
            }

            BaseComponentSystem system = (BaseComponentSystem)Activator.CreateInstance(type);

            system.PreInitialize(this);

            if (system.SystemType == SystemProcessingType.Draw)
            {
                _service.DrawSystems.Add(system);
                _service.DrawSystems.Sort((system1, system2) => system1.Order - system2.Order);
            }
            else if (system.SystemType == SystemProcessingType.Update)
            {
                _service.UpdateSystems.Add(system);
                _service.UpdateSystems.Sort((system1, system2) => system1.Order - system2.Order);
            }

            system.Initialize();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Performs initialization of all scanned systems and templates
        /// </summary>
        internal void Initialize(IEnumerable <Type> availableSystems, IEnumerable <Type> availableTemplates)
        {
            // processing templates
            foreach (Type availableTemplateType in availableTemplates)
            {
                EntityTemplate template = null;
                try
                {
                    template = (EntityTemplate)Activator.CreateInstance(availableTemplateType);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, $"Unable to create template '{availableTemplateType.Name}'");
                    continue;
                }

                template.InitializeInternal();

                _service.Templates[availableTemplateType] = template;
                _service.TemplatesByName[template.Name]   = template;
            }

            foreach (Type availableSystem in availableSystems)
            {
                BaseComponentSystem system = null;
                try
                {
                    system = (BaseComponentSystem)Activator.CreateInstance(availableSystem);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, $"Unable to create system '{availableSystem.Name}'");
                    continue;
                }

                system.PreInitialize(this);

                if (system.SystemType == SystemProcessingType.Draw)
                {
                    _service.DrawSystems.Add(system);
                }
                else if (system.SystemType == SystemProcessingType.Update)
                {
                    _service.UpdateSystems.Add(system);
                }
            }

            // performing ordering of systems and initializing
            _service.UpdateSystems.Sort((system1, system2) => system1.Order - system2.Order);
            _service.DrawSystems.Sort((system1, system2) => system1.Order - system2.Order);

            foreach (BaseComponentSystem system in _service.UpdateSystems)
            {
                system.Initialize();
            }

            foreach (BaseComponentSystem system in _service.DrawSystems)
            {
                system.Initialize();
            }
        }