MoonTools.ECS/src/Rev2/World.cs

388 lines
12 KiB
C#
Raw Normal View History

2023-10-19 22:41:49 +00:00
using System;
using System.Collections.Generic;
2023-10-20 00:41:45 +00:00
using System.Runtime.CompilerServices;
2023-10-21 00:24:35 +00:00
using System.Runtime.InteropServices;
2023-10-19 22:41:49 +00:00
namespace MoonTools.ECS.Rev2
{
2023-10-20 00:41:45 +00:00
public class World : IDisposable
2023-10-19 22:41:49 +00:00
{
2023-10-25 01:44:41 +00:00
// Get ComponentId from a Type
internal static Dictionary<Type, ComponentId> TypeToComponentId = new Dictionary<Type, ComponentId>();
// Get element size from a ComponentId
internal static Dictionary<ComponentId, int> ElementSizes = new Dictionary<ComponentId, int>();
2023-10-19 22:41:49 +00:00
// Lookup from ArchetypeSignature to Archetype
2023-10-25 01:44:41 +00:00
internal Dictionary<ArchetypeSignature, Archetype> ArchetypeIndex = new Dictionary<ArchetypeSignature, Archetype>();
2023-10-19 22:41:49 +00:00
// Going from EntityId to Archetype and storage row
Dictionary<EntityId, Record> EntityIndex = new Dictionary<EntityId, Record>();
2023-10-21 00:24:35 +00:00
// Going from ComponentId to Archetype list
Dictionary<ComponentId, List<Archetype>> ComponentIndex = new Dictionary<ComponentId, List<Archetype>>();
2023-10-19 22:41:49 +00:00
// ID Management
IdAssigner<EntityId> EntityIdAssigner = new IdAssigner<EntityId>();
IdAssigner<ComponentId> ComponentIdAssigner = new IdAssigner<ComponentId>();
2023-10-25 01:44:41 +00:00
internal readonly Archetype EmptyArchetype;
public FilterBuilder FilterBuilder => new FilterBuilder(this);
2023-10-24 20:13:14 +00:00
2023-10-20 00:41:45 +00:00
private bool IsDisposed;
2023-10-20 08:17:03 +00:00
public delegate void RefAction<T1, T2>(ref T1 arg1, ref T2 arg2);
2023-10-19 22:41:49 +00:00
public World()
{
// Create the Empty Archetype
2023-10-24 20:13:14 +00:00
EmptyArchetype = CreateArchetype(ArchetypeSignature.Empty);
2023-10-19 22:41:49 +00:00
}
2023-10-25 01:44:41 +00:00
internal Archetype CreateArchetype(ArchetypeSignature signature)
2023-10-19 22:41:49 +00:00
{
2023-10-25 01:44:41 +00:00
var archetype = new Archetype(this, signature)
2023-10-20 00:41:45 +00:00
{
2023-10-25 01:44:41 +00:00
ComponentColumns = new List<Column>(signature.Count)
2023-10-20 00:41:45 +00:00
};
2023-10-19 22:41:49 +00:00
ArchetypeIndex.Add(signature, archetype);
for (int i = 0; i < signature.Count; i += 1)
{
var componentId = signature[i];
2023-10-25 01:44:41 +00:00
ComponentIndex[componentId].Add(archetype);
archetype.ComponentToColumnIndex.Add(componentId, archetype.ComponentColumns.Count);
archetype.ComponentColumns.Add(new Column(ElementSizes[componentId]));
2023-10-19 22:41:49 +00:00
}
return archetype;
}
public EntityId CreateEntity()
{
var entityId = EntityIdAssigner.Assign();
2023-10-25 01:44:41 +00:00
EntityIndex.Add(entityId, new Record(EmptyArchetype, EmptyArchetype.Count));
EmptyArchetype.RowToEntity.Add(entityId);
2023-10-19 22:41:49 +00:00
return entityId;
}
2023-10-25 01:44:41 +00:00
// used as a fast path by Archetype.Transfer
internal EntityId CreateEntityOnArchetype(Archetype archetype)
{
var entityId = EntityIdAssigner.Assign();
EntityIndex.Add(entityId, new Record(archetype, archetype.Count));
archetype.RowToEntity.Add(entityId);
return entityId;
}
// used as a fast path by Archetype.ClearAll
internal void FreeEntity(EntityId entityId)
{
EntityIndex.Remove(entityId);
EntityIdAssigner.Unassign(entityId);
}
2023-10-19 22:41:49 +00:00
// FIXME: would be much more efficient to do all this at load time somehow
2023-10-20 08:17:03 +00:00
private void RegisterComponent<T>() where T : unmanaged
2023-10-19 22:41:49 +00:00
{
2023-10-20 00:41:45 +00:00
var componentId = ComponentIdAssigner.Assign();
TypeToComponentId.Add(typeof(T), componentId);
2023-10-21 00:24:35 +00:00
ComponentIndex.Add(componentId, new List<Archetype>());
2023-10-20 00:41:45 +00:00
ElementSizes.Add(componentId, Unsafe.SizeOf<T>());
2023-10-19 22:41:49 +00:00
}
2023-10-20 08:17:03 +00:00
private void TryRegisterComponentId<T>() where T : unmanaged
2023-10-19 22:41:49 +00:00
{
2023-10-25 01:44:41 +00:00
if (!TypeToComponentId.ContainsKey(typeof(T)))
2023-10-19 22:41:49 +00:00
{
2023-10-20 08:17:03 +00:00
RegisterComponent<T>();
2023-10-19 22:41:49 +00:00
}
2023-10-20 08:17:03 +00:00
}
2023-10-19 22:41:49 +00:00
2023-10-25 01:44:41 +00:00
// non-generic variant for use with Transfer
internal void AddComponentIndexEntry(ComponentId componentId)
{
if (!ComponentIndex.ContainsKey(componentId))
{
ComponentIndex.Add(componentId, new List<Archetype>());
}
}
2023-10-21 00:24:35 +00:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-10-20 08:17:03 +00:00
private ComponentId GetComponentId<T>() where T : unmanaged
{
return TypeToComponentId[typeof(T)];
}
2023-10-21 00:24:35 +00:00
public bool Has<T>(EntityId entityId) where T : unmanaged
2023-10-20 08:17:03 +00:00
{
var componentId = GetComponentId<T>();
2023-10-19 22:41:49 +00:00
var record = EntityIndex[entityId];
2023-10-21 00:24:35 +00:00
return record.Archetype.ComponentToColumnIndex.ContainsKey(componentId);
2023-10-19 22:41:49 +00:00
}
2023-10-21 00:24:35 +00:00
// will throw if non-existent
public unsafe ref T Get<T>(EntityId entityId) where T : unmanaged
2023-10-19 22:41:49 +00:00
{
var componentId = GetComponentId<T>();
var record = EntityIndex[entityId];
2023-10-21 00:24:35 +00:00
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
2023-10-25 01:44:41 +00:00
var column = record.Archetype.ComponentColumns[columnIndex];
2023-10-19 22:41:49 +00:00
2023-10-21 00:24:35 +00:00
return ref ((T*) column.Elements)[record.Row];
2023-10-19 22:41:49 +00:00
}
2023-10-21 00:24:35 +00:00
public unsafe void Set<T>(in EntityId entityId, in T component) where T : unmanaged
2023-10-20 08:17:03 +00:00
{
TryRegisterComponentId<T>();
var componentId = GetComponentId<T>();
2023-10-21 00:24:35 +00:00
if (Has<T>(entityId))
2023-10-20 08:17:03 +00:00
{
var record = EntityIndex[entityId];
2023-10-21 00:24:35 +00:00
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
2023-10-25 01:44:41 +00:00
var column = record.Archetype.ComponentColumns[columnIndex];
2023-10-20 08:17:03 +00:00
((T*) column.Elements)[record.Row] = component;
}
else
{
2023-10-21 00:24:35 +00:00
Add(entityId, component);
2023-10-20 08:17:03 +00:00
}
}
2023-10-21 00:24:35 +00:00
private void Add<T>(EntityId entityId, in T component) where T : unmanaged
2023-10-19 22:41:49 +00:00
{
Archetype? nextArchetype;
var componentId = GetComponentId<T>();
2023-10-20 00:41:45 +00:00
// move the entity to the new archetype
2023-10-19 22:41:49 +00:00
var record = EntityIndex[entityId];
var archetype = record.Archetype;
if (archetype.Edges.TryGetValue(componentId, out var edge))
{
nextArchetype = edge.Add;
}
else
{
// FIXME: pool the signatures
var nextSignature = new ArchetypeSignature(archetype.Signature.Count + 1);
archetype.Signature.CopyTo(nextSignature);
nextSignature.Insert(componentId);
if (!ArchetypeIndex.TryGetValue(nextSignature, out nextArchetype))
{
nextArchetype = CreateArchetype(nextSignature);
}
2023-10-20 00:41:45 +00:00
var newEdge = new ArchetypeEdge(nextArchetype, archetype);
archetype.Edges.Add(componentId, newEdge);
nextArchetype.Edges.Add(componentId, newEdge);
}
MoveEntityToHigherArchetype(entityId, record.Row, archetype, nextArchetype);
// add the new component to the new archetype
2023-10-21 00:24:35 +00:00
var columnIndex = nextArchetype.ComponentToColumnIndex[componentId];
2023-10-25 01:44:41 +00:00
var column = nextArchetype.ComponentColumns[columnIndex];
2023-10-20 00:41:45 +00:00
column.Append(component);
}
2023-10-21 00:24:35 +00:00
public void Remove<T>(EntityId entityId) where T : unmanaged
2023-10-20 00:41:45 +00:00
{
Archetype? nextArchetype;
var componentId = GetComponentId<T>();
2023-10-21 00:24:35 +00:00
var (archetype, row) = EntityIndex[entityId];
2023-10-20 00:41:45 +00:00
if (archetype.Edges.TryGetValue(componentId, out var edge))
{
nextArchetype = edge.Remove;
}
else
{
// FIXME: pool the signatures
var nextSignature = new ArchetypeSignature(archetype.Signature.Count + 1);
archetype.Signature.CopyTo(nextSignature);
nextSignature.Remove(componentId);
if (!ArchetypeIndex.TryGetValue(nextSignature, out nextArchetype))
{
nextArchetype = CreateArchetype(nextSignature);
}
var newEdge = new ArchetypeEdge(nextArchetype, archetype);
2023-10-19 22:41:49 +00:00
archetype.Edges.Add(componentId, newEdge);
nextArchetype.Edges.Add(componentId, newEdge);
}
2023-10-21 00:24:35 +00:00
MoveEntityToLowerArchetype(entityId, row, archetype, nextArchetype, componentId);
}
public void Destroy(EntityId entityId)
{
var record = EntityIndex[entityId];
var archetype = record.Archetype;
var row = record.Row;
2023-10-25 01:44:41 +00:00
for (int i = 0; i < archetype.ComponentColumns.Count; i += 1)
2023-10-21 00:24:35 +00:00
{
2023-10-25 01:44:41 +00:00
archetype.ComponentColumns[i].Delete(row);
2023-10-21 00:24:35 +00:00
}
2023-10-24 20:13:14 +00:00
if (row != archetype.Count - 1)
2023-10-21 00:24:35 +00:00
{
2023-10-24 20:13:14 +00:00
// move last row entity to open spot
var lastRowEntity = archetype.RowToEntity[archetype.Count - 1];
archetype.RowToEntity[row] = lastRowEntity;
EntityIndex[lastRowEntity] = new Record(archetype, row);
2023-10-21 00:24:35 +00:00
}
2023-10-24 20:13:14 +00:00
archetype.RowToEntity.RemoveAt(archetype.Count - 1);
2023-10-21 00:24:35 +00:00
EntityIndex.Remove(entityId);
EntityIdAssigner.Unassign(entityId);
2023-10-19 22:41:49 +00:00
}
2023-10-20 00:41:45 +00:00
private void MoveEntityToHigherArchetype(EntityId entityId, int row, Archetype from, Archetype to)
2023-10-19 22:41:49 +00:00
{
2023-10-25 01:44:41 +00:00
for (int i = 0; i < from.ComponentColumns.Count; i += 1)
2023-10-19 22:41:49 +00:00
{
var componentId = from.Signature[i];
2023-10-21 00:24:35 +00:00
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
2023-10-19 22:41:49 +00:00
2023-10-20 00:41:45 +00:00
// copy all components to higher archetype
2023-10-25 01:44:41 +00:00
from.ComponentColumns[i].CopyElementToEnd(row, to.ComponentColumns[destinationColumnIndex]);
2023-10-20 00:41:45 +00:00
// delete row on from archetype
2023-10-25 01:44:41 +00:00
from.ComponentColumns[i].Delete(row);
2023-10-21 00:24:35 +00:00
}
2023-10-20 00:41:45 +00:00
2023-10-24 20:13:14 +00:00
if (row != from.Count - 1)
2023-10-21 00:24:35 +00:00
{
2023-10-24 20:13:14 +00:00
// move last row entity to open spot
var lastRowEntity = from.RowToEntity[from.Count - 1];
from.RowToEntity[row] = lastRowEntity;
EntityIndex[lastRowEntity] = new Record(from, row);
2023-10-20 00:41:45 +00:00
}
2023-10-24 20:13:14 +00:00
from.RowToEntity.RemoveAt(from.Count - 1);
2023-10-20 00:41:45 +00:00
// update row to entity lookup on to archetype
EntityIndex[entityId] = new Record(to, to.Count);
to.RowToEntity.Add(entityId);
}
private void MoveEntityToLowerArchetype(EntityId entityId, int row, Archetype from, Archetype to, ComponentId removed)
{
2023-10-25 01:44:41 +00:00
for (int i = 0; i < from.ComponentColumns.Count; i += 1)
2023-10-20 00:41:45 +00:00
{
var componentId = from.Signature[i];
// delete the row
2023-10-25 01:44:41 +00:00
from.ComponentColumns[i].Delete(row);
2023-10-20 00:41:45 +00:00
// if this isn't the removed component, copy to the lower archetype
if (componentId != removed)
{
2023-10-21 00:24:35 +00:00
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
2023-10-25 01:44:41 +00:00
from.ComponentColumns[i].CopyElementToEnd(row, to.ComponentColumns[destinationColumnIndex]);
2023-10-20 00:41:45 +00:00
}
2023-10-19 22:41:49 +00:00
}
2023-10-24 20:13:14 +00:00
if (row != from.Count - 1)
2023-10-21 00:24:35 +00:00
{
// update row to entity lookup on from archetype
2023-10-24 20:13:14 +00:00
var lastRowEntity = from.RowToEntity[from.Count - 1];
from.RowToEntity[row] = lastRowEntity;
EntityIndex[lastRowEntity] = new Record(from, row);
2023-10-21 00:24:35 +00:00
}
2023-10-24 20:13:14 +00:00
from.RowToEntity.RemoveAt(from.Count - 1);
2023-10-20 00:41:45 +00:00
// update row to entity lookup on to archetype
2023-10-19 22:41:49 +00:00
EntityIndex[entityId] = new Record(to, to.Count);
2023-10-20 00:41:45 +00:00
to.RowToEntity.Add(entityId);
2023-10-19 22:41:49 +00:00
}
2023-10-20 00:41:45 +00:00
2023-10-24 20:13:14 +00:00
public unsafe void ForEachEntity<T, T1, T2>(Filter filter,
2023-10-21 00:24:35 +00:00
T rowForEachContainer) where T : IForEach<T1, T2> where T1 : unmanaged where T2 : unmanaged
{
2023-10-24 20:13:14 +00:00
foreach (var archetype in filter.Archetypes)
2023-10-21 00:24:35 +00:00
{
2023-10-24 20:13:14 +00:00
var componentIdOne = archetype.Signature[0];
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
2023-10-25 01:44:41 +00:00
var columnOneElements = archetype.ComponentColumns[columnIndexOne].Elements;
2023-10-20 08:17:03 +00:00
2023-10-24 20:13:14 +00:00
var componentIdTwo = archetype.Signature[1];
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
2023-10-25 01:44:41 +00:00
var columnTwoElements = archetype.ComponentColumns[columnIndexTwo].Elements;
2023-10-20 08:17:03 +00:00
2023-10-24 20:13:14 +00:00
for (int i = archetype.Count - 1; i >= 0; i -= 1)
2023-10-20 08:17:03 +00:00
{
2023-10-24 20:13:14 +00:00
rowForEachContainer.Update(ref ((T1*) columnOneElements)[i], ref ((T2*) columnTwoElements)[i]);
2023-10-20 08:17:03 +00:00
}
}
}
2023-10-24 20:13:14 +00:00
public unsafe void ForEachEntity<T1, T2>(Filter filter, RefAction<T1, T2> rowAction) where T1 : unmanaged where T2 : unmanaged
2023-10-20 08:17:03 +00:00
{
2023-10-24 20:13:14 +00:00
foreach (var archetype in filter.Archetypes)
2023-10-20 08:17:03 +00:00
{
2023-10-24 20:13:14 +00:00
var componentIdOne = archetype.Signature[0];
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
2023-10-25 01:44:41 +00:00
var columnOneElements = archetype.ComponentColumns[columnIndexOne].Elements;
2023-10-20 08:17:03 +00:00
2023-10-24 20:13:14 +00:00
var componentIdTwo = archetype.Signature[1];
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
2023-10-25 01:44:41 +00:00
var columnTwoElements = archetype.ComponentColumns[columnIndexTwo].Elements;
2023-10-24 20:13:14 +00:00
for (int i = archetype.Count - 1; i >= 0; i -= 1)
2023-10-20 08:17:03 +00:00
{
2023-10-24 20:13:14 +00:00
rowAction(ref ((T1*) columnOneElements)[i], ref ((T2*) columnTwoElements)[i]);
2023-10-20 08:17:03 +00:00
}
}
}
2023-10-21 00:24:35 +00:00
2023-10-20 00:41:45 +00:00
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
// dispose managed state (managed objects)
foreach (var archetype in ArchetypeIndex.Values)
{
for (var i = 0; i < archetype.Signature.Count; i += 1)
{
2023-10-25 01:44:41 +00:00
archetype.ComponentColumns[i].Dispose();
2023-10-20 00:41:45 +00:00
}
}
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
IsDisposed = true;
}
}
// TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~World()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
2023-10-19 22:41:49 +00:00
}
}