2022-04-08 05:52:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
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
|
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
private TypeIndices ComponentTypeIndices;
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
private ComponentStorage[] storages = new ComponentStorage[256];
|
2022-03-06 06:12:27 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +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)]
|
2023-01-10 00:41:00 +00:00
|
|
|
|
internal void Register<TComponent>(int index) where TComponent : unmanaged
|
2022-03-07 20:05:06 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
if (index >= storages.Length)
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +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
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
storages[index] = new ComponentStorage<TComponent>();
|
2022-04-08 05:52:03 +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
|
|
|
|
{
|
2023-01-10 00:41:00 +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)
|
2023-01-10 00:41:00 +00:00
|
|
|
|
{
|
|
|
|
|
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-05-03 04:51:11 +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
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
public ref readonly TComponent GetFirst<TComponent>() where TComponent : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +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
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
Lookup<TComponent>().Set(entityID, component);
|
2022-03-06 06:12:27 +00:00
|
|
|
|
}
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
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
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
public void Remove(int entityID, int storageIndex)
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +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
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
Lookup<TComponent>().Remove(entityID);
|
2022-03-05 02:01:44 +00:00
|
|
|
|
}
|
2022-03-06 06:12:27 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
public void Clear()
|
2022-03-06 06:12:27 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
for (var i = 0; i < ComponentTypeIndices.Count; i += 1)
|
2022-03-06 06:12:27 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
if (storages[i] != null)
|
2022-03-06 06:12:27 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +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
|
|
|
|
}
|
2022-04-02 08:00:48 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
// these methods used to implement snapshots, templates, and debugging
|
2022-04-02 08:00:48 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
internal unsafe void* UntypedGet(int entityID, int componentTypeIndex)
|
2022-03-06 06:12:27 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
return storages[componentTypeIndex].UntypedGet(entityID);
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
2022-03-25 19:32:35 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
internal unsafe void Set(int entityID, int componentTypeIndex, void* component)
|
2022-05-03 04:51:11 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
storages[componentTypeIndex].Set(entityID, component);
|
2022-05-03 04:51:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
public void CreateMissingStorages(ComponentDepot other)
|
2022-05-03 04:51:11 +00:00
|
|
|
|
{
|
2023-07-10 22:36:34 +00:00
|
|
|
|
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)
|
2022-05-03 04:51:11 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
if (storages[i] == null && other.storages[i] != null)
|
2022-05-03 04:51:11 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
storages[i] = other.storages[i].CreateStorage();
|
2022-05-03 04:51:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
// this method is used to iterate components of an entity, only for use with a debug inspector
|
2022-05-03 04:51:11 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
#if DEBUG
|
2023-01-10 00:41:00 +00:00
|
|
|
|
public object Debug_Get(int entityID, int componentTypeIndex)
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
return storages[componentTypeIndex].Debug_Get(entityID);
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
2022-03-25 19:32:35 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
public IEnumerable<int> Debug_GetEntityIDs(int componentTypeIndex)
|
2022-03-25 19:32:35 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +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
|
|
|
|
}
|