コード例 #1
0
        public static World InitializeWorld(string worldName)
        {
            var world = new World(worldName);

            World.Active = world;
            // Entity manager must be first so that other things can find it.
            world.AddManager(new EntityManager());
            return(world);
        }
コード例 #2
0
        public static void InitializeSystems(World world)
        {
            var allSystemTypes = TypeManager.GetSystems();
            var allSystemNames = TypeManager.SystemNames;

            if (allSystemTypes.Length == 0)
            {
                throw new InvalidOperationException("DefaultTinyWorldInitialization: No Systems found.");
            }

            // Create top level presentation system and simulation systems.
            InitializationSystemGroup initializationSystemGroup = new InitializationSystemGroup();

            world.AddManager(initializationSystemGroup);

            SimulationSystemGroup simulationSystemGroup = new SimulationSystemGroup();

            world.AddManager(simulationSystemGroup);

            PresentationSystemGroup presentationSystemGroup = new PresentationSystemGroup();

            world.AddManager(presentationSystemGroup);

            // Create the working set of systems.
            int nSystems = 0;

            Type[]            systemTypes = new Type[allSystemTypes.Length];
            ComponentSystem[] systems     = new ComponentSystem[allSystemTypes.Length];

#if WRITE_LOG
            Console.WriteLine("--- Adding systems:");
#endif

            for (int i = 0; i < allSystemTypes.Length; i++)
            {
                if (TypeManager.GetSystemAttributes(allSystemTypes[i], typeof(DisableAutoCreationAttribute)).Length > 0)
                {
                    continue;
                }
                if (allSystemTypes[i] == initializationSystemGroup.GetType() ||
                    allSystemTypes[i] == simulationSystemGroup.GetType() ||
                    allSystemTypes[i] == presentationSystemGroup.GetType())
                {
                    continue;
                }

                if (world.GetExistingManager(allSystemTypes[i]) != null)
                {
                    continue;
                }
#if WRITE_LOG
                Console.WriteLine(allSystemNames[i]);
#endif
                systemTypes[nSystems] = allSystemTypes[i];
                systems[nSystems]     = TypeManager.ConstructSystem(allSystemTypes[i]);
                world.AddManager(systems[nSystems]);
                nSystems++;
            }
#if WRITE_LOG
            Console.WriteLine("--- Adding systems Done.");
#endif

            for (int i = 0; i < nSystems; ++i)
            {
                var sysType = systemTypes[i];
                var system  = systems[i];

                var groups = TypeManager.GetSystemAttributes(sysType, typeof(UpdateInGroupAttribute));
                if (groups.Length == 0)
                {
                    simulationSystemGroup.AddSystemToUpdateList(system);
                }

                for (int g = 0; g < groups.Length; ++g)
                {
                    var groupType   = groups[g] as UpdateInGroupAttribute;
                    var groupSystem = world.GetExistingManager(groupType.GroupType) as ComponentSystemGroup;
                    if (groupSystem == null)
                    {
                        throw new Exception("DefaultTinyWorldInitialization failed to find existing SystemGroup.");
                    }

                    groupSystem.AddSystemToUpdateList(system);
                }
            }
        }