MoonTools.ECS/src/ComponentDepot.cs

140 lines
3.6 KiB
C#
Raw Normal View History

2022-04-08 05:52:03 +00:00
using System;
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
{
private TypeIndices ComponentTypeIndices;
2022-03-05 02:01:44 +00:00
private ComponentStorage[] storages = new ComponentStorage[256];
2022-03-06 06:12:27 +00:00
public ComponentDepot(TypeIndices componentTypeIndices)
{
ComponentTypeIndices = componentTypeIndices;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Register<TComponent>(int index) where TComponent : unmanaged
{
if (index >= storages.Length)
2022-04-08 05:52:03 +00:00
{
Array.Resize(ref storages, storages.Length * 2);
2022-04-08 05:52:03 +00:00
}
2022-03-05 02:01:44 +00:00
storages[index] = new ComponentStorage<TComponent>();
2022-04-08 05:52:03 +00:00
}
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
{
var storageIndex = ComponentTypeIndices.GetIndex<TComponent>();
// TODO: is there some way to avoid this null check?
2023-03-21 22:31:59 +00:00
if (storageIndex >= storages.Length || 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
public ref readonly TComponent Get<TComponent>(int entityID) where TComponent : unmanaged
2022-03-05 02:01:44 +00:00
{
2022-04-08 05:52:03 +00:00
return ref Lookup<TComponent>().Get(entityID);
2022-03-05 02:01:44 +00:00
}
public ref readonly TComponent GetFirst<TComponent>() where TComponent : unmanaged
2022-04-08 05:52:03 +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
{
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
public void Remove(int entityID, int storageIndex)
2022-04-08 05:52:03 +00:00
{
storages[storageIndex].Remove(entityID);
}
public void Remove<TComponent>(int entityID) where TComponent : unmanaged
2022-04-08 05:52:03 +00:00
{
Lookup<TComponent>().Remove(entityID);
2022-03-05 02:01:44 +00:00
}
2022-03-06 06:12:27 +00:00
public void Clear()
2022-03-06 06:12:27 +00:00
{
2023-07-27 21:53:33 +00:00
for (var i = 0; i < storages.Length; i += 1)
2022-03-06 06:12:27 +00:00
{
if (storages[i] != null)
2022-03-06 06:12:27 +00:00
{
storages[i].Clear();
2022-04-08 05:52:03 +00:00
}
2022-03-06 06:12:27 +00:00
}
2022-04-08 05:52:03 +00:00
}
// these methods used to implement transfers and debugging
internal unsafe void* UntypedGet(int entityID, int componentTypeIndex)
2022-03-06 06:12:27 +00:00
{
return storages[componentTypeIndex].UntypedGet(entityID);
2022-04-08 05:52:03 +00:00
}
2022-03-25 19:32:35 +00:00
internal unsafe void Set(int entityID, int componentTypeIndex, void* component)
{
storages[componentTypeIndex].Set(entityID, component);
}
public void CreateMissingStorages(ComponentDepot other)
{
while (other.ComponentTypeIndices.Count >= storages.Length)
{
Array.Resize(ref storages, storages.Length * 2);
}
while (other.ComponentTypeIndices.Count >= other.storages.Length)
{
Array.Resize(ref other.storages, other.storages.Length * 2);
}
for (var i = 0; i < other.ComponentTypeIndices.Count; i += 1)
{
if (storages[i] == null && other.storages[i] != null)
{
storages[i] = other.storages[i].CreateStorage();
}
}
}
// this method is used to iterate components of an entity, only for use with a debug inspector
2022-04-08 05:52:03 +00:00
#if DEBUG
public object Debug_Get(int entityID, int componentTypeIndex)
2022-04-08 05:52:03 +00:00
{
return storages[componentTypeIndex].Debug_Get(entityID);
2022-04-08 05:52:03 +00:00
}
2022-03-25 19:32:35 +00:00
public IEnumerable<int> Debug_GetEntityIDs(int componentTypeIndex)
2022-03-25 19:32:35 +00:00
{
return storages[componentTypeIndex].Debug_GetEntityIDs();
2022-03-25 19:32:35 +00:00
}
2022-04-08 05:52:03 +00:00
#endif
2022-03-25 19:32:35 +00:00
}
2022-03-05 02:01:44 +00:00
}