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.

149 lines
6.7KB

  1. using System.Runtime.CompilerServices;
  2. using Svelto.Common;
  3. using Svelto.DataStructures;
  4. using Svelto.ECS.Internal;
  5. namespace Svelto.ECS
  6. {
  7. public partial class EnginesRoot
  8. {
  9. class GenericEntityFunctions : IEntityFunctions
  10. {
  11. internal GenericEntityFunctions(EnginesRoot weakReference)
  12. {
  13. _enginesRoot = new WeakReference<EnginesRoot>(weakReference);
  14. }
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public void RemoveEntity<T>
  17. (uint entityID, ExclusiveBuildGroup groupID, [CallerMemberName] string caller = null)
  18. where T : IEntityDescriptor, new()
  19. {
  20. RemoveEntity<T>(new EGID(entityID, groupID), caller);
  21. }
  22. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  23. public void RemoveEntity<T>(EGID entityEGID, [CallerMemberName] string caller = null)
  24. where T : IEntityDescriptor, new()
  25. {
  26. DBC.ECS.Check.Require(entityEGID.groupID.isInvalid == false, "invalid group detected");
  27. _enginesRoot.Target.CheckRemoveEntityID(entityEGID, TypeCache<T>.type, caller);
  28. _enginesRoot.Target.QueueRemoveEntityOperation(
  29. entityEGID, _enginesRoot.Target.FindRealComponents<T>(entityEGID), caller);
  30. }
  31. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  32. public void RemoveEntitiesFromGroup(ExclusiveBuildGroup groupID, [CallerMemberName] string caller = null)
  33. {
  34. DBC.ECS.Check.Require(groupID.isInvalid == false, "invalid group detected");
  35. _enginesRoot.Target.RemoveGroupID(groupID);
  36. _enginesRoot.Target.QueueRemoveGroupOperation(groupID, caller);
  37. }
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public void SwapEntitiesInGroup
  40. (ExclusiveBuildGroup fromGroupID, ExclusiveBuildGroup toGroupID, [CallerMemberName] string caller = null)
  41. {
  42. if (_enginesRoot.Target._groupEntityComponentsDB.TryGetValue(
  43. fromGroupID.group
  44. , out FasterDictionary<ComponentID, ITypeSafeDictionary> entitiesInGroupPerType) == true)
  45. {
  46. #if DEBUG && !PROFILE_SVELTO
  47. ITypeSafeDictionary dictionary = entitiesInGroupPerType.unsafeValues[0];
  48. dictionary.KeysEvaluator((key) =>
  49. {
  50. _enginesRoot.Target.CheckSwapEntityID(new EGID(key, fromGroupID), new EGID(key, toGroupID), null, caller);
  51. });
  52. #endif
  53. _enginesRoot.Target.QueueSwapGroupOperation(fromGroupID, toGroupID, caller);
  54. }
  55. }
  56. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  57. public void SwapEntityGroup<T>
  58. (uint entityID, ExclusiveBuildGroup fromGroupID, ExclusiveBuildGroup toGroupID
  59. , [CallerMemberName] string caller = null) where T : IEntityDescriptor, new()
  60. {
  61. SwapEntityGroup<T>(new EGID(entityID, fromGroupID), toGroupID, caller);
  62. }
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. public void SwapEntityGroup<T>
  65. (EGID fromEGID, ExclusiveBuildGroup toGroupID, [CallerMemberName] string caller = null)
  66. where T : IEntityDescriptor, new()
  67. {
  68. SwapEntityGroup<T>(fromEGID, new EGID(fromEGID.entityID, toGroupID), caller);
  69. }
  70. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  71. public void SwapEntityGroup<T>
  72. (EGID fromEGID, EGID toEGID, ExclusiveBuildGroup mustBeFromGroup, [CallerMemberName] string caller = null)
  73. where T : IEntityDescriptor, new()
  74. {
  75. if (fromEGID.groupID != mustBeFromGroup)
  76. throw new ECSException(
  77. $"Entity is not coming from the expected group Expected {mustBeFromGroup} is {fromEGID.groupID}");
  78. SwapEntityGroup<T>(fromEGID, toEGID, caller);
  79. }
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public void SwapEntityGroup<T>(EGID fromEGID, EGID toEGID, [CallerMemberName] string caller = null)
  82. where T : IEntityDescriptor, new()
  83. {
  84. DBC.ECS.Check.Require(fromEGID.groupID.isInvalid == false, "invalid group detected");
  85. DBC.ECS.Check.Require(toEGID.groupID.isInvalid == false, "invalid group detected");
  86. var enginesRootTarget = _enginesRoot.Target;
  87. enginesRootTarget.CheckSwapEntityID(fromEGID, toEGID, TypeCache<T>.type, caller);
  88. enginesRootTarget.QueueSwapEntityOperation(fromEGID, toEGID
  89. , _enginesRoot.Target.FindRealComponents<T>(fromEGID)
  90. , caller);
  91. }
  92. #if UNITY_NATIVE
  93. public Native.NativeEntityRemove ToNativeRemove<T>(string memberName) where T : IEntityDescriptor, new()
  94. {
  95. return _enginesRoot.Target.ProvideNativeEntityRemoveQueue<T>(memberName);
  96. }
  97. public Native.NativeEntitySwap ToNativeSwap<T>(string memberName) where T : IEntityDescriptor, new()
  98. {
  99. return _enginesRoot.Target.ProvideNativeEntitySwapQueue<T>(memberName);
  100. }
  101. #endif
  102. //enginesRoot is a weakreference because GenericEntityStreamConsumerFactory can be injected inside
  103. //engines of other enginesRoot
  104. readonly WeakReference<EnginesRoot> _enginesRoot;
  105. }
  106. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  107. void QueueRemoveGroupOperation(ExclusiveBuildGroup groupID, string caller)
  108. {
  109. _entitiesOperations.QueueRemoveGroupOperation(groupID, caller);
  110. }
  111. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  112. void QueueSwapGroupOperation(ExclusiveBuildGroup fromGroupID, ExclusiveBuildGroup toGroupID, string caller)
  113. {
  114. _entitiesOperations.QueueSwapGroupOperation(fromGroupID, toGroupID, caller);
  115. }
  116. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  117. void QueueSwapEntityOperation(EGID fromID, EGID toID, IComponentBuilder[] componentBuilders, string caller)
  118. {
  119. _entitiesOperations.QueueSwapOperation(fromID, toID, componentBuilders, caller);
  120. }
  121. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  122. void QueueRemoveEntityOperation(EGID entityEGID, IComponentBuilder[] componentBuilders, string caller)
  123. {
  124. _entitiesOperations.QueueRemoveOperation(entityEGID, componentBuilders, caller);
  125. }
  126. }
  127. }