From 21807908525dadcb5da9fad12e3b0086cc82f7a1 Mon Sep 17 00:00:00 2001 From: copygirl Date: Wed, 16 Nov 2022 22:13:51 +0100 Subject: [PATCH] Add EntityRef.CreateOrNull --- src/gaemstone/ECS/EntityRef.cs | 17 ++++++++++------- src/gaemstone/ECS/Universe+Lookup.cs | 12 ++++++------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/gaemstone/ECS/EntityRef.cs b/src/gaemstone/ECS/EntityRef.cs index d1315a0..55c58eb 100644 --- a/src/gaemstone/ECS/EntityRef.cs +++ b/src/gaemstone/ECS/EntityRef.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 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); diff --git a/src/gaemstone/ECS/Universe+Lookup.cs b/src/gaemstone/ECS/Universe+Lookup.cs index ef5a0bc..b38e018 100644 --- a/src/gaemstone/ECS/Universe+Lookup.cs +++ b/src/gaemstone/ECS/Universe+Lookup.cs @@ -9,15 +9,15 @@ public unsafe partial class Universe { private readonly Dictionary _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() => 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() => LookupOrThrow(typeof(T));