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.

85 lines
4.1KB

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