2022-04-08 05:52:03 +00:00
|
|
|
|
using System;
|
2022-08-09 21:41:31 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2022-05-03 04:51:11 +00:00
|
|
|
|
|
2022-08-09 21:41:31 +00:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-12-06 01:46:18 +00:00
|
|
|
|
internal void Register<TComponent>(int index) where TComponent : unmanaged
|
2022-03-07 20:05:06 +00:00
|
|
|
|
{
|
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-07 20:05:06 +00:00
|
|
|
|
}
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2022-08-09 21:41:31 +00:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-05-03 04:51:11 +00:00
|
|
|
|
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
|
|
|
|
|
2022-05-03 04:51:11 +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-06 01:46:18 +00:00
|
|
|
|
// used for debugging and template instantiation
|
|
|
|
|
internal object UntypedGet(int entityID, int componentTypeIndex)
|
2022-03-05 02:01:44 +00:00
|
|
|
|
{
|
2022-12-06 01:46:18 +00:00
|
|
|
|
return storages[componentTypeIndex].UntypedGet(entityID);
|
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
|
|
|
|
|
2022-05-03 04:51:11 +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
|
|
|
|
|
2022-12-06 01:46:18 +00:00
|
|
|
|
internal void Set(int entityID, int componentTypeIndex, object component)
|
|
|
|
|
{
|
|
|
|
|
storages[componentTypeIndex].Set(entityID, component);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 04:51:11 +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
|
|
|
|
|
2022-05-03 04:51:11 +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);
|
2022-03-07 06:43:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
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-03-25 19:32:35 +00:00
|
|
|
|
}
|
2022-03-05 02:01:44 +00:00
|
|
|
|
}
|