Mirror of Svelto.ECS because we're a fan of it
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

82 行
3.6KB

  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. namespace Svelto.ECS.Internal
  5. {
  6. static class EntityFactory
  7. {
  8. public static FasterDictionary<RefWrapperType, ITypeSafeDictionary> BuildGroupedEntities
  9. (EGID egid, EnginesRoot.DoubleBufferedEntitiesToAdd groupEntitiesToAdd
  10. , IComponentBuilder[] componentsToBuild, IEnumerable<object> implementors, Type implementorType)
  11. {
  12. var group = FetchEntityGroup(egid.groupID, groupEntitiesToAdd);
  13. BuildEntitiesAndAddToGroup(egid, group, componentsToBuild, implementors, implementorType);
  14. return group;
  15. }
  16. static FasterDictionary<RefWrapperType, ITypeSafeDictionary> FetchEntityGroup(ExclusiveGroupStruct groupID,
  17. EnginesRoot.DoubleBufferedEntitiesToAdd groupEntityComponentsByType)
  18. {
  19. if (groupEntityComponentsByType.current.TryGetValue(groupID, out var group) == false)
  20. {
  21. group = new FasterDictionary<RefWrapperType, ITypeSafeDictionary>();
  22. groupEntityComponentsByType.current.Add(groupID, group);
  23. }
  24. if (groupEntityComponentsByType.currentEntitiesCreatedPerGroup.TryGetValue(groupID, out var value) == false)
  25. groupEntityComponentsByType.currentEntitiesCreatedPerGroup[groupID] = 0;
  26. else
  27. groupEntityComponentsByType.currentEntitiesCreatedPerGroup[groupID] = value+1;
  28. return group;
  29. }
  30. static void BuildEntitiesAndAddToGroup
  31. (EGID entityID, FasterDictionary<RefWrapperType, ITypeSafeDictionary> @group
  32. , IComponentBuilder[] componentBuilders, IEnumerable<object> implementors, Type implementorType)
  33. {
  34. var count = componentBuilders.Length;
  35. #if DEBUG && !PROFILE_SVELTO
  36. HashSet<Type> types = new HashSet<Type>();
  37. for (var index = 0; index < count; ++index)
  38. {
  39. var entityComponentType = componentBuilders[index].GetEntityComponentType();
  40. if (types.Contains(entityComponentType))
  41. {
  42. throw new ECSException($"EntityBuilders must be unique inside an EntityDescriptor. Descriptor Type {implementorType} Component Type: {entityComponentType}");
  43. }
  44. types.Add(entityComponentType);
  45. }
  46. #endif
  47. for (var index = 0; index < count; ++index)
  48. {
  49. var entityComponentBuilder = componentBuilders[index];
  50. var entityComponentType = entityComponentBuilder.GetEntityComponentType();
  51. BuildEntity(entityID, @group, entityComponentType, entityComponentBuilder, implementors);
  52. }
  53. }
  54. static void BuildEntity(EGID entityID, FasterDictionary<RefWrapperType, ITypeSafeDictionary> group,
  55. Type entityComponentType, IComponentBuilder componentBuilder, IEnumerable<object> implementors)
  56. {
  57. var entityComponentsPoolWillBeCreated =
  58. group.TryGetValue(new RefWrapperType(entityComponentType), out var safeDictionary) == false;
  59. //passing the undefined entityComponentsByType inside the entityComponentBuilder will allow it to be created with the
  60. //correct type and casted back to the undefined list. that's how the list will be eventually of the target
  61. //type.
  62. componentBuilder.BuildEntityAndAddToList(ref safeDictionary, entityID, implementors);
  63. if (entityComponentsPoolWillBeCreated)
  64. group.Add(new RefWrapperType(entityComponentType), safeDictionary);
  65. }
  66. }
  67. }