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.

130 lines
5.0KB

  1. using System.Runtime.CompilerServices;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS.Hybrid;
  4. using Svelto.ECS.Internal;
  5. namespace Svelto.ECS
  6. {
  7. public static class EntityManagedDBExtensions
  8. {
  9. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  10. public static MB<T> QueryEntitiesAndIndex<T>
  11. (this EntitiesDB entitiesDb, EGID entityGID, out uint index) where T : struct, IEntityViewComponent
  12. {
  13. if (entitiesDb.QueryEntitiesAndIndexInternal<T>(entityGID, out index, out MB<T> array) == true)
  14. return array;
  15. throw new EntityNotFoundException(entityGID, typeof(T));
  16. }
  17. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18. public static bool TryQueryEntitiesAndIndex<T>
  19. (this EntitiesDB entitiesDb, EGID entityGID, out uint index, out MB<T> array)
  20. where T : struct, IEntityViewComponent
  21. {
  22. if (entitiesDb.QueryEntitiesAndIndexInternal<T>(entityGID, out index, out array) == true)
  23. return true;
  24. return false;
  25. }
  26. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  27. public static bool TryQueryEntitiesAndIndex<T>
  28. (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group, out uint index, out MB<T> array)
  29. where T : struct, IEntityViewComponent
  30. {
  31. if (entitiesDb.QueryEntitiesAndIndexInternal<T>(new EGID(id, group), out index, out array) == true)
  32. return true;
  33. return false;
  34. }
  35. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  36. static bool QueryEntitiesAndIndexInternal<T>
  37. (this EntitiesDB entitiesDb, EGID entityGID, out uint index, out MB<T> buffer)
  38. where T : struct, IEntityViewComponent
  39. {
  40. index = 0;
  41. buffer = default;
  42. if (entitiesDb.SafeQueryEntityDictionary<T>(entityGID.groupID, out var safeDictionary) == false)
  43. return false;
  44. if (safeDictionary.TryFindIndex(entityGID.entityID, out index) == false)
  45. return false;
  46. buffer = (MB<T>) (safeDictionary as ITypeSafeDictionary<T>).GetValues(out _);
  47. return true;
  48. }
  49. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  50. public static ref T QueryEntity<T>
  51. (this EntitiesDB entitiesDb, EGID entityGID) where T : struct, IEntityViewComponent
  52. {
  53. var array = entitiesDb.QueryEntitiesAndIndex<T>(entityGID, out var index);
  54. return ref array[(int) index];
  55. }
  56. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  57. public static ref T QueryEntity<T>
  58. (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group) where T : struct, IEntityViewComponent
  59. {
  60. return ref entitiesDb.QueryEntity<T>(new EGID(id, group));
  61. }
  62. /// <summary>
  63. /// Expects that only one entity of type T exists in the group
  64. /// </summary>
  65. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  66. public static ref T QueryUniqueEntity<T>
  67. (this EntitiesDB entitiesDb, ExclusiveGroupStruct group) where T : struct, IEntityViewComponent
  68. {
  69. var (entities, entitiescount) = entitiesDb.QueryEntities<T>(@group);
  70. #if DEBUG && !PROFILE_SVELTO
  71. if (entitiescount == 0)
  72. throw new ECSException("Unique entity not found '".FastConcat(typeof(T).ToString()).FastConcat("'"));
  73. if (entitiescount != 1)
  74. throw new ECSException("Unique entities must be unique! '".FastConcat(typeof(T).ToString())
  75. .FastConcat("'"));
  76. #endif
  77. return ref entities[0];
  78. }
  79. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  80. public static MB<T> GetArrayAndEntityIndex<T>
  81. (this EGIDMapper<T> mapper, uint entityID, out uint index) where T : struct, IEntityViewComponent
  82. {
  83. if (mapper._map.TryFindIndex(entityID, out index))
  84. {
  85. return (MB<T>) mapper._map.GetValues(out _);
  86. }
  87. throw new ECSException("Entity not found");
  88. }
  89. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  90. public static bool TryGetArrayAndEntityIndex<T>
  91. (this EGIDMapper<T> mapper, uint entityID, out uint index, out MB<T> array)
  92. where T : struct, IEntityViewComponent
  93. {
  94. index = default;
  95. if (mapper._map != null && mapper._map.TryFindIndex(entityID, out index))
  96. {
  97. array = (MB<T>) mapper._map.GetValues(out _);
  98. return true;
  99. }
  100. array = default;
  101. return false;
  102. }
  103. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  104. public static AllGroupsEnumerable<T1> QueryEntities<T1>(this EntitiesDB db)
  105. where T1 :struct, IEntityViewComponent
  106. {
  107. return new AllGroupsEnumerable<T1>(db);
  108. }
  109. }
  110. }