using System; using System.Collections.Generic; using Svelto.DataStructures; namespace Svelto.ECS.Internal { static class EntityFactory { public static FasterDictionary BuildGroupedEntities (EGID egid, EnginesRoot.DoubleBufferedEntitiesToAdd groupEntitiesToAdd, IComponentBuilder[] componentsToBuild , IEnumerable implementors #if DEBUG && !PROFILE_SVELTO , System.Type descriptorType #endif ) { var group = groupEntitiesToAdd.currentComponentsToAddPerGroup.GetOrAdd( egid.groupID, () => new FasterDictionary()); //track the number of entities created so far in the group. groupEntitiesToAdd.IncrementEntityCount(egid.groupID); BuildEntitiesAndAddToGroup(egid, group, componentsToBuild, implementors #if DEBUG && !PROFILE_SVELTO , descriptorType #endif ); return group; } static void BuildEntitiesAndAddToGroup (EGID entityID, FasterDictionary @group , IComponentBuilder[] componentBuilders, IEnumerable implementors #if DEBUG && !PROFILE_SVELTO , System.Type descriptorType #endif ) { #if DEBUG && !PROFILE_SVELTO DBC.ECS.Check.Require(componentBuilders != null, $"Invalid Entity Descriptor {descriptorType}"); #endif var numberOfComponents = componentBuilders.Length; #if DEBUG && !PROFILE_SVELTO var types = new HashSet(); for (var index = 0; index < numberOfComponents; ++index) { var entityComponentType = componentBuilders[index].GetEntityComponentType(); if (types.Contains(entityComponentType)) { throw new ECSException( $"EntityBuilders must be unique inside an EntityDescriptor. Descriptor Type {descriptorType} Component Type: {entityComponentType}"); } types.Add(entityComponentType); } #endif for (var index = 0; index < numberOfComponents; ++index) { var entityComponentBuilder = componentBuilders[index]; BuildEntity(entityID, @group, entityComponentBuilder, implementors); } } static void BuildEntity(EGID entityID, FasterDictionary group, IComponentBuilder componentBuilder, IEnumerable implementors) { ITypeSafeDictionary safeDictionary = group.GetOrAdd(componentBuilder.getComponentID , (ref IComponentBuilder cb) => cb.CreateDictionary(1) , ref componentBuilder); // if the safeDictionary hasn't been created yet, it will be created inside this method. componentBuilder.BuildEntityAndAddToList(safeDictionary, entityID, implementors); } } }