|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace MoonTools.ECS.Rev2
|
|
|
|
|
{
|
|
|
|
@ -12,8 +13,8 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
// Going from EntityId to Archetype and storage row
|
|
|
|
|
Dictionary<EntityId, Record> EntityIndex = new Dictionary<EntityId, Record>();
|
|
|
|
|
|
|
|
|
|
// Going from ComponentId to an Archetype storage column index
|
|
|
|
|
Dictionary<ComponentId, Dictionary<ArchetypeId, ArchetypeRecord>> ComponentIndex = new Dictionary<ComponentId, Dictionary<ArchetypeId, ArchetypeRecord>>();
|
|
|
|
|
// Going from ComponentId to Archetype list
|
|
|
|
|
Dictionary<ComponentId, List<Archetype>> ComponentIndex = new Dictionary<ComponentId, List<Archetype>>();
|
|
|
|
|
|
|
|
|
|
// Get ComponentId from a Type
|
|
|
|
|
Dictionary<Type, ComponentId> TypeToComponentId = new Dictionary<Type, ComponentId>();
|
|
|
|
@ -49,7 +50,8 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
for (int i = 0; i < signature.Count; i += 1)
|
|
|
|
|
{
|
|
|
|
|
var componentId = signature[i];
|
|
|
|
|
ComponentIndex[componentId].Add(archetypeId, new ArchetypeRecord(i));
|
|
|
|
|
ComponentIndex[componentId].Add(archetype); //, new ArchetypeRecord(i));
|
|
|
|
|
archetype.ComponentToColumnIndex.Add(componentId, archetype.Components.Count);
|
|
|
|
|
archetype.Components.Add(new Column(ElementSizes[componentId]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -70,7 +72,7 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
{
|
|
|
|
|
var componentId = ComponentIdAssigner.Assign();
|
|
|
|
|
TypeToComponentId.Add(typeof(T), componentId);
|
|
|
|
|
ComponentIndex.Add(componentId, new Dictionary<ArchetypeId, ArchetypeRecord>());
|
|
|
|
|
ComponentIndex.Add(componentId, new List<Archetype>());
|
|
|
|
|
ElementSizes.Add(componentId, Unsafe.SizeOf<T>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -82,67 +84,51 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
private ComponentId GetComponentId<T>() where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
return TypeToComponentId[typeof(T)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal ArchetypeRecord GetArchetypeRecord<T>(Archetype archetype) where T : unmanaged
|
|
|
|
|
public bool Has<T>(EntityId entityId) where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
var componentId = GetComponentId<T>();
|
|
|
|
|
return ComponentIndex[componentId][archetype.Id];
|
|
|
|
|
var record = EntityIndex[entityId];
|
|
|
|
|
return record.Archetype.ComponentToColumnIndex.ContainsKey(componentId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasComponent<T>(EntityId entityId) where T : unmanaged
|
|
|
|
|
// will throw if non-existent
|
|
|
|
|
public unsafe ref T Get<T>(EntityId entityId) where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
var componentId = GetComponentId<T>();
|
|
|
|
|
|
|
|
|
|
var record = EntityIndex[entityId];
|
|
|
|
|
var archetypes = ComponentIndex[componentId];
|
|
|
|
|
return archetypes.ContainsKey(record.Archetype.Id);
|
|
|
|
|
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
|
|
|
|
|
var column = record.Archetype.Components[columnIndex];
|
|
|
|
|
|
|
|
|
|
return ref ((T*) column.Elements)[record.Row];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unsafe T GetComponent<T>(EntityId entityId) where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
var componentId = GetComponentId<T>();
|
|
|
|
|
|
|
|
|
|
var record = EntityIndex[entityId];
|
|
|
|
|
var archetype = record.Archetype;
|
|
|
|
|
|
|
|
|
|
var archetypes = ComponentIndex[componentId];
|
|
|
|
|
if (!archetypes.ContainsKey(archetype.Id))
|
|
|
|
|
{
|
|
|
|
|
return default; // FIXME: maybe throw in debug mode?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var archetypeRecord = archetypes[archetype.Id];
|
|
|
|
|
var column = archetype.Components[archetypeRecord.ColumnIndex];
|
|
|
|
|
|
|
|
|
|
return ((T*) column.Elements)[record.Row];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unsafe void SetComponent<T>(EntityId entityId, T component) where T : unmanaged
|
|
|
|
|
public unsafe void Set<T>(in EntityId entityId, in T component) where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
TryRegisterComponentId<T>();
|
|
|
|
|
var componentId = GetComponentId<T>();
|
|
|
|
|
|
|
|
|
|
if (HasComponent<T>(entityId))
|
|
|
|
|
if (Has<T>(entityId))
|
|
|
|
|
{
|
|
|
|
|
var record = EntityIndex[entityId];
|
|
|
|
|
var archetype = record.Archetype;
|
|
|
|
|
var archetypes = ComponentIndex[componentId];
|
|
|
|
|
var archetypeRecord = archetypes[archetype.Id];
|
|
|
|
|
var column = archetype.Components[archetypeRecord.ColumnIndex];
|
|
|
|
|
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
|
|
|
|
|
var column = record.Archetype.Components[columnIndex];
|
|
|
|
|
|
|
|
|
|
((T*) column.Elements)[record.Row] = component;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AddComponent(entityId, component);
|
|
|
|
|
Add(entityId, component);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddComponent<T>(EntityId entityId, T component) where T : unmanaged
|
|
|
|
|
private void Add<T>(EntityId entityId, in T component) where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
Archetype? nextArchetype;
|
|
|
|
|
|
|
|
|
@ -176,20 +162,18 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
MoveEntityToHigherArchetype(entityId, record.Row, archetype, nextArchetype);
|
|
|
|
|
|
|
|
|
|
// add the new component to the new archetype
|
|
|
|
|
var archetypes = ComponentIndex[componentId];
|
|
|
|
|
var archetypeRecord = archetypes[nextArchetype.Id];
|
|
|
|
|
var column = nextArchetype.Components[archetypeRecord.ColumnIndex];
|
|
|
|
|
var columnIndex = nextArchetype.ComponentToColumnIndex[componentId];
|
|
|
|
|
var column = nextArchetype.Components[columnIndex];
|
|
|
|
|
column.Append(component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveComponent<T>(EntityId entityId) where T : unmanaged
|
|
|
|
|
public void Remove<T>(EntityId entityId) where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
Archetype? nextArchetype;
|
|
|
|
|
|
|
|
|
|
var componentId = GetComponentId<T>();
|
|
|
|
|
|
|
|
|
|
var record = EntityIndex[entityId];
|
|
|
|
|
var archetype = record.Archetype;
|
|
|
|
|
var (archetype, row) = EntityIndex[entityId];
|
|
|
|
|
|
|
|
|
|
if (archetype.Edges.TryGetValue(componentId, out var edge))
|
|
|
|
|
{
|
|
|
|
@ -212,7 +196,30 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
nextArchetype.Edges.Add(componentId, newEdge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MoveEntityToLowerArchetype(entityId, record.Row, archetype, nextArchetype, componentId);
|
|
|
|
|
MoveEntityToLowerArchetype(entityId, row, archetype, nextArchetype, componentId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Destroy(EntityId entityId)
|
|
|
|
|
{
|
|
|
|
|
var record = EntityIndex[entityId];
|
|
|
|
|
var archetype = record.Archetype;
|
|
|
|
|
var row = record.Row;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < archetype.Components.Count; i += 1)
|
|
|
|
|
{
|
|
|
|
|
archetype.Components[i].Delete(row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (archetype.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
// update row to entity lookup on archetype
|
|
|
|
|
archetype.RowToEntity[row] = archetype.RowToEntity[archetype.Count - 1];
|
|
|
|
|
archetype.RowToEntity.RemoveAt(archetype.Count - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
archetype.Count -= 1;
|
|
|
|
|
EntityIndex.Remove(entityId);
|
|
|
|
|
EntityIdAssigner.Unassign(entityId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MoveEntityToHigherArchetype(EntityId entityId, int row, Archetype from, Archetype to)
|
|
|
|
@ -220,13 +227,14 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
for (int i = 0; i < from.Components.Count; i += 1)
|
|
|
|
|
{
|
|
|
|
|
var componentId = from.Signature[i];
|
|
|
|
|
var destinationColumnIndex = ComponentIndex[componentId][to.Id].ColumnIndex;
|
|
|
|
|
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
|
|
|
|
|
|
|
|
|
|
// copy all components to higher archetype
|
|
|
|
|
from.Components[i].CopyToEnd(row, to.Components[destinationColumnIndex]);
|
|
|
|
|
|
|
|
|
|
// delete row on from archetype
|
|
|
|
|
from.Components[i].Delete(row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (from.Count > 1)
|
|
|
|
|
{
|
|
|
|
@ -235,7 +243,6 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
from.RowToEntity.RemoveAt(from.Count - 1);
|
|
|
|
|
EntityIndex[from.RowToEntity[row]] = new Record(from, row);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update row to entity lookup on to archetype
|
|
|
|
|
EntityIndex[entityId] = new Record(to, to.Count);
|
|
|
|
@ -257,18 +264,18 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
// if this isn't the removed component, copy to the lower archetype
|
|
|
|
|
if (componentId != removed)
|
|
|
|
|
{
|
|
|
|
|
var destinationColumnIndex = ComponentIndex[componentId][to.Id].ColumnIndex;
|
|
|
|
|
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
|
|
|
|
|
from.Components[i].CopyToEnd(row, to.Components[destinationColumnIndex]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (from.Count > 0)
|
|
|
|
|
if (from.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
// update row to entity lookup on from archetype
|
|
|
|
|
from.RowToEntity[row] = from.RowToEntity[from.Count - 1];
|
|
|
|
|
from.RowToEntity.RemoveAt(from.Count - 1);
|
|
|
|
|
EntityIndex[from.RowToEntity[row]] = new Record(from, row);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update row to entity lookup on to archetype
|
|
|
|
|
EntityIndex[entityId] = new Record(to, to.Count);
|
|
|
|
@ -278,19 +285,46 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
from.Count -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unsafe void ForEachEntity<T, T1, T2>(ArchetypeSignature signature,
|
|
|
|
|
T rowForEachContainer) where T : IForEach<T1, T2> where T1 : unmanaged where T2 : unmanaged
|
|
|
|
|
{
|
|
|
|
|
var archetype = ArchetypeIndex[signature];
|
|
|
|
|
|
|
|
|
|
var componentIdOne = signature[0];
|
|
|
|
|
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
|
|
|
|
|
var columnOneElements = archetype.Components[columnIndexOne].Elements;
|
|
|
|
|
|
|
|
|
|
var componentIdTwo = signature[1];
|
|
|
|
|
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
|
|
|
|
|
var columnTwoElements = archetype.Components[columnIndexTwo].Elements;
|
|
|
|
|
|
|
|
|
|
for (int i = archetype.Count - 1; i >= 0; i -= 1)
|
|
|
|
|
{
|
|
|
|
|
rowForEachContainer.Update(ref ((T1*) columnOneElements)[i], ref ((T2*) columnTwoElements)[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var edge in archetype.Edges.Values)
|
|
|
|
|
{
|
|
|
|
|
if (edge.Add != archetype)
|
|
|
|
|
{
|
|
|
|
|
ForEachEntity<T, T1, T2>(edge.Add.Signature, rowForEachContainer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unsafe void ForEachEntity<T1, T2>(ArchetypeSignature signature, RefAction<T1, T2> rowAction) where T1 : unmanaged where T2 : unmanaged
|
|
|
|
|
{
|
|
|
|
|
var archetype = ArchetypeIndex[signature];
|
|
|
|
|
|
|
|
|
|
var componentIdOne = signature[0];
|
|
|
|
|
var columnIndexOne = ComponentIndex[componentIdOne][archetype.Id].ColumnIndex;
|
|
|
|
|
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
|
|
|
|
|
var columnOneElements = archetype.Components[columnIndexOne].Elements;
|
|
|
|
|
|
|
|
|
|
var componentIdTwo = signature[1];
|
|
|
|
|
var columnIndexTwo = ComponentIndex[componentIdTwo][archetype.Id].ColumnIndex;
|
|
|
|
|
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
|
|
|
|
|
var columnTwoElements = archetype.Components[columnIndexTwo].Elements;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < archetype.Count; i += 1)
|
|
|
|
|
for (int i = archetype.Count - 1; i >= 0; i -= 1)
|
|
|
|
|
{
|
|
|
|
|
rowAction(ref ((T1*) columnOneElements)[i], ref ((T2*) columnTwoElements)[i]);
|
|
|
|
|
}
|
|
|
|
@ -304,15 +338,13 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
public void ForEachEntity(ArchetypeSignature signature, Action<Entity> rowAction)
|
|
|
|
|
public void ForEachEntity(ArchetypeSignature signature, Action<EntityId> rowAction)
|
|
|
|
|
{
|
|
|
|
|
var archetype = ArchetypeIndex[signature];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < archetype.Count; i += 1)
|
|
|
|
|
{
|
|
|
|
|
var entity = new Entity(this, archetype, i, archetype.RowToEntity[i]);
|
|
|
|
|
rowAction(entity);
|
|
|
|
|
rowAction(archetype.RowToEntity[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// recursion might get too hairy here
|
|
|
|
@ -324,7 +356,13 @@ namespace MoonTools.ECS.Rev2
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public ReverseSpanEnumerator<EntityId> Entities(ArchetypeSignature signature)
|
|
|
|
|
{
|
|
|
|
|
var archetype = ArchetypeIndex[signature];
|
|
|
|
|
return new ReverseSpanEnumerator<EntityId>(
|
|
|
|
|
CollectionsMarshal.AsSpan(archetype.RowToEntity));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|