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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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>(this EntitiesDB entitiesDb, EGID entityGID, out uint index) where T : struct, IEntityViewComponent
  11. {
  12. if (entitiesDb.QueryEntitiesAndIndexInternal<T>(entityGID, out index, out MB<T> array) == true)
  13. return array;
  14. throw new EntityNotFoundException(entityGID, typeof(T));
  15. }
  16. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  17. public static bool TryQueryEntitiesAndIndex<T>(this EntitiesDB entitiesDb, EGID entityGID, out uint index, out MB<T> array)
  18. where T : struct, IEntityViewComponent
  19. {
  20. if (entitiesDb.QueryEntitiesAndIndexInternal<T>(entityGID, out index, out array) == true)
  21. return true;
  22. return false;
  23. }
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. public static bool TryQueryEntitiesAndIndex<T>(this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group, out uint index, out MB<T> array)
  26. where T : struct, IEntityViewComponent
  27. {
  28. if (entitiesDb.QueryEntitiesAndIndexInternal<T>(new EGID(id, group), out index, out array) == true)
  29. return true;
  30. return false;
  31. }
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. static bool QueryEntitiesAndIndexInternal<T>(this EntitiesDB entitiesDb, EGID entityGID, out uint index, out MB<T> buffer) where T : struct, IEntityViewComponent
  34. {
  35. index = 0;
  36. buffer = default;
  37. if (entitiesDb.SafeQueryEntityDictionary<T>(entityGID.groupID, out var safeDictionary) == false)
  38. return false;
  39. if (safeDictionary.TryFindIndex(entityGID.entityID, out index) == false)
  40. return false;
  41. buffer = (MB<T>) (safeDictionary as ITypeSafeDictionary<T>).GetValues(out _);
  42. return true;
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public static ref T QueryEntity<T>(this EntitiesDB entitiesDb, EGID entityGID) where T : struct, IEntityViewComponent
  46. {
  47. var array = entitiesDb.QueryEntitiesAndIndex<T>(entityGID, out var index);
  48. return ref array[(int) index];
  49. }
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public static ref T QueryEntity<T>(this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group) where T : struct, IEntityViewComponent
  52. {
  53. return ref entitiesDb.QueryEntity<T>(new EGID(id, group));
  54. }
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. public static ref T QueryUniqueEntity<T>(this EntitiesDB entitiesDb, ExclusiveGroupStruct group) where T : struct, IEntityViewComponent
  57. {
  58. var (entities, entitiescount) = entitiesDb.QueryEntities<T>(@group);
  59. #if DEBUG && !PROFILE_SVELTO
  60. if (entitiescount == 0)
  61. throw new ECSException("Unique entity not found '".FastConcat(typeof(T).ToString()).FastConcat("'"));
  62. if (entitiescount != 1)
  63. throw new ECSException("Unique entities must be unique! '".FastConcat(typeof(T).ToString())
  64. .FastConcat("'"));
  65. #endif
  66. return ref entities[0];
  67. }
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. public static MB<T> GetArrayAndEntityIndex<T>(this EGIDMapper<T> mapper, uint entityID, out uint index) where T : struct, IEntityViewComponent
  70. {
  71. if (mapper._map.TryFindIndex(entityID, out index))
  72. {
  73. return (MB<T>) mapper._map.GetValues(out _);
  74. }
  75. throw new ECSException("Entity not found");
  76. }
  77. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  78. public static bool TryGetArrayAndEntityIndex<T>(this EGIDMapper<T> mapper, uint entityID, out uint index, out MB<T> array) where T : struct, IEntityViewComponent
  79. {
  80. index = default;
  81. if (mapper._map != null && mapper._map.TryFindIndex(entityID, out index))
  82. {
  83. array = (MB<T>) mapper._map.GetValues(out _);
  84. return true;
  85. }
  86. array = default;
  87. return false;
  88. }
  89. }
  90. }