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.

70 lines
2.3KB

  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 struct AllGroupsEnumerable<T1> where T1 : struct, IEntityComponent
  12. {
  13. public ref struct GroupCollection
  14. {
  15. internal EntityCollection<T1> collection;
  16. internal ExclusiveGroupStruct group;
  17. public void Deconstruct(out EntityCollection<T1> collection, out ExclusiveGroupStruct group)
  18. {
  19. collection = this.collection;
  20. group = this.@group;
  21. }
  22. }
  23. public AllGroupsEnumerable(EntitiesDB db) { _db = db; }
  24. public ref struct GroupsIterator
  25. {
  26. public GroupsIterator(EntitiesDB db) : this()
  27. {
  28. _db = db.FindGroups_INTERNAL(TypeCache<T1>.type).GetEnumerator();
  29. }
  30. public bool MoveNext()
  31. {
  32. //attention, the while is necessary to skip empty groups
  33. while (_db.MoveNext() == true)
  34. {
  35. var group = _db.Current;
  36. if (group.key.IsEnabled() == false)
  37. continue;
  38. ITypeSafeDictionary<T1> typeSafeDictionary = @group.value as ITypeSafeDictionary<T1>;
  39. if (typeSafeDictionary.count == 0)
  40. continue;
  41. _array.collection = new EntityCollection<T1>(typeSafeDictionary.GetValues(out var count), count,
  42. typeSafeDictionary.entityIDs);
  43. _array.@group = group.key;
  44. return true;
  45. }
  46. return false;
  47. }
  48. public GroupCollection Current => _array;
  49. SveltoDictionaryKeyValueEnumerator<ExclusiveGroupStruct, ITypeSafeDictionary,
  50. ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>, ManagedStrategy<ITypeSafeDictionary>,
  51. ManagedStrategy<int>> _db;
  52. GroupCollection _array;
  53. }
  54. public GroupsIterator GetEnumerator() { return new GroupsIterator(_db); }
  55. readonly EntitiesDB _db;
  56. }
  57. }