Add EntityRef.CreateOrNull

wip/source-generators
copygirl 2 years ago
parent 14a70f5038
commit 2180790852
  1. 17
      src/gaemstone/ECS/EntityRef.cs
  2. 12
      src/gaemstone/ECS/Universe+Lookup.cs

@ -14,7 +14,6 @@ public unsafe sealed class EntityRef
public Entity Entity { get; }
public uint ID => Entity.ID;
public bool IsValid => ecs_is_valid(Universe, this);
public bool IsAlive => ecs_is_alive(Universe, this);
public EntityType Type => new(Universe, ecs_get_type(Universe, this));
@ -29,18 +28,22 @@ public unsafe sealed class EntityRef
ecs_set_symbol(Universe, this, alloc.AllocateCString(value)); }
}
// public IEnumerable<Entity> Children { get {
//
// } }
public EntityRef(Universe universe, Entity entity, bool throwOnInvalid = true)
private EntityRef(Universe universe, Entity entity, bool throwOnInvalid)
{
if (throwOnInvalid && !ecs_is_valid(universe, entity))
throw new InvalidOperationException($"The entity {entity} is not valid");
Universe = universe;
Entity = entity;
if (throwOnInvalid && !IsValid) throw new InvalidOperationException(
$"The entity {entity} is not valid");
}
public EntityRef(Universe universe, Entity entity)
: this(universe, entity, true) { }
public static EntityRef? CreateOrNull(Universe universe, Entity entity)
=> ecs_is_valid(universe, entity) ? new(universe, entity) : null;
public void Delete()
=> ecs_delete(Universe, this);

@ -9,15 +9,15 @@ public unsafe partial class Universe
{
private readonly Dictionary<Type, Entity> _lookupByType = new();
public void AddLookupByType(Type type, Entity entity)
=> _lookupByType.Add(type, entity);
public void RemoveLookupByType(Type type)
{ if (!_lookupByType.Remove(type)) throw new InvalidOperationException(
$"Lookup for {type} does not exist"); }
private EntityRef? GetOrNull(Entity entity)
{ var e = new EntityRef(this, entity, false); return e.IsValid ? e : null; }
private EntityRef? CreateOrNull(Entity entity)
=> EntityRef.CreateOrNull(this, entity);
public EntityRef? Lookup<T>()
=> Lookup(typeof(T));
@ -25,16 +25,16 @@ public unsafe partial class Universe
=> Lookup(_lookupByType.GetValueOrDefault(type));
public EntityRef? Lookup(Entity value)
=> GetOrNull(new(ecs_get_alive(this, value)));
=> CreateOrNull(new(ecs_get_alive(this, value)));
public EntityRef? Lookup(EntityPath path)
=> Lookup(default, path);
public EntityRef? Lookup(Entity parent, EntityPath path)
=> GetOrNull(EntityPath.Lookup(this, parent, path));
=> CreateOrNull(EntityPath.Lookup(this, parent, path));
public EntityRef? LookupSymbol(string symbol)
{
using var alloc = TempAllocator.Use();
return GetOrNull(new(ecs_lookup_symbol(this, alloc.AllocateCString(symbol), false)));
return CreateOrNull(new(ecs_lookup_symbol(this, alloc.AllocateCString(symbol), false)));
}
public EntityRef LookupOrThrow<T>() => LookupOrThrow(typeof(T));

Loading…
Cancel
Save