add singleton getter

pull/1/head
cosmonaut 2022-03-21 16:21:42 -07:00
parent 9eb4b947f0
commit 0ddc89ddea
3 changed files with 38 additions and 2 deletions

View File

@ -50,6 +50,11 @@ internal class ComponentDepot
return ref Lookup<TComponent>().Get(entityID);
}
public ref readonly TComponent Get<TComponent>() where TComponent : struct
{
return ref Lookup<TComponent>().Get();
}
public void Set<TComponent>(int entityID, in TComponent component) where TComponent : struct
{
Lookup<TComponent>().Set(entityID, component);
@ -74,6 +79,11 @@ internal class ComponentDepot
}
}
public ref readonly Entity GetEntity<TComponent>() where TComponent : struct
{
return ref Lookup<TComponent>().FirstEntity();
}
public ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct
{
return Lookup<TComponent>().AllEntities();

View File

@ -29,6 +29,17 @@ internal class ComponentStorage<TComponent> : 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<TComponent> : ComponentStorage where TComponent
{
return new ReadOnlySpan<TComponent>(components, 0, nextID);
}
public ref readonly Entity FirstEntity()
{
return ref storageIndexToEntities[0];
}
}

View File

@ -37,8 +37,18 @@ public abstract class EntityComponentReader
return ComponentDepot.Some<TComponent>();
}
protected TComponent Get<TComponent>(in Entity entity) where TComponent : struct
protected ref readonly TComponent Get<TComponent>(in Entity entity) where TComponent : struct
{
return ComponentDepot.Get<TComponent>(entity.ID);
return ref ComponentDepot.Get<TComponent>(entity.ID);
}
protected ref readonly TComponent Get<TComponent>() where TComponent : struct
{
return ref ComponentDepot.Get<TComponent>();
}
protected ref readonly Entity GetEntity<TComponent>() where TComponent : struct
{
return ref ComponentDepot.GetEntity<TComponent>();
}
}