diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 9ec2684..c6f18b3 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -12,6 +12,12 @@ .Set(new Velocity()); +ecs.Filter<(Position, Velocity)>() + .Query((EntityView entity) => { + Console.WriteLine(entity); + }); + + var e2 = ecs.Entity("Main"); ref var pp = ref e2.Get(); diff --git a/src/World.cs b/src/World.cs index 4c6355e..c840a12 100644 --- a/src/World.cs +++ b/src/World.cs @@ -395,6 +395,8 @@ public bool MoveNext() public void Reset() => _index = -1; } +public delegate void QueryFilterDelegateWithEntity(EntityView entity); + public readonly ref partial struct FilterQuery where TFilter : struct { private readonly ReadOnlySpan _archetypes; @@ -404,6 +406,25 @@ internal FilterQuery(ReadOnlySpan archetypes) _archetypes = archetypes; } + public void Query(QueryFilterDelegateWithEntity fn) + { + var terms = Lookup.Query.Terms; + var query = new QueryInternal(_archetypes, terms); + foreach (var arch in query) + { + foreach (ref readonly var chunk in arch) + { + ref var entity = ref chunk.Entities[0]; + ref var last = ref Unsafe.Add(ref entity, chunk.Count); + while (Unsafe.IsAddressLessThan(ref entity, ref last)) + { + fn(entity); + entity = ref Unsafe.Add(ref entity, 1); + } + } + } + } + public QueryIterator GetEnumerator() { return new QueryIterator(_archetypes, Lookup.Query.Terms);