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.

71 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. _array.@group = group.Key;
  43. return true;
  44. }
  45. return false;
  46. }
  47. public GroupCollection Current => _array;
  48. SveltoDictionaryKeyValueEnumerator<ExclusiveGroupStruct, ITypeSafeDictionary,
  49. ManagedStrategy<SveltoDictionaryNode<ExclusiveGroupStruct>>, ManagedStrategy<ITypeSafeDictionary>,
  50. ManagedStrategy<int>> _db;
  51. GroupCollection _array;
  52. }
  53. public GroupsIterator GetEnumerator() { return new GroupsIterator(_db); }
  54. readonly EntitiesDB _db;
  55. }
  56. }