public UnitSpawnerEngine(PrefabsDictionary prefabsDictionary, IEntityFactory entityFactory, HexGrid mapManager) { _prefabsDictionary = prefabsDictionary; _entityFactory = entityFactory; _mapManager = mapManager; var cells = _mapManager.GetCells(); for (int i = 0; i < 2; i++) { int random; do { random = Random.Range(0, cells.Count); } while (cells[random].IsUnderwater); var coord = cells[random].coordinates; var go = _prefabsDictionary.Istantiate("testUnit"); var implementors = new List <IImplementor>(); go.GetComponentsInChildren(implementors); implementors.Add(new UnitCoordinatesImplementor(coord.X, coord.Z)); _entityFactory.BuildEntityInGroup <PlayerEntityDescriptor>( go.GetInstanceID(), 1, implementors.ToArray()); BuildAbilities("FireBolt", go.GetInstanceID()); if (i == 1) { BuildAbilities("IcePunch", go.GetInstanceID()); } } }
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; }