Mirror of Svelto.ECS because we're a fan of it
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

152 lignes
6.9KB

  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.CheckRemoveEntityID(new EGID(key, fromGroupID), null, caller);
  51. _enginesRoot.Target.CheckAddEntityID(new EGID(key, toGroupID), null, caller);
  52. });
  53. #endif
  54. _enginesRoot.Target.QueueSwapGroupOperation(fromGroupID, toGroupID, caller);
  55. }
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public void SwapEntityGroup<T>
  59. (uint entityID, ExclusiveBuildGroup fromGroupID, ExclusiveBuildGroup toGroupID
  60. , [CallerMemberName] string caller = null) where T : IEntityDescriptor, new()
  61. {
  62. SwapEntityGroup<T>(new EGID(entityID, fromGroupID), toGroupID, caller);
  63. }
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public void SwapEntityGroup<T>
  66. (EGID fromEGID, ExclusiveBuildGroup toGroupID, [CallerMemberName] string caller = null)
  67. where T : IEntityDescriptor, new()
  68. {
  69. SwapEntityGroup<T>(fromEGID, new EGID(fromEGID.entityID, toGroupID), caller);
  70. }
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. public void SwapEntityGroup<T>
  73. (EGID fromEGID, EGID toEGID, ExclusiveBuildGroup mustBeFromGroup, [CallerMemberName] string caller = null)
  74. where T : IEntityDescriptor, new()
  75. {
  76. if (fromEGID.groupID != mustBeFromGroup)
  77. throw new ECSException(
  78. $"Entity is not coming from the expected group Expected {mustBeFromGroup} is {fromEGID.groupID}");
  79. SwapEntityGroup<T>(fromEGID, toEGID, caller);
  80. }
  81. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82. public void SwapEntityGroup<T>(EGID fromEGID, EGID toEGID, [CallerMemberName] string caller = null)
  83. where T : IEntityDescriptor, new()
  84. {
  85. DBC.ECS.Check.Require(fromEGID.groupID.isInvalid == false, "invalid group detected");
  86. DBC.ECS.Check.Require(toEGID.groupID.isInvalid == false, "invalid group detected");
  87. var enginesRootTarget = _enginesRoot.Target;
  88. enginesRootTarget.CheckRemoveEntityID(fromEGID, TypeCache<T>.type, caller);
  89. enginesRootTarget.CheckAddEntityID(toEGID, TypeCache<T>.type, caller);
  90. enginesRootTarget.QueueSwapEntityOperation(fromEGID, toEGID
  91. , _enginesRoot.Target.FindRealComponents<T>(fromEGID)
  92. , caller);
  93. }
  94. #if UNITY_NATIVE
  95. public Native.NativeEntityRemove ToNativeRemove<T>(string memberName) where T : IEntityDescriptor, new()
  96. {
  97. return _enginesRoot.Target.ProvideNativeEntityRemoveQueue<T>(memberName);
  98. }
  99. public Native.NativeEntitySwap ToNativeSwap<T>(string memberName) where T : IEntityDescriptor, new()
  100. {
  101. return _enginesRoot.Target.ProvideNativeEntitySwapQueue<T>(memberName);
  102. }
  103. #endif
  104. //enginesRoot is a weakreference because GenericEntityStreamConsumerFactory can be injected inside
  105. //engines of other enginesRoot
  106. readonly WeakReference<EnginesRoot> _enginesRoot;
  107. }
  108. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  109. void QueueRemoveGroupOperation(ExclusiveBuildGroup groupID, string caller)
  110. {
  111. _entitiesOperations.QueueRemoveGroupOperation(groupID, caller);
  112. }
  113. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  114. void QueueSwapGroupOperation(ExclusiveBuildGroup fromGroupID, ExclusiveBuildGroup toGroupID, string caller)
  115. {
  116. _entitiesOperations.QueueSwapGroupOperation(fromGroupID, toGroupID, caller);
  117. }
  118. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  119. void QueueSwapEntityOperation
  120. (EGID fromID, EGID toID, IComponentBuilder[] componentBuilders, string caller)
  121. {
  122. _entitiesOperations.QueueSwapOperation(fromID, toID, componentBuilders, caller);
  123. }
  124. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  125. void QueueRemoveEntityOperation(EGID entityEGID, IComponentBuilder[] componentBuilders, string caller)
  126. {
  127. _entitiesOperations.QueueRemoveOperation(entityEGID, componentBuilders, caller);
  128. }
  129. }
  130. }