|
|
|
using System;
|
|
|
|
using static flecs_hub.flecs;
|
|
|
|
|
|
|
|
namespace gaemstone.ECS;
|
|
|
|
|
|
|
|
public readonly struct Id
|
|
|
|
: IEquatable<Id>
|
|
|
|
{
|
|
|
|
public readonly ecs_id_t Value;
|
|
|
|
public IdFlags Flags => (IdFlags)(Value & ECS_ID_FLAGS_MASK);
|
|
|
|
|
|
|
|
public bool IsPair => ecs_id_is_pair(this);
|
|
|
|
public bool IsWildcard => ecs_id_is_wildcard(this);
|
|
|
|
|
|
|
|
public Entity RelationUnsafe => new(new() { Data = (Value & ECS_COMPONENT_MASK) >> 32 });
|
|
|
|
public Entity TargetUnsafe => new(new() { Data = Value & ECS_ENTITY_MASK });
|
|
|
|
|
|
|
|
public Id(ecs_id_t value) => Value = value;
|
|
|
|
|
|
|
|
public static Id Combine(IdFlags flags, Id id)
|
|
|
|
=> new((ulong)flags | id.Value);
|
|
|
|
|
|
|
|
public static Id Pair(Entity relation, Entity target)
|
|
|
|
=> Combine(IdFlags.Pair, new(
|
|
|
|
((relation.Value.Data << 32) & ECS_COMPONENT_MASK) |
|
|
|
|
( target.Value.Data & ECS_ENTITY_MASK )));
|
|
|
|
|
|
|
|
public bool Equals(Id other) => Value.Data == other.Value.Data;
|
|
|
|
public override bool Equals(object? obj) => (obj is Id other) && Equals(other);
|
|
|
|
public override int GetHashCode() => Value.Data.GetHashCode();
|
|
|
|
public override string? ToString()
|
|
|
|
=> (Flags != default) ? $"Id({Value.Data}, Flags={Flags})"
|
|
|
|
: $"Id({Value.Data})";
|
|
|
|
|
|
|
|
public static bool operator ==(Id left, Id right) => left.Equals(right);
|
|
|
|
public static bool operator !=(Id left, Id right) => !left.Equals(right);
|
|
|
|
|
|
|
|
public static implicit operator ecs_id_t(Id i) => i.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
public enum IdFlags : ulong
|
|
|
|
{
|
|
|
|
Pair = 1ul << 63,
|
|
|
|
Override = 1ul << 62,
|
|
|
|
Toggle = 1ul << 61,
|
|
|
|
And = 1ul << 60,
|
|
|
|
}
|