Mirror of Svelto.ECS because we're a fan of it
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

151 行
6.0KB

  1. using System.Runtime.CompilerServices;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS.Internal;
  4. //todo: once using native memory for unmanaged struct will be optional, this will need to be moved under the Native namespace
  5. namespace Svelto.ECS
  6. {
  7. public static class EntityNativeDBExtensions
  8. {
  9. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  10. public static NB<T> QueryEntitiesAndIndex<T>
  11. (this EntitiesDB entitiesDb, EGID entityGID, out uint index) where T : unmanaged, IEntityComponent
  12. {
  13. if (entitiesDb.QueryEntitiesAndIndexInternal(entityGID, out index, out NB<T> array) == true)
  14. return array;
  15. throw new EntityNotFoundException(entityGID, typeof(T));
  16. }
  17. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18. public static NB<T> QueryEntitiesAndIndex<T>
  19. (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group, out uint index)
  20. where T : unmanaged, IEntityComponent
  21. {
  22. EGID entityGID = new EGID(id, group);
  23. if (entitiesDb.QueryEntitiesAndIndexInternal(entityGID, out index, out NB<T> array) == true)
  24. return array;
  25. throw new EntityNotFoundException(entityGID, typeof(T));
  26. }
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public static bool TryQueryEntitiesAndIndex<T>
  29. (this EntitiesDB entitiesDb, EGID entityGID, out uint index, out NB<T> array)
  30. where T : unmanaged, IEntityComponent
  31. {
  32. if (entitiesDb.QueryEntitiesAndIndexInternal(entityGID, out index, out array) == true)
  33. return true;
  34. return false;
  35. }
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public static bool TryQueryEntitiesAndIndex<T>
  38. (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group, out uint index, out NB<T> array)
  39. where T : unmanaged, IEntityComponent
  40. {
  41. if (entitiesDb.QueryEntitiesAndIndexInternal(new EGID(id, group), out index, out array) == true)
  42. return true;
  43. return false;
  44. }
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public static bool TryGetEntity<T>(this EntitiesDB entitiesDb, uint entityID, ExclusiveGroupStruct @group, out T value)
  47. where T : unmanaged, IEntityComponent
  48. {
  49. if (TryQueryEntitiesAndIndex<T>(entitiesDb, entityID, group, out var index, out var array))
  50. {
  51. value = array[index];
  52. return true;
  53. }
  54. value = default;
  55. return false;
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public static bool TryGetEntity<T>(this EntitiesDB entitiesDb, EGID egid, out T value)
  59. where T : unmanaged, IEntityComponent
  60. {
  61. return TryGetEntity<T>(entitiesDb, egid.entityID, egid.groupID, out value);
  62. }
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. static bool QueryEntitiesAndIndexInternal<T>
  65. (this EntitiesDB entitiesDb, EGID entityGID, out uint index, out NB<T> buffer)
  66. where T : unmanaged, IEntityComponent
  67. {
  68. index = 0;
  69. buffer = default;
  70. if (entitiesDb.SafeQueryEntityDictionary<T>(entityGID.groupID, out var safeDictionary) == false)
  71. return false;
  72. if (safeDictionary.TryFindIndex(entityGID.entityID, out index) == false)
  73. return false;
  74. buffer = (NB<T>) (safeDictionary as ITypeSafeDictionary<T>).GetValues(out _);
  75. return true;
  76. }
  77. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  78. public static ref T QueryEntity<T>
  79. (this EntitiesDB entitiesDb, EGID entityGID) where T : unmanaged, IEntityComponent
  80. {
  81. var array = entitiesDb.QueryEntitiesAndIndex<T>(entityGID, out var index);
  82. return ref array[(int) index];
  83. }
  84. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  85. public static ref T QueryEntity<T>
  86. (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
  87. {
  88. return ref entitiesDb.QueryEntity<T>(new EGID(id, group));
  89. }
  90. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  91. public static ref T QueryUniqueEntity<T>
  92. (this EntitiesDB entitiesDb, ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
  93. {
  94. var (entities, entitiescount) = entitiesDb.QueryEntities<T>(@group);
  95. #if DEBUG && !PROFILE_SVELTO
  96. if (entitiescount == 0)
  97. throw new ECSException("Unique entity not found '".FastConcat(typeof(T).ToString()).FastConcat("'"));
  98. if (entitiescount != 1)
  99. throw new ECSException("Unique entities must be unique! '".FastConcat(typeof(T).ToString())
  100. .FastConcat("'"));
  101. #endif
  102. return ref entities[0];
  103. }
  104. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  105. public static NB<T> GetArrayAndEntityIndex<T>
  106. (this EGIDMapper<T> mapper, uint entityID, out uint index) where T : unmanaged, IEntityComponent
  107. {
  108. if (mapper._map.TryFindIndex(entityID, out index))
  109. {
  110. return (NB<T>) mapper._map.GetValues(out _);
  111. }
  112. throw new ECSException("Entity not found");
  113. }
  114. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  115. public static bool TryGetArrayAndEntityIndex<T>
  116. (this EGIDMapper<T> mapper, uint entityID, out uint index, out NB<T> array)
  117. where T : unmanaged, IEntityComponent
  118. {
  119. index = default;
  120. if (mapper._map != null && mapper._map.TryFindIndex(entityID, out index))
  121. {
  122. array = (NB<T>) mapper._map.GetValues(out _);
  123. return true;
  124. }
  125. array = default;
  126. return false;
  127. }
  128. }
  129. }