From 9eade0adb1200223dffcb2077ecbc992569f4080 Mon Sep 17 00:00:00 2001 From: copygirl Date: Sat, 6 May 2023 21:22:51 +0200 Subject: [PATCH] Add Entity.Valid/AliveOrNull/Throw --- src/gaemstone.ECS/Entity.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/gaemstone.ECS/Entity.cs b/src/gaemstone.ECS/Entity.cs index 11a008f..28ae86d 100644 --- a/src/gaemstone.ECS/Entity.cs +++ b/src/gaemstone.ECS/Entity.cs @@ -39,9 +39,17 @@ public unsafe readonly partial struct Entity public static Entity GetOrInvalid(World world, Entity value) => new(world, value); public static Entity? GetOrNull(World world, Entity value) - => ecs_is_valid(world, value) ? new(world, value) : null; + => new Entity(world, value).ValidOrNull(); public static Entity GetOrThrow(World world, Entity value) - => ecs_is_valid(world, value) ? new(world, value) : throw new InvalidOperationException($"The entity {value} is not valid"); + => new Entity(world, value).ValidOrThrow(); + + public Entity? ValidOrNull() => IsValid ? this : null; + public Entity ValidOrThrow() => IsValid ? this + : throw new InvalidOperationException($"The entity {this} is not valid"); + + public Entity? AliveOrNull() => IsAlive ? this : null; + public Entity AliveOrThrow() => IsAlive ? this + : throw new InvalidOperationException($"The entity {this} is not alive"); public void Delete() => ecs_delete(World, this);