Add .Lookup and .MaybeGet

wip/source-generators
copygirl 2 years ago
parent cf6c050501
commit 1015fa4399
  1. 2
      src/gaemstone/ECS/EntityBase.cs
  2. 2
      src/gaemstone/ECS/EntityBuilder.cs
  3. 32
      src/gaemstone/ECS/EntityRef.cs

@ -11,6 +11,8 @@ public abstract class EntityBase<TReturn>
public abstract bool Has(Identifier id);
public abstract T Get<T>();
public abstract T? MaybeGet<T>() where T : unmanaged;
public abstract T? MaybeGet<T>(T _ = null!) where T : class;
public abstract ref T GetRef<T>() where T : unmanaged;
public abstract void Modified<T>();

@ -68,6 +68,8 @@ public class EntityBuilder
: throw new NotSupportedException(); // TODO: Support wildcard.
public override T Get<T>() => throw new NotSupportedException();
public override T? MaybeGet<T>() => throw new NotSupportedException();
public override T? MaybeGet<T>(T _ = null!) where T : class => throw new NotSupportedException();
public override ref T GetRef<T>() => throw new NotSupportedException();
public override void Modified<T>() => throw new NotImplementedException();

@ -19,14 +19,18 @@ public unsafe sealed class EntityRef
public string? Name {
get => ecs_get_name(Universe, this).FlecsToString()!;
set { using var alloc = TempAllocator.Use(); ecs_set_name(Universe, this, alloc.AllocateCString(value)); }
set { using var alloc = TempAllocator.Use();
ecs_set_name(Universe, this, alloc.AllocateCString(value)); }
}
public string? Symbol {
get => ecs_get_symbol(Universe, this).FlecsToString()!;
set { using var alloc = TempAllocator.Use(); ecs_set_symbol(Universe, this, alloc.AllocateCString(value)); }
set { using var alloc = TempAllocator.Use();
ecs_set_symbol(Universe, this, alloc.AllocateCString(value)); }
}
// TODO: public IEnumerable<Entity> Children => ...
// public IEnumerable<Entity> Children { get {
//
// } }
public EntityRef(Universe universe, Entity entity, bool throwOnInvalid = true)
{
@ -41,6 +45,11 @@ public unsafe sealed class EntityRef
public EntityBuilder NewChild(EntityPath? path = null)
=> Universe.New(EnsureRelativePath(path)).ChildOf(this);
public EntityRef? Lookup(EntityPath path)
=> Universe.Lookup(this, EnsureRelativePath(path)!);
public EntityRef LookupOrThrow(EntityPath path)
=> Universe.LookupOrThrow(this, EnsureRelativePath(path)!);
private static EntityPath? EnsureRelativePath(EntityPath? path)
{ if (path?.IsAbsolute == true) throw new ArgumentException("path must not be absolute", nameof(path)); return path; }
@ -53,10 +62,25 @@ public unsafe sealed class EntityRef
var comp = Universe.LookupOrThrow<T>();
var ptr = ecs_get_id(Universe, this, comp);
if (ptr == null) throw new Exception($"Component {typeof(T)} not found on {this}");
return (typeof(T).IsValueType) ? Unsafe.Read<T>(ptr)
return typeof(T).IsValueType ? Unsafe.Read<T>(ptr)
: (T)((GCHandle)Unsafe.Read<nint>(ptr)).Target!;
}
public override T? MaybeGet<T>()
{
var comp = Universe.LookupOrThrow<T>();
var ptr = ecs_get_id(Universe, this, comp);
return (ptr != null) ? Unsafe.Read<T>(ptr) : null;
}
public override T? MaybeGet<T>(T _ = null!)
where T : class
{
var comp = Universe.LookupOrThrow<T>();
var ptr = ecs_get_id(Universe, this, comp);
return (ptr != null) ? (T)((GCHandle)Unsafe.Read<nint>(ptr)).Target! : null;
}
public override ref T GetRef<T>()
{
var comp = Universe.LookupOrThrow<T>();

Loading…
Cancel
Save