fix EntitiesInRandomOrder

pull/4/head
cosmonaut 2023-04-06 14:54:20 -07:00
parent 5f0d694eb4
commit 4268762a0c
2 changed files with 28 additions and 3 deletions

View File

@ -15,7 +15,7 @@ namespace MoonTools.ECS
}
public ReverseSpanEnumerator<Entity> Entities => FilterStorage.FilterEntities(Signature);
public LinearCongruentialEnumerator EntitiesInRandomOrder => FilterStorage.FilterEntitiesRandom(Signature);
public RandomEntityEnumerator EntitiesInRandomOrder => FilterStorage.FilterEntitiesRandom(Signature);
public Entity RandomEntity => FilterStorage.FilterRandomEntity(Signature);
public int Count => FilterStorage.FilterCount(Signature);

View File

@ -54,9 +54,12 @@ namespace MoonTools.ECS
return filterSignatureToEntityIDs[filterSignature].GetEnumerator();
}
public LinearCongruentialEnumerator FilterEntitiesRandom(FilterSignature filterSignature)
public RandomEntityEnumerator FilterEntitiesRandom(FilterSignature filterSignature)
{
return RandomGenerator.LinearCongruentialGenerator(FilterCount(filterSignature));
return new RandomEntityEnumerator(
this,
filterSignature,
RandomGenerator.LinearCongruentialGenerator(FilterCount(filterSignature)));
}
public Entity FilterNthEntity(FilterSignature filterSignature, int index)
@ -178,4 +181,26 @@ namespace MoonTools.ECS
removeCallbacks.Add(filterSignature, callback);
}
}
public ref struct RandomEntityEnumerator
{
public RandomEntityEnumerator GetEnumerator() => this;
private FilterStorage FilterStorage;
private FilterSignature FilterSignature;
private LinearCongruentialEnumerator LinearCongruentialEnumerator;
internal RandomEntityEnumerator(
FilterStorage filterStorage,
FilterSignature filterSignature,
LinearCongruentialEnumerator linearCongruentialEnumerator)
{
FilterStorage = filterStorage;
FilterSignature = filterSignature;
LinearCongruentialEnumerator = linearCongruentialEnumerator;
}
public bool MoveNext() => LinearCongruentialEnumerator.MoveNext();
public Entity Current => FilterStorage.FilterNthEntity(FilterSignature, LinearCongruentialEnumerator.Current);
}
}