Alternative managed wrapper around flecs-cs bindings for using the ECS framework Flecs in modern .NET.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
2.0 KiB

using System;
using gaemstone.Utility;
using static flecs_hub.flecs;
namespace gaemstone.ECS;
public unsafe class IdentifierRef
: IEquatable<IdentifierRef>
{
public World World { 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(World, this);
public bool IsInUse => ecs_id_in_use(World, this);
public IdentifierRef(World world, Identifier id)
{ World = world; Id = id; }
public static IdentifierRef Combine(IdentifierFlags flags, IdentifierRef id)
=> new(id.World, Identifier.Combine(flags, id));
public static IdentifierRef Pair(EntityRef relation, Entity target)
=> new(relation.World, Identifier.Pair(relation, target));
public static IdentifierRef Pair(Entity relation, EntityRef target)
=> new(target.World, Identifier.Pair(relation, target));
public EntityRef? AsEntity()
=> (Flags == default) ? World.Lookup(new Entity(new() { Data = Id })) : null;
public (EntityRef Relation, EntityRef Target)? AsPair()
=> IsPair && (World.Lookup(Id.RelationUnsafe) is EntityRef relation) &&
(World.Lookup(Id.TargetUnsafe ) is EntityRef target )
? (relation, target) : null;
public bool Equals(IdentifierRef? other) => (other is not null) && (World == other.World) && (Id == other.Id);
public override bool Equals(object? obj) => Equals(obj as IdentifierRef);
public override int GetHashCode() => HashCode.Combine(World, Id);
public override string? ToString() => ecs_id_str(World, 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;
}