|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- using Svelto.DataStructures;
-
- namespace Svelto.ECS
- {
- /// <summary>
- /// NOTE THESE ENUMERABLES EXIST TO AVOID BOILERPLATE CODE AS THEY SKIP 0 SIZED GROUPS
- /// However if the normal pattern with the double foreach is used, this is not necessary
- /// Note: atm cannot be ref structs because they are returned in a valuetuple
- /// </summary>
- /// <typeparam name="T1"></typeparam>
- /// <typeparam name="T2"></typeparam>
- /// <typeparam name="T3"></typeparam>
- /// <typeparam name="T4"></typeparam>
- public readonly ref struct GroupsEnumerable<T1, T2, T3, T4> where T1 : struct, IEntityComponent
- where T2 : struct, IEntityComponent
- where T3 : struct, IEntityComponent
- where T4 : struct, IEntityComponent
- {
- public GroupsEnumerable(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
- {
- _db = db;
- _groups = groups;
- }
-
- public ref struct GroupsIterator
- {
- public GroupsIterator(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups) : this()
- {
- _groups = groups;
- _indexGroup = -1;
- _entitiesDB = db;
- }
-
- public bool MoveNext()
- {
- //attention, the while is necessary to skip empty groups
- while (++_indexGroup < _groups.count)
- {
- var exclusiveGroupStruct = _groups[_indexGroup];
- if (!exclusiveGroupStruct.IsEnabled())
- continue;
-
- var entityCollection1 = _entitiesDB.QueryEntities<T1, T2, T3, T4>(exclusiveGroupStruct);
-
- var array = entityCollection1;
- _buffers = new EntityCollection<T1, T2, T3, T4>(array.buffer1, array.buffer2, array.buffer3
- , array.buffer4);
- break;
- }
-
- var moveNext = _indexGroup < _groups.count;
-
- if (moveNext == false)
- Reset();
-
- return moveNext;
- }
-
- public void Reset() { _indexGroup = -1; }
-
- public RefCurrent Current => new RefCurrent(_buffers, _groups[_indexGroup]);
- public bool isValid => _indexGroup != -1;
-
- readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
-
- int _indexGroup;
- EntityCollection<T1, T2, T3, T4> _buffers;
- readonly EntitiesDB _entitiesDB;
- }
-
- public GroupsIterator GetEnumerator() { return new GroupsIterator(_db, _groups); }
-
- readonly EntitiesDB _db;
- readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
-
- public readonly ref struct RefCurrent
- {
- public RefCurrent(in EntityCollection<T1, T2, T3, T4> buffers, ExclusiveGroupStruct group)
- {
- _buffers = buffers;
- _group = group;
- }
-
- public void Deconstruct(out EntityCollection<T1, T2, T3, T4> buffers, out ExclusiveGroupStruct group)
- {
- buffers = _buffers;
- group = _group;
- }
-
- public readonly EntityCollection<T1, T2, T3, T4> _buffers;
- public readonly ExclusiveGroupStruct _group;
- }
- }
-
- public readonly ref struct GroupsEnumerable<T1, T2, T3> where T1 : struct, IEntityComponent
- where T2 : struct, IEntityComponent
- where T3 : struct, IEntityComponent
- {
- public GroupsEnumerable(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
- {
- _db = db;
- _groups = groups;
- }
-
- public ref struct GroupsIterator
- {
- public GroupsIterator(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups) : this()
- {
- _groups = groups;
- _indexGroup = -1;
- _entitiesDB = db;
- }
-
- public bool MoveNext()
- {
- //attention, the while is necessary to skip empty groups
- while (++_indexGroup < _groups.count)
- {
- var exclusiveGroupStruct = _groups[_indexGroup];
- if (!exclusiveGroupStruct.IsEnabled())
- continue;
-
- var entityCollection = _entitiesDB.QueryEntities<T1, T2, T3>(exclusiveGroupStruct);
- if (entityCollection.count == 0)
- continue;
-
- _buffers = entityCollection;
- break;
- }
-
- var moveNext = _indexGroup < _groups.count;
-
- if (moveNext == false)
- Reset();
-
- return moveNext;
- }
-
- public void Reset() { _indexGroup = -1; }
-
- public RefCurrent Current => new RefCurrent(_buffers, _groups[_indexGroup]);
- public bool isValid => _indexGroup != -1;
-
- readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
-
- int _indexGroup;
- EntityCollection<T1, T2, T3> _buffers;
- readonly EntitiesDB _entitiesDB;
- }
-
- public GroupsIterator GetEnumerator() { return new GroupsIterator(_db, _groups); }
-
- readonly EntitiesDB _db;
- readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
-
- public readonly ref struct RefCurrent
- {
- public RefCurrent(in EntityCollection<T1, T2, T3> buffers, ExclusiveGroupStruct group)
- {
- _buffers = buffers;
- _group = group;
- }
-
- public void Deconstruct(out EntityCollection<T1, T2, T3> buffers, out ExclusiveGroupStruct group)
- {
- buffers = _buffers;
- group = _group;
- }
-
- public readonly EntityCollection<T1, T2, T3> _buffers;
- public readonly ExclusiveGroupStruct _group;
- }
- }
-
- public readonly ref struct GroupsEnumerable<T1, T2>
- where T1 : struct, IEntityComponent where T2 : struct, IEntityComponent
- {
- public GroupsEnumerable(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
- {
- _db = db;
- _groups = groups;
- }
-
- public ref struct GroupsIterator
- {
- public GroupsIterator(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups) : this()
- {
- _db = db;
- _groups = groups;
- _indexGroup = -1;
- }
-
- public bool MoveNext()
- {
- //attention, the while is necessary to skip empty groups
- while (++_indexGroup < _groups.count)
- {
- var exclusiveGroupStruct = _groups[_indexGroup];
- if (!exclusiveGroupStruct.IsEnabled())
- continue;
-
- var entityCollection = _db.QueryEntities<T1, T2>(exclusiveGroupStruct);
- if (entityCollection.count == 0)
- continue;
-
- _buffers = entityCollection;
- break;
- }
-
- var moveNext = _indexGroup < _groups.count;
-
- if (moveNext == false)
- Reset();
-
- return moveNext;
- }
-
- public void Reset() { _indexGroup = -1; }
-
- public RefCurrent Current => new RefCurrent(_buffers, _groups[_indexGroup]);
- public bool isValid => _indexGroup != -1;
-
- readonly EntitiesDB _db;
- readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
-
- int _indexGroup;
- EntityCollection<T1, T2> _buffers;
- }
-
- public GroupsIterator GetEnumerator() { return new GroupsIterator(_db, _groups); }
-
- readonly EntitiesDB _db;
- readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
-
- public readonly ref struct RefCurrent
- {
- public RefCurrent(in EntityCollection<T1, T2> buffers, ExclusiveGroupStruct group)
- {
- _buffers = buffers;
- _group = group;
- }
-
- public void Deconstruct(out EntityCollection<T1, T2> buffers, out ExclusiveGroupStruct group)
- {
- buffers = _buffers;
- group = _group;
- }
-
- public readonly EntityCollection<T1, T2> _buffers;
- public readonly ExclusiveGroupStruct _group;
- }
- }
-
- public readonly ref struct GroupsEnumerable<T1> where T1 : struct, IEntityComponent
- {
- public GroupsEnumerable(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups)
- {
- _db = db;
- _groups = groups;
- }
-
- public ref struct GroupsIterator
- {
- public GroupsIterator(EntitiesDB db, in LocalFasterReadOnlyList<ExclusiveGroupStruct> groups) : this()
- {
- _db = db;
- _groups = groups;
- _indexGroup = -1;
- }
-
- public bool MoveNext()
- {
- //attention, the while is necessary to skip empty groups
- while (++_indexGroup < _groups.count)
- {
- var exclusiveGroupStruct = _groups[_indexGroup];
-
- if (!exclusiveGroupStruct.IsEnabled())
- continue;
-
- var entityCollection = _db.QueryEntities<T1>(exclusiveGroupStruct);
- if (entityCollection.count == 0)
- continue;
-
- _buffer = entityCollection;
- break;
- }
-
- var moveNext = _indexGroup < _groups.count;
-
- if (moveNext == false)
- Reset();
-
- return moveNext;
- }
-
- public void Reset() { _indexGroup = -1; }
-
- public RefCurrent Current => new RefCurrent(_buffer, _groups[_indexGroup]);
- public bool isValid => _indexGroup != -1;
-
- readonly EntitiesDB _db;
- readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
-
- int _indexGroup;
- EntityCollection<T1> _buffer;
- }
-
- public GroupsIterator GetEnumerator() { return new GroupsIterator(_db, _groups); }
-
- readonly EntitiesDB _db;
- readonly LocalFasterReadOnlyList<ExclusiveGroupStruct> _groups;
-
- public readonly ref struct RefCurrent
- {
- public RefCurrent(in EntityCollection<T1> buffers, in ExclusiveGroupStruct group)
- {
- _buffers = buffers;
- _group = group;
- }
-
- public void Deconstruct(out EntityCollection<T1> buffers, out ExclusiveGroupStruct group)
- {
- buffers = _buffers;
- group = _group;
- }
-
- public void Deconstruct(out EntityCollection<T1> buffers)
- {
- buffers = _buffers;
- }
-
- internal readonly EntityCollection<T1> _buffers;
- internal readonly ExclusiveGroupStruct _group;
- }
- }
- }
|