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.

EntityManagedDBExtensions.cs 4.9KB

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