Skip to content

Commit

Permalink
without
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Angelini committed Feb 26, 2024
1 parent 381deca commit 8da3a32
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
7 changes: 5 additions & 2 deletions samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@

e.Delete();

ecs.Entity()
.Set<Position>(new Position())
.Set<Velocity>(new Velocity());

for (int i = 0; i < ENTITIES_COUNT; i++)
for (int i = 0; i < ENTITIES_COUNT / 1000; i++)
ecs.Entity()
.Set<Position>(new Position())
.Set<Velocity>(new Velocity())
Expand All @@ -101,7 +104,7 @@
// pos.Y *= vel.Y;
// });

ecs.System((ref Position pos , ref Velocity vel) => {
ecs.System<Not<PlayerTag>, Position, Velocity>((ref Position pos , ref Velocity vel) => {
pos.X *= vel.X;
pos.Y *= vel.Y;
});
Expand Down
33 changes: 25 additions & 8 deletions src/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,12 @@ public QueryIterator2 Query2<TFilter>() where TFilter : struct
return new QueryIterator2(list);
}

public void System<T0, T1>(QueryFilterDelegate<T0, T1> fn) where T0 : struct where T1 : struct
public void System<TFilter, T0, T1>(QueryFilterDelegate<T0, T1> fn)
where TFilter : struct where T0 : struct where T1 : struct
{
var hash = Lookup.Query<(T0, T1)>.Hash;
var terms = Lookup.Query<(T0, T1)>.Terms.AsSpan();
var hash = Lookup.Query<(T0, T1), TFilter>.Hash;
var terms = Lookup.Query<(T0, T1), TFilter>.Terms.AsSpan();
var withouts = Lookup.Query<(T0, T1), TFilter>.Withouts;

if (!_typeIndex.TryGetValue(hash, out var arch))
{
Expand All @@ -377,9 +379,15 @@ public void System<T0, T1>(QueryFilterDelegate<T0, T1> fn) where T0 : struct whe
if (arch == null)
return;

InternalQuery(arch, fn, terms);
InternalQuery(arch, fn, terms, withouts);

static void InternalQuery(Archetype root, QueryFilterDelegate<T0, T1> fn, ReadOnlySpan<Term> terms)
static void InternalQuery
(
Archetype root,
QueryFilterDelegate<T0, T1> fn,
ReadOnlySpan<Term> terms,
ImmutableDictionary<ulong, Term> withouts
)
{
if (root.Count > 0)
{
Expand Down Expand Up @@ -408,7 +416,8 @@ static void InternalQuery(Archetype root, QueryFilterDelegate<T0, T1> fn, ReadOn

while (Unsafe.IsAddressLessThan(ref start, ref end))
{
InternalQuery(start.Archetype, fn, terms);
if (!withouts.ContainsKey(start.ComponentID))
InternalQuery(start.Archetype, fn, terms, withouts);

start = ref Unsafe.Add(ref start, 1);
}
Expand Down Expand Up @@ -779,6 +788,7 @@ internal static class Query<TQuery, TFilter> where TQuery : struct where TFilter
{
public static readonly ImmutableArray<Term> Terms;
public static readonly ImmutableArray<Term> Columns;
public static readonly ImmutableDictionary<ulong, Term> Withs, Withouts;
public static readonly ulong Hash;

static Query()
Expand All @@ -790,13 +800,17 @@ static Query()
ParseType<TFilter>(list);
Terms = list.ToImmutableArray();

Hash = GetHash(Terms.AsSpan());
Withs = list.Where(s => s.Op == TermOp.With).ToImmutableDictionary(s => s.ID, k => k);
Withouts = list.Where(s => s.Op == TermOp.Without).ToImmutableDictionary(s => s.ID, k => k);

Hash = GetHash(Withs.Values.ToArray());
}
}

internal static class Query<T> where T : struct
{
public static readonly ImmutableArray<Term> Terms;
public static readonly ImmutableDictionary<ulong, Term> Withs, Withouts;
public static readonly ulong Hash;

static Query()
Expand All @@ -805,7 +819,10 @@ static Query()
ParseType<T>(list);
Terms = list.ToImmutableArray();

Hash = GetHash(Terms.AsSpan());
Withs = list.Where(s => s.Op == TermOp.With).ToImmutableDictionary(s => s.ID, k => k);
Withouts = list.Where(s => s.Op == TermOp.Without).ToImmutableDictionary(s => s.ID, k => k);

Hash = GetHash(Withs.Values.ToArray());
}
}
}
Expand Down

0 comments on commit 8da3a32

Please sign in to comment.