|
|
@ -8,10 +8,9 @@ using static flecs_hub.flecs; |
|
|
|
namespace gaemstone.ECS; |
|
|
|
namespace gaemstone.ECS; |
|
|
|
|
|
|
|
|
|
|
|
public unsafe class EntityRef |
|
|
|
public unsafe class EntityRef |
|
|
|
: EntityBase<EntityRef> |
|
|
|
: IEquatable<EntityRef> |
|
|
|
, IEquatable<EntityRef> |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
public override World World { get; } |
|
|
|
public World World { get; } |
|
|
|
public Entity Entity { get; } |
|
|
|
public Entity Entity { get; } |
|
|
|
public uint Id => Entity.Id; |
|
|
|
public uint Id => Entity.Id; |
|
|
|
|
|
|
|
|
|
|
@ -74,25 +73,43 @@ public unsafe class EntityRef |
|
|
|
yield return iter.Entity(i); |
|
|
|
yield return iter.Entity(i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public EntityRef ChildOf(Entity parent) |
|
|
|
|
|
|
|
=> Add(World.ChildOf, parent); |
|
|
|
|
|
|
|
|
|
|
|
public override EntityRef Add(Id id) { ecs_add_id(World, this, id); return this; } |
|
|
|
|
|
|
|
public override EntityRef Remove(Id id) { ecs_remove_id(World, this, id); return this; } |
|
|
|
|
|
|
|
public override bool Has(Id id) => ecs_has_id(World, this, id); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override T? GetOrNull<T>(Id id) |
|
|
|
public bool IsDisabled => Has(World.Disabled); |
|
|
|
|
|
|
|
public EntityRef Disable() => Add(World.Disabled); |
|
|
|
|
|
|
|
public EntityRef Enable() => Remove(World.Disabled); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public EntityRef Add(Id id) { ecs_add_id(World, this, id); return this; } |
|
|
|
|
|
|
|
public EntityRef Add(string symbol) => Add(World.LookupBySymbolOrThrow(symbol)); |
|
|
|
|
|
|
|
public EntityRef Add(Entity relation, Entity target) => Add(ECS.Id.Pair(relation, target)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public EntityRef Remove(Id id) { ecs_remove_id(World, this, id); return this; } |
|
|
|
|
|
|
|
public EntityRef Remove(string symbol) => Remove(World.LookupBySymbolOrThrow(symbol)); |
|
|
|
|
|
|
|
public EntityRef Remove(Entity relation, Entity target) => Remove(ECS.Id.Pair(relation, target)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool Has(Id id) => ecs_has_id(World, this, id); |
|
|
|
|
|
|
|
public bool Has(string symbol) => Has(World.LookupBySymbolOrThrow(symbol)); |
|
|
|
|
|
|
|
public bool Has(Entity relation, Entity target) => Has(ECS.Id.Pair(relation, target)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public T? GetOrNull<T>(Id id) |
|
|
|
|
|
|
|
where T : unmanaged |
|
|
|
{ |
|
|
|
{ |
|
|
|
var ptr = ecs_get_id(World, this, id); |
|
|
|
var ptr = ecs_get_id(World, this, id); |
|
|
|
return (ptr != null) ? Unsafe.Read<T>(ptr) : null; |
|
|
|
return (ptr != null) ? Unsafe.Read<T>(ptr) : null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override T? GetOrNull<T>(Id id, T _ = null!) |
|
|
|
public T? GetOrNull<T>(Id id, T _ = null!) |
|
|
|
where T : class
|
|
|
|
where T : class
|
|
|
|
{ |
|
|
|
{ |
|
|
|
var ptr = ecs_get_id(World, this, id); |
|
|
|
var ptr = ecs_get_id(World, this, id); |
|
|
|
return (T?)Unsafe.Read<ReferenceHandle>(ptr).Target; |
|
|
|
return (T?)Unsafe.Read<ReferenceHandle>(ptr).Target; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override T GetOrThrow<T>(Id id) |
|
|
|
public T GetOrThrow<T>(Id id) |
|
|
|
{ |
|
|
|
{ |
|
|
|
var ptr = ecs_get_id(World, this, id); |
|
|
|
var ptr = ecs_get_id(World, this, id); |
|
|
|
if (ptr == null) throw new Exception($"Component {typeof(T)} not found on {this}"); |
|
|
|
if (ptr == null) throw new Exception($"Component {typeof(T)} not found on {this}"); |
|
|
@ -100,7 +117,16 @@ public unsafe class EntityRef |
|
|
|
else return (T)Unsafe.Read<ReferenceHandle>(ptr).Target!; |
|
|
|
else return (T)Unsafe.Read<ReferenceHandle>(ptr).Target!; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override ref T GetRefOrNull<T>(Id id) |
|
|
|
public ref T GetMut<T>(Id id) |
|
|
|
|
|
|
|
where T : unmanaged |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var ptr = ecs_get_mut_id(World, this, id); |
|
|
|
|
|
|
|
// NOTE: Value is added if it doesn't exist on the entity. |
|
|
|
|
|
|
|
return ref Unsafe.AsRef<T>(ptr); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ref T GetRefOrNull<T>(Id id) |
|
|
|
|
|
|
|
where T : unmanaged |
|
|
|
{ |
|
|
|
{ |
|
|
|
var @ref = ecs_ref_init_id(World, this, id); |
|
|
|
var @ref = ecs_ref_init_id(World, this, id); |
|
|
|
var ptr = ecs_ref_get_id(World, &@ref, id); |
|
|
|
var ptr = ecs_ref_get_id(World, &@ref, id); |
|
|
@ -108,7 +134,8 @@ public unsafe class EntityRef |
|
|
|
: ref Unsafe.NullRef<T>(); |
|
|
|
: ref Unsafe.NullRef<T>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override ref T GetRefOrThrow<T>(Id id) |
|
|
|
public ref T GetRefOrThrow<T>(Id id) |
|
|
|
|
|
|
|
where T : unmanaged |
|
|
|
{ |
|
|
|
{ |
|
|
|
ref var ptr = ref GetRefOrNull<T>(id); |
|
|
|
ref var ptr = ref GetRefOrNull<T>(id); |
|
|
|
if (Unsafe.IsNullRef(ref ptr)) throw new Exception( |
|
|
|
if (Unsafe.IsNullRef(ref ptr)) throw new Exception( |
|
|
@ -116,17 +143,12 @@ public unsafe class EntityRef |
|
|
|
return ref ptr; |
|
|
|
return ref ptr; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override ref T GetMut<T>(Id id) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var ptr = ecs_get_mut_id(World, this, id); |
|
|
|
|
|
|
|
// NOTE: Value is added if it doesn't exist on the entity. |
|
|
|
|
|
|
|
return ref Unsafe.AsRef<T>(ptr); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void Modified<T>(Id id) |
|
|
|
public void Modified<T>(Id id) |
|
|
|
=> ecs_modified_id(World, this, id); |
|
|
|
=> ecs_modified_id(World, this, id); |
|
|
|
|
|
|
|
|
|
|
|
public override EntityRef Set<T>(Id id, in T value) |
|
|
|
public EntityRef Set<T>(Id id, in T value) |
|
|
|
|
|
|
|
where T : unmanaged |
|
|
|
{ |
|
|
|
{ |
|
|
|
var size = (ulong)Unsafe.SizeOf<T>(); |
|
|
|
var size = (ulong)Unsafe.SizeOf<T>(); |
|
|
|
fixed (T* ptr = &value) |
|
|
|
fixed (T* ptr = &value) |
|
|
@ -135,7 +157,8 @@ public unsafe class EntityRef |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public override EntityRef Set<T>(Id id, T obj) where T : class
|
|
|
|
public EntityRef Set<T>(Id id, T obj) |
|
|
|
|
|
|
|
where T : class
|
|
|
|
{ |
|
|
|
{ |
|
|
|
if (obj == null) throw new ArgumentNullException(nameof(obj)); |
|
|
|
if (obj == null) throw new ArgumentNullException(nameof(obj)); |
|
|
|
var size = (ulong)sizeof(ReferenceHandle); |
|
|
|
var size = (ulong)sizeof(ReferenceHandle); |
|
|
@ -146,6 +169,7 @@ public unsafe class EntityRef |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private EntityRef? GetTarget(Entity relation, int index) |
|
|
|
private EntityRef? GetTarget(Entity relation, int index) |
|
|
|
=> CreateOrNull(World, new(ecs_get_target(World, this, relation, index))); |
|
|
|
=> CreateOrNull(World, new(ecs_get_target(World, this, relation, index))); |
|
|
|
public IEnumerable<EntityRef> GetTargets(Entity relation) |
|
|
|
public IEnumerable<EntityRef> GetTargets(Entity relation) |
|
|
|