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.

EnginesRoot.GenericEntityFactory.cs 3.6KB

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
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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using Svelto.Common;
  5. namespace Svelto.ECS
  6. {
  7. public partial class EnginesRoot
  8. {
  9. class GenericEntityFactory : IEntityFactory
  10. {
  11. public GenericEntityFactory(EnginesRoot weakReference)
  12. {
  13. _enginesRoot = new Svelto.DataStructures.WeakReference<EnginesRoot>(weakReference);
  14. }
  15. public EntityInitializer BuildEntity<T>
  16. (uint entityID, ExclusiveBuildGroup groupStructId, IEnumerable<object> implementors = null
  17. , [CallerMemberName] string caller = null) where T : IEntityDescriptor, new()
  18. {
  19. return _enginesRoot.Target.BuildEntity(new EGID(entityID, groupStructId)
  20. , EntityDescriptorTemplate<T>.descriptor.componentsToBuild
  21. , TypeCache<T>.type, implementors, caller);
  22. }
  23. public EntityInitializer BuildEntity<T>
  24. (EGID egid, IEnumerable<object> implementors = null
  25. , [CallerMemberName] string caller = null) where T : IEntityDescriptor, new()
  26. {
  27. return _enginesRoot.Target.BuildEntity(egid, EntityDescriptorTemplate<T>.descriptor.componentsToBuild
  28. , TypeCache<T>.type, implementors, caller);
  29. }
  30. public EntityInitializer BuildEntity<T>
  31. (EGID egid, T entityDescriptor, IEnumerable<object> implementors
  32. , [CallerMemberName] string caller = null) where T : IEntityDescriptor
  33. {
  34. return _enginesRoot.Target.BuildEntity(egid, entityDescriptor.componentsToBuild, TypeCache<T>.type
  35. , implementors, caller);
  36. }
  37. public EntityInitializer BuildEntity<T>
  38. (uint entityID, ExclusiveBuildGroup groupStructId, T descriptorEntity, IEnumerable<object> implementors
  39. , [CallerMemberName] string caller = null) where T : IEntityDescriptor
  40. {
  41. return _enginesRoot.Target.BuildEntity(new EGID(entityID, groupStructId)
  42. , descriptorEntity.componentsToBuild, TypeCache<T>.type
  43. , implementors, caller);
  44. }
  45. public void PreallocateEntitySpace<T>(ExclusiveGroupStruct groupStructId, uint numberOfEntities)
  46. where T : IEntityDescriptor, new()
  47. {
  48. _enginesRoot.Target.Preallocate(groupStructId, numberOfEntities
  49. , EntityDescriptorTemplate<T>.descriptor.componentsToBuild);
  50. }
  51. public EntityInitializer BuildEntity
  52. (EGID egid, IComponentBuilder[] componentsToBuild, Type type, IEnumerable<object> implementors = null
  53. , [CallerMemberName] string caller = null)
  54. {
  55. return _enginesRoot.Target.BuildEntity(egid, componentsToBuild, type, implementors, caller);
  56. }
  57. #if UNITY_NATIVE
  58. public Native.NativeEntityFactory ToNative<T>
  59. ([CallerMemberName] string caller = null)
  60. where T : IEntityDescriptor, new()
  61. {
  62. return _enginesRoot.Target.ProvideNativeEntityFactoryQueue<T>(caller);
  63. }
  64. #endif
  65. //enginesRoot is a weakreference because GenericEntityStreamConsumerFactory can be injected inside
  66. //engines of other enginesRoot
  67. readonly Svelto.DataStructures.WeakReference<EnginesRoot> _enginesRoot;
  68. }
  69. }
  70. }