From 0ddc89ddea19351980e7189872c999e54906d453 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Mon, 21 Mar 2022 16:21:42 -0700 Subject: [PATCH] add singleton getter --- src/ComponentDepot.cs | 10 ++++++++++ src/ComponentStorage.cs | 16 ++++++++++++++++ src/EntityComponentReader.cs | 14 ++++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/ComponentDepot.cs b/src/ComponentDepot.cs index 3afb8b6..02afb20 100644 --- a/src/ComponentDepot.cs +++ b/src/ComponentDepot.cs @@ -50,6 +50,11 @@ internal class ComponentDepot return ref Lookup().Get(entityID); } + public ref readonly TComponent Get() where TComponent : struct + { + return ref Lookup().Get(); + } + public void Set(int entityID, in TComponent component) where TComponent : struct { Lookup().Set(entityID, component); @@ -74,6 +79,11 @@ internal class ComponentDepot } } + public ref readonly Entity GetEntity() where TComponent : struct + { + return ref Lookup().FirstEntity(); + } + public ReadOnlySpan ReadEntities() where TComponent : struct { return Lookup().AllEntities(); diff --git a/src/ComponentStorage.cs b/src/ComponentStorage.cs index fcbd3a5..54ca698 100644 --- a/src/ComponentStorage.cs +++ b/src/ComponentStorage.cs @@ -29,6 +29,17 @@ internal class ComponentStorage : ComponentStorage where TComponent return ref components[entityIDToStorageIndex[entityID]]; } + public ref readonly TComponent Get() + { + #if DEBUG + if (nextID == 0) + { + throw new ArgumentOutOfRangeException("Component storage is empty!"); + } + #endif + return ref components[0]; + } + public void Set(int entityID, in TComponent component) { if (!entityIDToStorageIndex.ContainsKey(entityID)) @@ -87,4 +98,9 @@ internal class ComponentStorage : ComponentStorage where TComponent { return new ReadOnlySpan(components, 0, nextID); } + + public ref readonly Entity FirstEntity() + { + return ref storageIndexToEntities[0]; + } } diff --git a/src/EntityComponentReader.cs b/src/EntityComponentReader.cs index 2531109..f547518 100644 --- a/src/EntityComponentReader.cs +++ b/src/EntityComponentReader.cs @@ -37,8 +37,18 @@ public abstract class EntityComponentReader return ComponentDepot.Some(); } - protected TComponent Get(in Entity entity) where TComponent : struct + protected ref readonly TComponent Get(in Entity entity) where TComponent : struct { - return ComponentDepot.Get(entity.ID); + return ref ComponentDepot.Get(entity.ID); + } + + protected ref readonly TComponent Get() where TComponent : struct + { + return ref ComponentDepot.Get(); + } + + protected ref readonly Entity GetEntity() where TComponent : struct + { + return ref ComponentDepot.GetEntity(); } }