Skip to content

Commit

Permalink
delegate for entityview only
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Feb 25, 2024
1 parent e695828 commit f024825
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
.Set<Velocity>(new Velocity());


ecs.Filter<(Position, Velocity)>()
.Query((EntityView entity) => {
Console.WriteLine(entity);
});


var e2 = ecs.Entity("Main");
ref var pp = ref e2.Get<Position>();

Expand Down
21 changes: 21 additions & 0 deletions src/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ public bool MoveNext()
public void Reset() => _index = -1;
}

public delegate void QueryFilterDelegateWithEntity(EntityView entity);

public readonly ref partial struct FilterQuery<TFilter> where TFilter : struct
{
private readonly ReadOnlySpan<Archetype> _archetypes;
Expand All @@ -404,6 +406,25 @@ internal FilterQuery(ReadOnlySpan<Archetype> archetypes)
_archetypes = archetypes;
}

public void Query(QueryFilterDelegateWithEntity fn)
{
var terms = Lookup.Query<TFilter>.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<TFilter>.Terms);
Expand Down

0 comments on commit f024825

Please sign in to comment.