Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EntityFactory.cs 3.1KB

Svelto.ECS 2.9 changes (random order of importance): New Serialization framework more thorough disposing of the EnginesRoot an EnginesRoot reference should never be held, unless it’s a weak reference. The code changed to stick to this rule IReactOnAddAndRemove callbacks are now guaranteed to be called after all the entity structs generated by the same entity have been added and before any is removed. both functions pass the EGID of the analysing entity by parameter now, so that the entity struct won’t need to implement INeedEGID for this sole purpose. The IReactOnSwap MovedFrom method has been removed, it is now redundant. Entities built or removed during the IReactOnAddAndRemove callbacks are now added and removed immediately and not on the next submission like used to happen. This avoid some awkward checks that were previously needed inside engines. EntityStreams can get (optionally) the EGID of the entity published, so that the EntityStruct won’t need an INeedEGID for this sole purpose. Groups are not trimmed anymore when they are emptied to avoid allocations. Removed a bunch of run-time allocations that weren’t supposed to happen in Release and/or when the Profile define is used in editor (for debugging reasons Svelto.ECS may need to use strings at run-time, but Svelto.ECS is allocation zero in Release and when the Profile keyword is used) A improved the DynamicEntityDescriptor and ExtendibleEntityDescriptor code and more notably, introduced the new method ExtendedWith<> to facilitate the writing of modular and reusable entity descriptors. Several minor code design improvements/optimisations
5 years ago
Svelto.ECS 2.9 changes (random order of importance): New Serialization framework more thorough disposing of the EnginesRoot an EnginesRoot reference should never be held, unless it’s a weak reference. The code changed to stick to this rule IReactOnAddAndRemove callbacks are now guaranteed to be called after all the entity structs generated by the same entity have been added and before any is removed. both functions pass the EGID of the analysing entity by parameter now, so that the entity struct won’t need to implement INeedEGID for this sole purpose. The IReactOnSwap MovedFrom method has been removed, it is now redundant. Entities built or removed during the IReactOnAddAndRemove callbacks are now added and removed immediately and not on the next submission like used to happen. This avoid some awkward checks that were previously needed inside engines. EntityStreams can get (optionally) the EGID of the entity published, so that the EntityStruct won’t need an INeedEGID for this sole purpose. Groups are not trimmed anymore when they are emptied to avoid allocations. Removed a bunch of run-time allocations that weren’t supposed to happen in Release and/or when the Profile define is used in editor (for debugging reasons Svelto.ECS may need to use strings at run-time, but Svelto.ECS is allocation zero in Release and when the Profile keyword is used) A improved the DynamicEntityDescriptor and ExtendibleEntityDescriptor code and more notably, introduced the new method ExtendedWith<> to facilitate the writing of modular and reusable entity descriptors. Several minor code design improvements/optimisations
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. }