From 46e171940eea723f2ad8376b5056afd9b9d8a340 Mon Sep 17 00:00:00 2001 From: copygirl Date: Sat, 31 Dec 2022 00:47:53 +0100 Subject: [PATCH] Rename Iterator.FieldRef to .Field and more - Add FieldOrEmpty for reference types - Reorder methods in Iterator class --- src/gaemstone.ECS/Iterator.cs | 50 +++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/gaemstone.ECS/Iterator.cs b/src/gaemstone.ECS/Iterator.cs index f37011d..b35d818 100644 --- a/src/gaemstone.ECS/Iterator.cs +++ b/src/gaemstone.ECS/Iterator.cs @@ -40,6 +40,7 @@ public unsafe class Iterator ecs_iter_fini(ptr); } + public EntityRef GetVar(Variable var) { fixed (ecs_iter_t* ptr = &Value) @@ -53,6 +54,7 @@ public unsafe class Iterator return this; } + public bool Next() { fixed (ecs_iter_t* ptr = &Value) { @@ -68,8 +70,23 @@ public unsafe class Iterator } } - public EntityRef Entity(int index) - => new(World, new(Value.entities[index])); + + public bool FieldIsSet(int index) + { + fixed (ecs_iter_t* ptr = &Value) + return ecs_field_is_set(ptr, index); + } + + public bool FieldIs(int index, Id id) + { + fixed (ecs_iter_t* ptr = &Value) + return ecs_field_id(ptr, index) == id.Value; + } + + // TODO: Potentially misleading, doesn't check the field's backing data type. + // The id might be "(Identifier, Name)", but its data type "Identifier". + public bool FieldIs(int index) + => FieldIs(index, World.LookupByType()); public IdRef FieldId(int index) { @@ -77,6 +94,10 @@ public unsafe class Iterator return new(World, new(ecs_field_id(ptr, index))); } + + public EntityRef Entity(int index) + => new(World, new(Value.entities[index])); + public Span Field(int index) where T : unmanaged { @@ -88,27 +109,15 @@ public unsafe class Iterator } } - public Span FieldOrEmpty(int index) - where T : unmanaged => FieldIsSet(index) ? Field(index) : default; + public SpanToRef Field(int index, T _ = null!) where T : class + => new(Field(index)); - public SpanToRef FieldRef(int index) - where T : class => new(Field(index)); + public Span FieldOrEmpty(int index) where T : unmanaged + => FieldIsSet(index) ? Field(index) : Span.Empty; - public bool FieldIsSet(int index) - { - fixed (ecs_iter_t* ptr = &Value) - return ecs_field_is_set(ptr, index); - } + public SpanToRef FieldOrEmpty(int index, T _ = null!) where T : class + => FieldIsSet(index) ? Field(index, _) : SpanToRef.Empty; - // TODO: Potentially misleading, doesn't check the field's backing data type. - // The id might be "(Identifier, Name)", but its data type "Identifier". - public bool FieldIs(int index) - => FieldIs(index, World.LookupByType()); - public bool FieldIs(int index, Id id) - { - fixed (ecs_iter_t* ptr = &Value) - return ecs_field_id(ptr, index) == id.Value; - } public override string ToString() { @@ -132,6 +141,7 @@ public unsafe class Iterator public readonly ref struct SpanToRef where T : class { + public static SpanToRef Empty => default; private readonly Span _span; public int Length => _span.Length; public T? this[int index] => (T?)_span[index].Target;