Add EntityRef.GetTarget, .Parent and .GetChildren

wip/source-generators
copygirl 2 years ago
parent 10d7210ff0
commit 9e1ab326c7
  1. 19
      src/gaemstone/ECS/EntityRef.cs

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using gaemstone.Utility;
@ -57,6 +58,17 @@ public unsafe sealed class EntityRef
private static EntityPath? EnsureRelativePath(EntityPath? path)
{ if (path?.IsAbsolute == true) throw new ArgumentException("path must not be absolute", nameof(path)); return path; }
public EntityRef? Parent => GetTarget(Universe.ChildOf);
public IEnumerable<EntityRef> GetChildren()
{
foreach (var iter in Iterator.FromTerm(Universe, new(Universe.ChildOf, this)))
for (var i = 0; i < iter.Count; i++)
yield return iter.Entity(i);
}
public override EntityRef Add(Identifier id) { ecs_add_id(Universe, this, id); return this; }
public override EntityRef Remove(Identifier id) { ecs_remove_id(Universe, this, id); return this; }
public override bool Has(Identifier id) => ecs_has_id(Universe, this, id);
@ -133,6 +145,13 @@ public unsafe sealed class EntityRef
return this;
}
public EntityRef? GetTarget(Entity relation, int index = 0)
=> CreateOrNull(Universe, new(ecs_get_target(Universe, this, relation, index)));
public EntityRef? GetTarget(string symbol, int index = 0)
=> GetTarget(Universe.LookupSymbolOrThrow(symbol), index);
public EntityRef? GetTarget<T>(int index = 0)
=> GetTarget(Universe.LookupOrThrow(typeof(T)), index);
public bool Equals(EntityRef? other) => (other is not null) && Universe == other.Universe && Entity == other.Entity;
public override bool Equals(object? obj) => Equals(obj as EntityRef);
public override int GetHashCode() => HashCode.Combine(Universe, Entity);

Loading…
Cancel
Save