Mirror of Svelto.ECS because we're a fan of it
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

210 linhas
9.1KB

  1. using DBC.ECS;
  2. using Svelto.DataStructures;
  3. using Svelto.DataStructures.Native;
  4. namespace Svelto.ECS
  5. {
  6. /// <summary>
  7. /// This feature must be eventually tied to the new ExclusiveGroup that won't allow the use of
  8. /// custom EntitiesID
  9. /// The filters could be updated when entities buffer changes during the submission, while now this process
  10. /// is completely manual.
  11. /// Making this working properly is not in my priorities right now, as I will need to add the new group type
  12. /// AND optimize the submission process to be able to add more overhead
  13. /// </summary>
  14. public partial class EntitiesDB
  15. {
  16. FasterDictionary<RefWrapperType, FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>> _filters =>
  17. _enginesRoot._groupFilters;
  18. public LegacyFilters GetLegacyFilters()
  19. {
  20. return new LegacyFilters(_filters);
  21. }
  22. public readonly struct LegacyFilters
  23. {
  24. public LegacyFilters(
  25. FasterDictionary<RefWrapperType, FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>>
  26. filtersLegacy)
  27. {
  28. _filtersLegacy = filtersLegacy;
  29. }
  30. public ref LegacyFilterGroup CreateOrGetFilterForGroup<T>(int filterID, ExclusiveGroupStruct groupID)
  31. where T : struct, IEntityComponent
  32. {
  33. var refWrapper = TypeRefWrapper<T>.wrapper;
  34. return ref CreateOrGetFilterForGroup(filterID, groupID, refWrapper);
  35. }
  36. public bool HasFiltersForGroup<T>(ExclusiveGroupStruct groupID) where T : struct, IEntityComponent
  37. {
  38. if (_filtersLegacy.TryGetValue(TypeRefWrapper<T>.wrapper, out var fasterDictionary) == false)
  39. return false;
  40. return fasterDictionary.ContainsKey(groupID);
  41. }
  42. public bool HasFilterForGroup<T>(int filterID, ExclusiveGroupStruct groupID)
  43. where T : struct, IEntityComponent
  44. {
  45. if (_filtersLegacy.TryGetValue(TypeRefWrapper<T>.wrapper, out var fasterDictionary) == false)
  46. return false;
  47. if (fasterDictionary.TryGetValue(groupID, out var result))
  48. return result.HasFilter(filterID);
  49. return false;
  50. }
  51. public ref LegacyGroupFilters CreateOrGetFiltersForGroup<T>(ExclusiveGroupStruct groupID)
  52. where T : struct, IEntityComponent
  53. {
  54. var fasterDictionary = _filtersLegacy.GetOrAdd(TypeRefWrapper<T>.wrapper,
  55. () => new FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>());
  56. return ref fasterDictionary.GetOrAdd(groupID,
  57. () => new LegacyGroupFilters(new SharedSveltoDictionaryNative<int, LegacyFilterGroup>(0), groupID));
  58. }
  59. public ref LegacyGroupFilters GetFiltersForGroup<T>(ExclusiveGroupStruct groupID)
  60. where T : struct, IEntityComponent
  61. {
  62. #if DEBUG && !PROFILE_SVELTO
  63. if (_filtersLegacy.ContainsKey(TypeRefWrapper<T>.wrapper) == false)
  64. throw new ECSException($"trying to fetch not existing filters, type {typeof(T)}");
  65. if (_filtersLegacy[TypeRefWrapper<T>.wrapper].ContainsKey(groupID) == false)
  66. throw new ECSException(
  67. $"trying to fetch not existing filters, type {typeof(T)} group {groupID.ToName()}");
  68. #endif
  69. return ref _filtersLegacy[TypeRefWrapper<T>.wrapper].GetValueByRef(groupID);
  70. }
  71. public ref LegacyFilterGroup GetFilterForGroup<T>(int filterId, ExclusiveGroupStruct groupID)
  72. where T : struct, IEntityComponent
  73. {
  74. #if DEBUG && !PROFILE_SVELTO
  75. if (_filtersLegacy.ContainsKey(TypeRefWrapper<T>.wrapper) == false)
  76. throw new ECSException($"trying to fetch not existing filters, type {typeof(T)}");
  77. if (_filtersLegacy[TypeRefWrapper<T>.wrapper].ContainsKey(groupID) == false)
  78. throw new ECSException(
  79. $"trying to fetch not existing filters, type {typeof(T)} group {groupID.ToName()}");
  80. #endif
  81. return ref _filtersLegacy[TypeRefWrapper<T>.wrapper][groupID].GetFilter(filterId);
  82. }
  83. public bool TryGetFilterForGroup<T>(int filterId, ExclusiveGroupStruct groupID,
  84. out LegacyFilterGroup groupLegacyFilter) where T : struct, IEntityComponent
  85. {
  86. groupLegacyFilter = default;
  87. if (_filtersLegacy.TryGetValue(TypeRefWrapper<T>.wrapper, out var fasterDictionary) == false)
  88. return false;
  89. if (fasterDictionary.TryGetValue(groupID, out var groupFilters) == false)
  90. return false;
  91. if (groupFilters.TryGetFilter(filterId, out groupLegacyFilter) == false)
  92. return false;
  93. return true;
  94. }
  95. public bool TryGetFiltersForGroup<T>(ExclusiveGroupStruct groupID,
  96. out LegacyGroupFilters legacyGroupFilters) where T : struct, IEntityComponent
  97. {
  98. legacyGroupFilters = default;
  99. if (_filtersLegacy.TryGetValue(TypeRefWrapper<T>.wrapper, out var fasterDictionary) == false)
  100. return false;
  101. return fasterDictionary.TryGetValue(groupID, out legacyGroupFilters);
  102. }
  103. public void ClearFilter<T>(int filterID, ExclusiveGroupStruct exclusiveGroupStruct)
  104. {
  105. if (_filtersLegacy.TryGetValue(TypeRefWrapper<T>.wrapper, out var fasterDictionary))
  106. {
  107. Check.Require(fasterDictionary.ContainsKey(exclusiveGroupStruct),
  108. $"trying to clear filter not present in group {exclusiveGroupStruct}");
  109. fasterDictionary[exclusiveGroupStruct].ClearFilter(filterID);
  110. }
  111. }
  112. public void ClearFilters<T>(int filterID)
  113. {
  114. if (_filtersLegacy.TryGetValue(TypeRefWrapper<T>.wrapper, out var fasterDictionary))
  115. foreach (var filtersPerGroup in fasterDictionary)
  116. filtersPerGroup.value.ClearFilter(filterID);
  117. }
  118. public void DisposeFilters<T>(ExclusiveGroupStruct exclusiveGroupStruct)
  119. {
  120. if (_filtersLegacy.TryGetValue(TypeRefWrapper<T>.wrapper, out var fasterDictionary))
  121. {
  122. fasterDictionary[exclusiveGroupStruct].DisposeFilters();
  123. fasterDictionary.Remove(exclusiveGroupStruct);
  124. }
  125. }
  126. public void DisposeFilters<T>()
  127. {
  128. if (_filtersLegacy.TryGetValue(TypeRefWrapper<T>.wrapper, out var fasterDictionary))
  129. foreach (var filtersPerGroup in fasterDictionary)
  130. filtersPerGroup.value.DisposeFilters();
  131. _filtersLegacy.Remove(TypeRefWrapper<T>.wrapper);
  132. }
  133. public void DisposeFilterForGroup<T>(int resetFilterID, ExclusiveGroupStruct group)
  134. {
  135. if (_filtersLegacy.TryGetValue(TypeRefWrapper<T>.wrapper, out var fasterDictionary))
  136. fasterDictionary[@group].DisposeFilter(resetFilterID);
  137. }
  138. public bool TryRemoveEntityFromFilter<T>(int filtersID, EGID egid) where T : struct, IEntityComponent
  139. {
  140. if (TryGetFilterForGroup<T>(filtersID, egid.groupID, out var filter))
  141. return filter.TryRemove(egid.entityID);
  142. return false;
  143. }
  144. public void RemoveEntityFromFilter<T>(int filtersID, EGID egid) where T : struct, IEntityComponent
  145. {
  146. ref var filter = ref GetFilterForGroup<T>(filtersID, egid.groupID);
  147. filter.Remove(egid.entityID);
  148. }
  149. public bool AddEntityToFilter<N>(int filtersID, EGID egid, N mapper) where N : IEGIDMapper
  150. {
  151. ref var filter =
  152. ref CreateOrGetFilterForGroup(filtersID, egid.groupID, new RefWrapperType(mapper.entityType));
  153. return filter.Add(egid.entityID, mapper);
  154. }
  155. internal ref LegacyFilterGroup CreateOrGetFilterForGroup(int filterID, ExclusiveGroupStruct groupID,
  156. RefWrapperType refWrapper)
  157. {
  158. var fasterDictionary = _filtersLegacy.GetOrAdd(refWrapper,
  159. () => new FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>());
  160. var filters = fasterDictionary.GetOrAdd(groupID,
  161. (ref ExclusiveGroupStruct gid) =>
  162. new LegacyGroupFilters(new SharedSveltoDictionaryNative<int, LegacyFilterGroup>(0), gid),
  163. ref groupID);
  164. return ref filters.CreateOrGetFilter(filterID);
  165. }
  166. readonly FasterDictionary<RefWrapperType, FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>>
  167. _filtersLegacy;
  168. }
  169. }
  170. }