Mirror of Svelto.ECS because we're a fan of it
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

78 строки
2.6KB

  1. using Svelto.Common;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS.Internal;
  4. namespace Svelto.ECS
  5. {
  6. /// <summary>
  7. /// ToDo it would be interesting to have a version of this dedicated to unmanaged, IEntityComponent
  8. /// that can be burstifiable
  9. /// </summary>
  10. /// <typeparam name="T1"></typeparam>
  11. public readonly ref struct AllGroupsEnumerable<T1> where T1 : struct, _IInternalEntityComponent
  12. {
  13. public readonly ref struct GroupCollection
  14. {
  15. readonly EntityCollection<T1> collection;
  16. readonly ExclusiveGroupStruct group;
  17. public GroupCollection(EntityCollection<T1> entityCollection, ExclusiveGroupStruct groupKey)
  18. {
  19. collection = entityCollection;
  20. group = groupKey;
  21. }
  22. public void Deconstruct(out EntityCollection<T1> collection, out ExclusiveGroupStruct group)
  23. {
  24. collection = this.collection;
  25. group = this.@group;
  26. }
  27. }
  28. public AllGroupsEnumerable(EntitiesDB db) { _db = db; }
  29. public ref struct GroupsIterator
  30. {
  31. public GroupsIterator(EntitiesDB db) : this()
  32. {
  33. _db = db.FindGroups_INTERNAL(TypeCache<T1>.type).GetEnumerator();
  34. }
  35. public bool MoveNext()
  36. {
  37. //attention, the while is necessary to skip empty groups
  38. while (_db.MoveNext() == true)
  39. {
  40. var group = _db.Current;
  41. if (group.key.IsEnabled() == false)
  42. continue;
  43. ITypeSafeDictionary<T1> typeSafeDictionary = @group.value as ITypeSafeDictionary<T1>;
  44. if (typeSafeDictionary.count == 0)
  45. continue;
  46. _array = new GroupCollection(
  47. new EntityCollection<T1>(
  48. typeSafeDictionary.GetValues(out var count),
  49. typeSafeDictionary.entityIDs, count), group.key);
  50. return true;
  51. }
  52. return false;
  53. }
  54. public GroupCollection Current => _array;
  55. SveltoDictionaryKeyValueEnumerator<ExclusiveGroupStruct, ITypeSafeDictionary,
  56. ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>, ManagedStrategy<ITypeSafeDictionary>,
  57. ManagedStrategy<int>> _db;
  58. GroupCollection _array;
  59. }
  60. public GroupsIterator GetEnumerator() { return new GroupsIterator(_db); }
  61. readonly EntitiesDB _db;
  62. }
  63. }