Esempio n. 1
0
        public SimpleContext()
        {
            //An EnginesRoot holds all the engines created. it needs a EntitySubmissionScheduler to know when to
            //add the EntityViews generated inside the EntityDB.
            var simpleSubmissionEntityViewScheduler = new SimpleSubmissionEntityViewScheduler();

            _enginesRoot = new EnginesRoot(simpleSubmissionEntityViewScheduler);

            //an EnginesRoot must never be injected inside other classes only IEntityFactory and IEntityFunctions
            //implementation can
            var entityFactory   = _enginesRoot.GenerateEntityFactory();
            var entityFunctions = _enginesRoot.GenerateEntityFunctions();

            //Add the Engine to manage the SimpleEntities
            var behaviourForEntityClassEngine = new BehaviourForEntityClassEngine(entityFunctions);

            _enginesRoot.AddEngine(behaviourForEntityClassEngine);

            //build Entity with ID 1 in group 0
            entityFactory.BuildEntity <SimpleEntityDescriptor>(new EGID(0, ExclusiveGroups.group0));

            //as we are using a basic scheduler, we need to schedule the entity submission ourselves
            simpleSubmissionEntityViewScheduler.SubmitEntities();
            //twice as we want the Swap that is executed after the first submission, to be executed too
            simpleSubmissionEntityViewScheduler.SubmitEntities();

            //as we don't have any ticking system for this basic example, we tick explicitly
            behaviourForEntityClassEngine.Update();

            Console.Log("Done - click any button to quit");

            System.Console.ReadKey();

            Environment.Exit(0);
        }
        public void Init()
        {
            _simpleSubmissionEntityViewScheduler = new SimpleSubmissionEntityViewScheduler();
            _enginesRoot = new EnginesRoot(_simpleSubmissionEntityViewScheduler);
            _neverDoThisIsJustForTheTest = new TestEngine();

            _enginesRoot.AddEngine(_neverDoThisIsJustForTheTest);

            _entityFactory   = _enginesRoot.GenerateEntityFactory();
            _entityFunctions = _enginesRoot.GenerateEntityFunctions();
        }
        IEnumerator Run()
        {
            #region comment
            //An EngineRoot holds all the engines created so far and is
            //responsible of the injection of the entity entityViews inside every
            //relative engine.
            //Every Composition Root can have one or more EnginesRoots. Spliting
            //EnginesRoots promote even more encapsulation than the ECS paradigm
            //itself already does
            #endregion

            var simpleSubmissionEntityViewScheduler = new SimpleSubmissionEntityViewScheduler();
            _enginesRoot = new EnginesRoot(simpleSubmissionEntityViewScheduler);

            #region comment
            //an EnginesRoot must never be injected inside other classes
            //only IEntityFactory and IEntityFunctions implementation can
            #endregion
            IEntityFactory   entityFactory   = _enginesRoot.GenerateEntityFactory();
            IEntityFunctions entityFunctions = _enginesRoot.GenerateEntityFunctions();

            //Add the Engine to manage the SimpleEntities
            _enginesRoot.AddEngine(new BehaviourForSimpleEntityEngine(entityFunctions));
            //Add the Engine to manage the SimpleStructEntities
            _enginesRoot.AddEngine(new BehaviourForSimpleEntityAsStructEngine());

            #region comment
            //the number of implementors to use to implement the Entity components is arbitrary and it depends
            //by the modularity/reusability of the implementors.
            //You may avoid to create new implementors if you create modular/reusable ones
            //The concept of implementor is pretty unique in Svelto.ECS
            //and enable very interesting features. Refer to my articles to understand
            //more about it.
            #endregion
            object[] implementors = new object[1];
            int      groupID      = 0;

            ProfileIt <SimpleEntityDescriptor>((entityID) =>
            {
                //every entity must be implemented with its own implementor obviously
                //(otherwise the instances will be shared between entities and
                //we don't want that, right? :) )
                implementors[0] = new SimpleImplementor("simpleEntity");

                //Build a SimpleEntity using specific implementors to implement the
                //Entity Components interfaces
                entityFactory.BuildEntity <SimpleEntityDescriptor>(entityID, implementors);
            }, entityFactory);

            #region comment
            //Entities as struct do not need an implementor. They are much more rigid
            //to use, but much faster. Please use them only when performance is really
            //critical (most of the time is not)
            #endregion
            ProfileIt <SimpleStructEntityDescriptor>((entityID) =>
            {
                //Build a SimpleStructEntity inside the group groupID
                entityFactory.BuildEntityInGroup <SimpleStructEntityDescriptor>(entityID, groupID);
            }, entityFactory);

            implementors[0] = new SimpleImplementor(groupID);


            //Build and BuildEntityInGroup can be used either with Entity defined by implementors
            //and/or by structs
            entityFactory.BuildEntityInGroup <SimpleGroupedEntityDescriptor>(0, groupID, implementors);

            #region comment
            //quick way to submit entities, this is not the standard way, but if you
            //create a custom EntitySubmissionScheduler is up to you to decide
            //when the EntityViews are submited to the engines and DB.
            #endregion
            simpleSubmissionEntityViewScheduler.SubmitEntities();

            Utility.Console.Log("Done - click any button to quit");

            Console.ReadKey();

            Environment.Exit(0);

            yield break;
        }
        public SimpleContext()
        {
            #region comment
            //An EnginesRoot holds all the engines created.
            //it needs a EntitySubmissionScheduler to know when to
            //add the EntityViews generated inside the EntityDB.
            #endregion

            var simpleSubmissionEntityViewScheduler = new SimpleSubmissionEntityViewScheduler();
            _enginesRoot = new EnginesRoot(simpleSubmissionEntityViewScheduler);

            #region comment
            //an EnginesRoot must never be injected inside other classes
            //only IEntityFactory and IEntityFunctions implementation can
            #endregion

            var entityFactory   = _enginesRoot.GenerateEntityFactory();
            var entityFunctions = _enginesRoot.GenerateEntityFunctions();

            //Add the Engine to manage the SimpleEntities
            _enginesRoot.AddEngine(new BehaviourForEntityClassEngine(entityFunctions));
            //Add the Engine to manage the SimpleStructEntities
            _enginesRoot.AddEngine(new EntityStructEngine());

            #region comment

            //the number of implementors to use to implement the Entity components is arbitrary and it depends
            //by the modularity/reusability of the implementors.
            //You may avoid to create new implementors if you create modular/reusable ones
            //The concept of implementor is pretty unique in Svelto.ECS
            //and enable very interesting features. Refer to my articles to understand
            //more about it.
            //every entity component must be implemented with through the entity implementor
            //Entity can be built grouped. Grouped entities have more applications and can be
            //used for more focused queried. In this case the entityID and the groupID must
            //be specified.
            //Entity ID must be globally unique regardless if they are grouped or not.
            #endregion

            //build Entity with ID 1
            entityFactory.BuildEntity <SimpleEntityDescriptor>(1, new[] { new EntityImplementor("simpleEntity", false) });
            //build Entity with ID 0 in group 0
            entityFactory.BuildEntityInGroup <SimpleEntityDescriptor>(0, 0, new[] { new EntityImplementor("simpleGroupedEntity", true) });

            #region comment
            //Entities as struct do not need an implementor. They are much more rigid
            //to use, but much faster. Please use them only when performance is really
            //critical (most of the time is not)
            //build entitystruct with ID 2 in group 0. Entities are
            //separated by their type, but belong to the same group
            //if they have the same groupID.
            #endregion
            entityFactory.BuildEntityInGroup <SimpleEntityStructDescriptor>(2, 0, null);

            simpleSubmissionEntityViewScheduler.SubmitEntities();

            Console.Log("Done - click any button to quit");

            System.Console.ReadKey();

            Environment.Exit(0);
        }