|
|
|
@ -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>(); |
|
|
|
|