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.

101 lines
3.6KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Svelto.Common;
  4. using Svelto.DataStructures;
  5. using Svelto.ECS.Internal;
  6. namespace Svelto.ECS
  7. {
  8. /// <summary>
  9. /// to retrieve an EGIDMultiMapper use entitiesDB.QueryMappedEntities<T>(groups);
  10. /// </summary>
  11. /// <typeparam name="T"></typeparam>
  12. public readonly struct EGIDMultiMapper<T>: IEGIDMultiMapper where T : struct, _IInternalEntityComponent
  13. {
  14. internal EGIDMultiMapper(FasterDictionary<ExclusiveGroupStruct, ITypeSafeDictionary<T>> dictionary)
  15. {
  16. _dic = dictionary;
  17. }
  18. public int count
  19. {
  20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  21. get => _dic.count;
  22. }
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. public ref T Entity(EGID entity)
  25. {
  26. #if DEBUG && !PROFILE_SVELTO
  27. if (Exists(entity) == false)
  28. throw new Exception("EGIDMultiMapper: Entity not found");
  29. #endif
  30. ref var sveltoDictionary = ref _dic.GetValueByRef(entity.groupID);
  31. return ref sveltoDictionary.GetValueByRef(entity.entityID);
  32. }
  33. public EntityCollection<T> Entities(ExclusiveGroupStruct targetEgidGroupID)
  34. {
  35. uint count = 0;
  36. IBuffer<T> buffer;
  37. IEntityIDs ids = default;
  38. if (_dic.TryGetValue(targetEgidGroupID, out var typeSafeDictionary) == false)
  39. buffer = default;
  40. else
  41. {
  42. ITypeSafeDictionary<T> safeDictionary = typeSafeDictionary;
  43. buffer = safeDictionary.GetValues(out count);
  44. ids = safeDictionary.entityIDs;
  45. }
  46. return new EntityCollection<T>(buffer, ids, count);
  47. }
  48. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  49. public bool Exists(EGID entity)
  50. {
  51. return _dic.TryFindIndex(entity.groupID, out var index) && _dic.GetDirectValueByRef(index).ContainsKey(entity.entityID);
  52. }
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. public bool TryGetEntity(EGID entity, out T component)
  55. {
  56. component = default;
  57. return _dic.TryFindIndex(entity.groupID, out var index)
  58. && _dic.GetDirectValueByRef(index).TryGetValue(entity.entityID, out component);
  59. }
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. public bool FindIndex(ExclusiveGroupStruct group, uint entityID, out uint index)
  62. {
  63. index = 0;
  64. return _dic.TryFindIndex(group, out var groupIndex) &&
  65. _dic.GetDirectValueByRef(groupIndex).TryFindIndex(entityID, out index);
  66. }
  67. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  68. public uint GetIndex(ExclusiveGroupStruct group, uint entityID)
  69. {
  70. uint groupIndex = _dic.GetIndex(group);
  71. return _dic.GetDirectValueByRef(groupIndex).GetIndex(entityID);
  72. }
  73. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  74. public uint GetIndex(EGID egid)
  75. {
  76. return GetIndex(egid.groupID, egid.entityID);
  77. }
  78. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. public bool Exists(ExclusiveGroupStruct group, uint entityID)
  80. {
  81. return _dic.TryFindIndex(group, out var groupIndex) &&
  82. _dic.GetDirectValueByRef(groupIndex).ContainsKey(entityID);
  83. }
  84. public Type entityType => TypeCache<T>.type;
  85. readonly FasterDictionary<ExclusiveGroupStruct, ITypeSafeDictionary<T>> _dic;
  86. }
  87. }