You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.9 KiB
92 lines
2.9 KiB
using System; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using gaemstone.ECS.Internal; |
|
using gaemstone.ECS.Utility; |
|
using static flecs_hub.flecs; |
|
|
|
namespace gaemstone.ECS; |
|
|
|
public unsafe class Iterator<TContext> : Iterator |
|
, IEnumerable<Iterator<TContext>> |
|
{ |
|
public new World<TContext> World => new(base.World); |
|
|
|
public Iterator(ecs_iter_t* handle) : base(handle) { } |
|
public Iterator(ecs_iter_t value) : base(value) { } |
|
|
|
public static TermIterator<TContext> FromTerm(World world, Term term) |
|
{ |
|
using var alloc = TempAllocator.Use(); |
|
var flecsTerm = term.ToFlecs(alloc); |
|
var flecsIter = ecs_term_iter(world, &flecsTerm); |
|
return new(flecsIter); |
|
} |
|
|
|
public new Entity<TContext>? GetVar(Variable var) |
|
=> Entity<TContext>.GetOrNull(World, base.GetVar(var)); |
|
public new Iterator<TContext> SetVar(Variable var, Entity entity) |
|
=> (Iterator<TContext>)base.SetVar(var, entity); |
|
|
|
public new Entity<TContext>? First() |
|
=> Entity<TContext>.GetOrNull(World, base.First()); |
|
public new IEnumerable<Entity<TContext>> GetAllEntities() |
|
=> base.GetAllEntities().Select(e => Entity<TContext>.GetOrInvalid(World, e)); |
|
|
|
public new Id<TContext> FieldId(int index) |
|
=> Id<TContext>.GetUnsafe(World, base.FieldId(index)); |
|
|
|
public new Entity<TContext> Entity(int index) |
|
=> Entity<TContext>.GetOrInvalid(World, base.Entity(index)); |
|
|
|
// IEnumerable implementation |
|
public virtual IEnumerator<Iterator<TContext>> GetEnumerator() |
|
{ while (Next()) yield return this; } |
|
IEnumerator IEnumerable.GetEnumerator() |
|
=> GetEnumerator(); |
|
} |
|
|
|
[Flags] |
|
public enum IteratorFlags : uint |
|
{ |
|
/// <summary> Does iterator contain valid result. </summary> |
|
IsValid = EcsIterIsValid, |
|
/// <summary> Does iterator provide (component). </summary> |
|
NoData = EcsIterNoData, |
|
/// <summary> Is iterator instanced. </summary> |
|
IsInstanced = EcsIterIsInstanced, |
|
/// <summary> Does result have shared terms. </summary> |
|
HasShared = EcsIterHasShared, |
|
/// <summary> Result only populates table. </summary> |
|
TableOnly = EcsIterTableOnly, |
|
/// <summary> Treat terms with entity subject as optional. </summary> |
|
EntityOptional = EcsIterEntityOptional, |
|
/// <summary> Iterator has no results. </summary> |
|
NoResults = EcsIterNoResults, |
|
/// <summary> Only evaluate non-this terms. </summary> |
|
IgnoreThis = EcsIterIgnoreThis, |
|
/// <summary> Does iterator have conditionally set fields. </summary> |
|
MatchVar = EcsIterMatchVar, |
|
/// <summary> </summary> |
|
HasCondSet = EcsIterHasCondSet, |
|
/// <summary> Profile iterator performance. </summary> |
|
Profile = EcsIterProfile, |
|
} |
|
|
|
public class Variable |
|
{ |
|
public int Index { get; } |
|
public string Name { get; } |
|
public Variable(int index, string name) |
|
{ Index = index; Name = name; } |
|
} |
|
|
|
public unsafe class TermIterator<TContext> |
|
: Iterator<TContext> |
|
{ |
|
internal TermIterator(ecs_iter_t value) |
|
: base(value) { } |
|
public override bool Next() |
|
=> ecs_term_next(Handle); |
|
}
|
|
|