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.

127 line
4.9KB

  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. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. public static ref T QueryUniqueEntity<T>
  64. (this EntitiesDB entitiesDb, ExclusiveGroupStruct group) where T : struct, IEntityViewComponent
  65. {
  66. var (entities, entitiescount) = entitiesDb.QueryEntities<T>(@group);
  67. #if DEBUG && !PROFILE_SVELTO
  68. if (entitiescount == 0)
  69. throw new ECSException("Unique entity not found '".FastConcat(typeof(T).ToString()).FastConcat("'"));
  70. if (entitiescount != 1)
  71. throw new ECSException("Unique entities must be unique! '".FastConcat(typeof(T).ToString())
  72. .FastConcat("'"));
  73. #endif
  74. return ref entities[0];
  75. }
  76. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  77. public static MB<T> GetArrayAndEntityIndex<T>
  78. (this EGIDMapper<T> mapper, uint entityID, out uint index) where T : struct, IEntityViewComponent
  79. {
  80. if (mapper._map.TryFindIndex(entityID, out index))
  81. {
  82. return (MB<T>) mapper._map.GetValues(out _);
  83. }
  84. throw new ECSException("Entity not found");
  85. }
  86. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87. public static bool TryGetArrayAndEntityIndex<T>
  88. (this EGIDMapper<T> mapper, uint entityID, out uint index, out MB<T> array)
  89. where T : struct, IEntityViewComponent
  90. {
  91. index = default;
  92. if (mapper._map != null && mapper._map.TryFindIndex(entityID, out index))
  93. {
  94. array = (MB<T>) mapper._map.GetValues(out _);
  95. return true;
  96. }
  97. array = default;
  98. return false;
  99. }
  100. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  101. public static AllGroupsEnumerable<T1> QueryEntities<T1>(this EntitiesDB db)
  102. where T1 :struct, IEntityComponent
  103. {
  104. return new AllGroupsEnumerable<T1>(db);
  105. }
  106. }
  107. }