Mirror of Svelto.ECS because we're a fan of it
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

80 行
3.1KB

  1. using System.Collections.Generic;
  2. using Svelto.DataStructures;
  3. namespace Svelto.ECS.Internal
  4. {
  5. static class EntityFactory
  6. {
  7. public static FasterDictionary<RefWrapperType, ITypeSafeDictionary> BuildGroupedEntities
  8. (EGID egid, EnginesRoot.DoubleBufferedEntitiesToAdd groupEntitiesToAdd, IComponentBuilder[] componentsToBuild
  9. , IEnumerable<object> implementors
  10. #if DEBUG && !PROFILE_SVELTO
  11. , System.Type descriptorType
  12. #endif
  13. )
  14. {
  15. var group = groupEntitiesToAdd.currentComponentsToAddPerGroup.GetOrAdd(
  16. egid.groupID, () => new FasterDictionary<RefWrapperType, ITypeSafeDictionary>());
  17. //track the number of entities created so far in the group.
  18. groupEntitiesToAdd.IncrementEntityCount(egid.groupID);
  19. BuildEntitiesAndAddToGroup(egid, group, componentsToBuild, implementors
  20. #if DEBUG && !PROFILE_SVELTO
  21. , descriptorType
  22. #endif
  23. );
  24. return group;
  25. }
  26. static void BuildEntitiesAndAddToGroup
  27. (EGID entityID, FasterDictionary<RefWrapperType, ITypeSafeDictionary> @group
  28. , IComponentBuilder[] componentBuilders, IEnumerable<object> implementors
  29. #if DEBUG && !PROFILE_SVELTO
  30. , System.Type descriptorType
  31. #endif
  32. )
  33. {
  34. #if DEBUG && !PROFILE_SVELTO
  35. DBC.ECS.Check.Require(componentBuilders != null, $"Invalid Entity Descriptor {descriptorType}");
  36. #endif
  37. var numberOfComponents = componentBuilders.Length;
  38. #if DEBUG && !PROFILE_SVELTO
  39. var types = new HashSet<System.Type>();
  40. for (var index = 0; index < numberOfComponents; ++index)
  41. {
  42. var entityComponentType = componentBuilders[index].GetEntityComponentType();
  43. if (types.Contains(entityComponentType))
  44. {
  45. throw new ECSException(
  46. $"EntityBuilders must be unique inside an EntityDescriptor. Descriptor Type {descriptorType} Component Type: {entityComponentType}");
  47. }
  48. types.Add(entityComponentType);
  49. }
  50. #endif
  51. for (var index = 0; index < numberOfComponents; ++index)
  52. {
  53. var entityComponentBuilder = componentBuilders[index];
  54. BuildEntity(entityID, @group, entityComponentBuilder, implementors);
  55. }
  56. }
  57. static void BuildEntity
  58. (EGID entityID, FasterDictionary<RefWrapperType, ITypeSafeDictionary> group
  59. , IComponentBuilder componentBuilder, IEnumerable<object> implementors)
  60. {
  61. var entityComponentType = componentBuilder.GetEntityComponentType();
  62. ITypeSafeDictionary safeDictionary = group.GetOrAdd(new RefWrapperType(entityComponentType)
  63. , (ref IComponentBuilder cb) => cb.CreateDictionary(1)
  64. , ref componentBuilder);
  65. // if the safeDictionary hasn't been created yet, it will be created inside this method.
  66. componentBuilder.BuildEntityAndAddToList(safeDictionary, entityID, implementors);
  67. }
  68. }
  69. }