MoonTools.ECS/src/ComponentDepot.cs

130 lines
3.3 KiB
C#
Raw Normal View History

2022-04-08 05:52:03 +00:00
using System;
2022-12-07 21:53:23 +00:00
using System.Collections.Generic;
using System.Runtime.CompilerServices;
2022-03-05 02:01:44 +00:00
2022-04-08 05:52:03 +00:00
namespace MoonTools.ECS
2022-03-05 02:01:44 +00:00
{
2022-04-08 05:52:03 +00:00
internal class ComponentDepot
{
2022-12-03 07:43:54 +00:00
private TypeIndices ComponentTypeIndices;
2022-03-05 02:01:44 +00:00
2022-12-03 07:43:54 +00:00
private ComponentStorage[] storages = new ComponentStorage[256];
2022-03-06 06:12:27 +00:00
2022-12-03 07:43:54 +00:00
public ComponentDepot(TypeIndices componentTypeIndices)
{
ComponentTypeIndices = componentTypeIndices;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2022-12-06 01:46:18 +00:00
internal void Register<TComponent>(int index) where TComponent : unmanaged
{
2022-12-03 07:43:54 +00:00
if (index >= storages.Length)
2022-04-08 05:52:03 +00:00
{
2022-12-03 07:43:54 +00:00
Array.Resize(ref storages, storages.Length * 2);
2022-04-08 05:52:03 +00:00
}
2022-12-03 07:43:54 +00:00
storages[index] = new ComponentStorage<TComponent>();
}
2022-03-05 02:01:44 +00:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private ComponentStorage<TComponent> Lookup<TComponent>() where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
2022-12-03 07:43:54 +00:00
var storageIndex = ComponentTypeIndices.GetIndex<TComponent>();
// TODO: is there some way to avoid this null check?
if (storages[storageIndex] == null)
{
Register<TComponent>(storageIndex);
}
return (ComponentStorage<TComponent>) storages[storageIndex];
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
public bool Some<TComponent>() where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
return Lookup<TComponent>().Any();
}
2022-03-05 02:01:44 +00:00
2022-12-03 07:43:54 +00:00
public ref readonly TComponent Get<TComponent>(int entityID) where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
2022-12-03 07:43:54 +00:00
return ref Lookup<TComponent>().Get(entityID);
2022-04-08 05:52:03 +00:00
}
2022-03-05 02:01:44 +00:00
2022-12-03 07:43:54 +00:00
public ref readonly TComponent GetFirst<TComponent>() where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
2022-12-03 07:43:54 +00:00
return ref Lookup<TComponent>().GetFirst();
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
public void Set<TComponent>(int entityID, in TComponent component) where TComponent : unmanaged
2022-03-06 06:12:27 +00:00
{
2022-12-03 07:43:54 +00:00
Lookup<TComponent>().Set(entityID, component);
2022-03-06 06:12:27 +00:00
}
2022-03-05 02:01:44 +00:00
public Entity GetSingletonEntity<TComponent>() where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
return Lookup<TComponent>().FirstEntity();
}
2022-03-05 02:01:44 +00:00
public ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
return Lookup<TComponent>().AllComponents();
}
2022-03-05 02:01:44 +00:00
2022-12-03 07:43:54 +00:00
public void Remove(int entityID, int storageIndex)
2022-04-08 05:52:03 +00:00
{
2022-12-03 07:43:54 +00:00
storages[storageIndex].Remove(entityID);
}
public void Remove<TComponent>(int entityID) where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
2022-12-03 07:43:54 +00:00
Lookup<TComponent>().Remove(entityID);
2022-04-08 05:52:03 +00:00
}
2022-12-06 09:59:22 +00:00
public void Clear()
{
for (var i = 0; i < ComponentTypeIndices.Count; i += 1)
{
if (storages[i] != null)
{
storages[i].Clear();
}
}
}
2022-12-07 06:06:02 +00:00
// these methods used to implement snapshots, templates, and debugging
2022-12-07 21:53:23 +00:00
internal unsafe void* UntypedGet(int entityID, int componentTypeIndex)
2022-12-07 06:06:02 +00:00
{
return storages[componentTypeIndex].UntypedGet(entityID);
}
2022-12-07 21:53:23 +00:00
internal unsafe void Set(int entityID, int componentTypeIndex, void* component)
2022-12-07 06:06:02 +00:00
{
storages[componentTypeIndex].Set(entityID, component);
}
public void CreateMissingStorages(ComponentDepot other)
2022-12-06 09:59:22 +00:00
{
for (var i = 0; i < ComponentTypeIndices.Count; i += 1)
{
if (storages[i] == null && other.storages[i] != null)
{
storages[i] = other.storages[i].CreateStorage();
}
}
}
2022-12-07 21:53:23 +00:00
// this method is used to iterate components of an entity, only for use with a debug inspector
#if DEBUG
public object Debug_Get(int entityID, int componentTypeIndex)
{
return storages[componentTypeIndex].Debug_Get(entityID);
}
public IEnumerable<int> Debug_GetEntityIDs(int componentTypeIndex)
{
return storages[componentTypeIndex].Debug_GetEntityIDs();
}
#endif
2022-03-25 19:32:35 +00:00
}
2022-03-05 02:01:44 +00:00
}