|
|
|
using System;
|
|
|
|
using gaemstone.Utility;
|
|
|
|
using static flecs_hub.flecs;
|
|
|
|
|
|
|
|
namespace gaemstone.ECS;
|
|
|
|
|
|
|
|
public unsafe class IdentifierRef
|
|
|
|
: IEquatable<IdentifierRef>
|
|
|
|
{
|
|
|
|
public Universe Universe { get; }
|
|
|
|
public Identifier ID { get; }
|
|
|
|
|
|
|
|
public IdentifierFlags Flags => ID.Flags;
|
|
|
|
public bool IsPair => ID.IsPair;
|
|
|
|
public bool IsWildcard => ID.IsWildcard;
|
|
|
|
public bool IsValid => ecs_id_is_valid(Universe, ID);
|
|
|
|
|
|
|
|
public IdentifierRef(Universe universe, Identifier id)
|
|
|
|
{ Universe = universe; ID = id; }
|
|
|
|
|
|
|
|
public static IdentifierRef Combine(IdentifierFlags flags, IdentifierRef id)
|
|
|
|
=> new(id.Universe, Identifier.Combine(flags, id));
|
|
|
|
public static IdentifierRef Pair(EntityRef relation, Entity target)
|
|
|
|
=> new(relation.Universe, Identifier.Pair(relation, target));
|
|
|
|
public static IdentifierRef Pair(Entity relation, EntityRef target)
|
|
|
|
=> new(target.Universe, Identifier.Pair(relation, target));
|
|
|
|
|
|
|
|
public (EntityRef Relation, EntityRef Target)? AsPair()
|
|
|
|
=> IsPair ? (Universe.LookupOrThrow(ID.RelationUnsafe), Universe.LookupOrThrow(ID.TargetUnsafe)) : null;
|
|
|
|
|
|
|
|
public bool Equals(IdentifierRef? other) => (other is not null) && (Universe == other.Universe) && (ID == other.ID);
|
|
|
|
public override bool Equals(object? obj) => Equals(obj as IdentifierRef);
|
|
|
|
public override int GetHashCode() => HashCode.Combine(Universe, ID);
|
|
|
|
public override string? ToString() => ecs_id_str(Universe, this).FlecsToStringAndFree()!;
|
|
|
|
|
|
|
|
public static bool operator ==(IdentifierRef? left, IdentifierRef? right) => ReferenceEquals(left, right) || (left?.Equals(right) ?? false);
|
|
|
|
public static bool operator !=(IdentifierRef? left, IdentifierRef? right) => !(left == right);
|
|
|
|
|
|
|
|
public static implicit operator Identifier(IdentifierRef i) => i.ID;
|
|
|
|
public static implicit operator ecs_id_t(IdentifierRef i) => i.ID.Value;
|
|
|
|
}
|