snapshot system
parent
b3ff7e3f1c
commit
5419fbd72d
|
@ -1,12 +1,12 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MoonTools.ECS.Rev2
|
namespace MoonTools.ECS.Rev2;
|
||||||
|
|
||||||
|
internal class Archetype
|
||||||
{
|
{
|
||||||
public class Archetype
|
public World World;
|
||||||
{
|
|
||||||
public ArchetypeSignature Signature;
|
public ArchetypeSignature Signature;
|
||||||
public ArchetypeId Id { get; private set; }
|
public List<Column> ComponentColumns = new List<Column>();
|
||||||
public List<Column> Components = new List<Column>();
|
|
||||||
public List<EntityId> RowToEntity = new List<EntityId>();
|
public List<EntityId> RowToEntity = new List<EntityId>();
|
||||||
|
|
||||||
public Dictionary<ComponentId, int> ComponentToColumnIndex =
|
public Dictionary<ComponentId, int> ComponentToColumnIndex =
|
||||||
|
@ -15,10 +15,24 @@ namespace MoonTools.ECS.Rev2
|
||||||
|
|
||||||
public int Count => RowToEntity.Count;
|
public int Count => RowToEntity.Count;
|
||||||
|
|
||||||
public Archetype(ArchetypeId id, ArchetypeSignature signature)
|
public Archetype(World world, ArchetypeSignature signature)
|
||||||
{
|
{
|
||||||
Id = id;
|
World = world;
|
||||||
Signature = signature;
|
Signature = signature;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ClearAll()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < ComponentColumns.Count; i += 1)
|
||||||
|
{
|
||||||
|
ComponentColumns[i].Count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var entityId in RowToEntity)
|
||||||
|
{
|
||||||
|
World.FreeEntity(entityId);
|
||||||
|
}
|
||||||
|
|
||||||
|
RowToEntity.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
namespace MoonTools.ECS.Rev2
|
namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
public readonly record struct ArchetypeEdge(Archetype Add, Archetype Remove);
|
internal readonly record struct ArchetypeEdge(Archetype Add, Archetype Remove);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
namespace MoonTools.ECS.Rev2
|
|
||||||
{
|
|
||||||
public readonly record struct ArchetypeId(int Id) : IHasId;
|
|
||||||
}
|
|
|
@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MoonTools.ECS.Rev2
|
namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
public class ArchetypeSignature : IEquatable<ArchetypeSignature>
|
internal class ArchetypeSignature : IEquatable<ArchetypeSignature>
|
||||||
{
|
{
|
||||||
public static ArchetypeSignature Empty = new ArchetypeSignature(0);
|
public static ArchetypeSignature Empty = new ArchetypeSignature(0);
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,13 @@ using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace MoonTools.ECS.Rev2
|
namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
public unsafe class Column : IDisposable
|
internal unsafe class Column : IDisposable
|
||||||
{
|
{
|
||||||
public nint Elements;
|
public nint Elements;
|
||||||
public int ElementSize;
|
|
||||||
public int Count;
|
public int Count;
|
||||||
public int Capacity;
|
|
||||||
|
private int Capacity;
|
||||||
|
private readonly int ElementSize;
|
||||||
|
|
||||||
private bool IsDisposed;
|
private bool IsDisposed;
|
||||||
|
|
||||||
|
@ -27,6 +28,12 @@ namespace MoonTools.ECS.Rev2
|
||||||
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ResizeTo(int capacity)
|
||||||
|
{
|
||||||
|
Capacity = capacity;
|
||||||
|
Elements = (nint) NativeMemory.Realloc((void*) Elements, (nuint) (ElementSize * Capacity));
|
||||||
|
}
|
||||||
|
|
||||||
// Fills gap by copying final element to the deleted index
|
// Fills gap by copying final element to the deleted index
|
||||||
public void Delete(int index)
|
public void Delete(int index)
|
||||||
{
|
{
|
||||||
|
@ -53,7 +60,7 @@ namespace MoonTools.ECS.Rev2
|
||||||
Count += 1;
|
Count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CopyToEnd(int index, Column other)
|
public void CopyElementToEnd(int index, Column other)
|
||||||
{
|
{
|
||||||
if (other.Count >= other.Capacity)
|
if (other.Count >= other.Capacity)
|
||||||
{
|
{
|
||||||
|
@ -69,6 +76,22 @@ namespace MoonTools.ECS.Rev2
|
||||||
other.Count += 1;
|
other.Count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void CopyAllTo(Column other)
|
||||||
|
{
|
||||||
|
if (Count >= other.Capacity)
|
||||||
|
{
|
||||||
|
other.ResizeTo(Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
NativeMemory.Copy(
|
||||||
|
(void*) Elements,
|
||||||
|
(void*) other.Elements,
|
||||||
|
(nuint) (ElementSize * Count)
|
||||||
|
);
|
||||||
|
|
||||||
|
other.Count = Count;
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
protected virtual void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (!IsDisposed)
|
if (!IsDisposed)
|
||||||
|
|
|
@ -2,7 +2,7 @@ using System;
|
||||||
|
|
||||||
namespace MoonTools.ECS.Rev2
|
namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
public readonly record struct ComponentId(int Id) : IHasId, IComparable<ComponentId>
|
internal readonly record struct ComponentId(int Id) : IHasId, IComparable<ComponentId>
|
||||||
{
|
{
|
||||||
public int CompareTo(ComponentId other)
|
public int CompareTo(ComponentId other)
|
||||||
{
|
{
|
||||||
|
|
|
@ -47,6 +47,15 @@ namespace MoonTools.ECS.Rev2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EntityId RandomEntity
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var randomIndex = RandomManager.Next(Count);
|
||||||
|
return NthEntity(randomIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WARNING: this WILL crash if the index is out of range!
|
// WARNING: this WILL crash if the index is out of range!
|
||||||
public EntityId NthEntity(int index)
|
public EntityId NthEntity(int index)
|
||||||
{
|
{
|
||||||
|
@ -66,16 +75,25 @@ namespace MoonTools.ECS.Rev2
|
||||||
throw new InvalidOperationException("Filter index out of range!");
|
throw new InvalidOperationException("Filter index out of range!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityId RandomEntity
|
public void DestroyAllEntities()
|
||||||
{
|
{
|
||||||
get
|
foreach (var archetype in Archetypes)
|
||||||
{
|
{
|
||||||
var randomIndex = RandomManager.Next(Count);
|
archetype.ClearAll();
|
||||||
return NthEntity(randomIndex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Filter(Archetype emptyArchetype, HashSet<ComponentId> included, HashSet<ComponentId> excluded)
|
public void TakeSnapshot(Snapshot snapshot)
|
||||||
|
{
|
||||||
|
snapshot.Reset();
|
||||||
|
|
||||||
|
foreach (var archetype in Archetypes)
|
||||||
|
{
|
||||||
|
snapshot.TakeArchetypeSnapshot(archetype);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal Filter(Archetype emptyArchetype, HashSet<ComponentId> included, HashSet<ComponentId> excluded)
|
||||||
{
|
{
|
||||||
EmptyArchetype = emptyArchetype;
|
EmptyArchetype = emptyArchetype;
|
||||||
Included = included;
|
Included = included;
|
||||||
|
@ -85,7 +103,6 @@ namespace MoonTools.ECS.Rev2
|
||||||
internal ref struct ArchetypeEnumerator
|
internal ref struct ArchetypeEnumerator
|
||||||
{
|
{
|
||||||
private Archetype CurrentArchetype;
|
private Archetype CurrentArchetype;
|
||||||
private bool Active;
|
|
||||||
|
|
||||||
// TODO: pool these
|
// TODO: pool these
|
||||||
private Queue<Archetype> ArchetypeQueue = new Queue<Archetype>();
|
private Queue<Archetype> ArchetypeQueue = new Queue<Archetype>();
|
||||||
|
@ -96,8 +113,6 @@ namespace MoonTools.ECS.Rev2
|
||||||
|
|
||||||
public ArchetypeEnumerator(Filter filter)
|
public ArchetypeEnumerator(Filter filter)
|
||||||
{
|
{
|
||||||
Active = false;
|
|
||||||
|
|
||||||
var empty = filter.EmptyArchetype;
|
var empty = filter.EmptyArchetype;
|
||||||
ArchetypeSearchQueue.Enqueue(empty);
|
ArchetypeSearchQueue.Enqueue(empty);
|
||||||
|
|
||||||
|
@ -127,29 +142,16 @@ namespace MoonTools.ECS.Rev2
|
||||||
ArchetypeQueue.Enqueue(current);
|
ArchetypeQueue.Enqueue(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the current archetype satisfies the filter, we need to add all edges that
|
// breadth-first search
|
||||||
// do not have an excluded component
|
// ignore excluded component edges
|
||||||
// if the current archetype does not satisfy the filter, we need to add all edges that
|
|
||||||
// include an included component
|
|
||||||
foreach (var (componentId, edge) in current.Edges)
|
foreach (var (componentId, edge) in current.Edges)
|
||||||
{
|
{
|
||||||
if (satisfiesFilter)
|
if (!Explored.Contains(edge.Add) && !filter.Excluded.Contains(componentId))
|
||||||
{
|
|
||||||
if (!filter.Excluded.Contains(componentId) && !Explored.Contains(edge.Add))
|
|
||||||
{
|
{
|
||||||
Explored.Add(edge.Add);
|
Explored.Add(edge.Add);
|
||||||
ArchetypeSearchQueue.Enqueue(edge.Add);
|
ArchetypeSearchQueue.Enqueue(edge.Add);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
if (filter.Included.Contains(componentId) && !Explored.Contains(edge.Add))
|
|
||||||
{
|
|
||||||
Explored.Add(edge.Add);
|
|
||||||
ArchetypeSearchQueue.Enqueue(edge.Add);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace MoonTools.ECS.Rev2
|
||||||
|
{
|
||||||
|
public ref struct FilterBuilder
|
||||||
|
{
|
||||||
|
World World;
|
||||||
|
HashSet<ComponentId> Included;
|
||||||
|
HashSet<ComponentId> Excluded;
|
||||||
|
|
||||||
|
internal FilterBuilder(World world)
|
||||||
|
{
|
||||||
|
World = world;
|
||||||
|
Included = new HashSet<ComponentId>();
|
||||||
|
Excluded = new HashSet<ComponentId>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private FilterBuilder(World world, HashSet<ComponentId> included, HashSet<ComponentId> excluded)
|
||||||
|
{
|
||||||
|
World = world;
|
||||||
|
Included = included;
|
||||||
|
Excluded = excluded;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilterBuilder Include<T>() where T : unmanaged
|
||||||
|
{
|
||||||
|
Included.Add(World.TypeToComponentId[typeof(T)]);
|
||||||
|
return new FilterBuilder(World, Included, Excluded);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilterBuilder Exclude<T>() where T : unmanaged
|
||||||
|
{
|
||||||
|
Excluded.Add(World.TypeToComponentId[typeof(T)]);
|
||||||
|
return new FilterBuilder(World, Included, Excluded);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Filter Build()
|
||||||
|
{
|
||||||
|
return new Filter(World.EmptyArchetype, Included, Excluded);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,7 @@ using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MoonTools.ECS.Rev2
|
namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
public class IdAssigner<T> where T : struct, IHasId
|
internal class IdAssigner<T> where T : struct, IHasId
|
||||||
{
|
{
|
||||||
int Next;
|
int Next;
|
||||||
Queue<int> AvailableIds = new Queue<int>();
|
Queue<int> AvailableIds = new Queue<int>();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
namespace MoonTools.ECS.Rev2
|
namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
public readonly record struct Record(Archetype Archetype, int Row);
|
internal readonly record struct Record(Archetype Archetype, int Row);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,110 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace MoonTools.ECS.Rev2;
|
||||||
|
|
||||||
|
public class Snapshot
|
||||||
|
{
|
||||||
|
private Dictionary<ArchetypeSignature, ArchetypeSnapshot> ArchetypeSnapshots =
|
||||||
|
new Dictionary<ArchetypeSignature, ArchetypeSnapshot>();
|
||||||
|
|
||||||
|
public int Count
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var count = 0;
|
||||||
|
|
||||||
|
foreach (var snapshot in ArchetypeSnapshots.Values)
|
||||||
|
{
|
||||||
|
count += snapshot.Count;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Restore(World world)
|
||||||
|
{
|
||||||
|
foreach (var (archetypeSignature, archetypeSnapshot) in ArchetypeSnapshots)
|
||||||
|
{
|
||||||
|
var archetype = world.ArchetypeIndex[archetypeSignature];
|
||||||
|
RestoreArchetypeSnapshot(archetype);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void Reset()
|
||||||
|
{
|
||||||
|
foreach (var archetypeSnapshot in ArchetypeSnapshots.Values)
|
||||||
|
{
|
||||||
|
archetypeSnapshot.Count = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void TakeArchetypeSnapshot(Archetype archetype)
|
||||||
|
{
|
||||||
|
if (!ArchetypeSnapshots.TryGetValue(archetype.Signature, out var archetypeSnapshot))
|
||||||
|
{
|
||||||
|
archetypeSnapshot = new ArchetypeSnapshot(archetype.Signature);
|
||||||
|
ArchetypeSnapshots.Add(archetype.Signature, archetypeSnapshot);
|
||||||
|
}
|
||||||
|
|
||||||
|
archetypeSnapshot.Take(archetype);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RestoreArchetypeSnapshot(Archetype archetype)
|
||||||
|
{
|
||||||
|
var archetypeSnapshot = ArchetypeSnapshots[archetype.Signature];
|
||||||
|
archetypeSnapshot.Restore(archetype);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class ArchetypeSnapshot
|
||||||
|
{
|
||||||
|
public ArchetypeSignature Signature;
|
||||||
|
public readonly List<Column> ComponentColumns;
|
||||||
|
|
||||||
|
public int Count;
|
||||||
|
|
||||||
|
public ArchetypeSnapshot(ArchetypeSignature signature)
|
||||||
|
{
|
||||||
|
Signature = signature;
|
||||||
|
ComponentColumns = new List<Column>(signature.Count);
|
||||||
|
|
||||||
|
for (int i = 0; i < signature.Count; i += 1)
|
||||||
|
{
|
||||||
|
var componentId = signature[i];
|
||||||
|
ComponentColumns.Add(new Column(World.ElementSizes[componentId]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Take(Archetype archetype)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < ComponentColumns.Count; i += 1)
|
||||||
|
{
|
||||||
|
archetype.ComponentColumns[i].CopyAllTo(ComponentColumns[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Count = archetype.Count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Restore(Archetype archetype)
|
||||||
|
{
|
||||||
|
// Clear out existing entities
|
||||||
|
archetype.ClearAll();
|
||||||
|
|
||||||
|
// Copy all component data
|
||||||
|
for (int i = 0; i < ComponentColumns.Count; i += 1)
|
||||||
|
{
|
||||||
|
ComponentColumns[i].CopyAllTo(archetype.ComponentColumns[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the row to entity list
|
||||||
|
archetype.RowToEntity.Clear();
|
||||||
|
|
||||||
|
// Create new entities and repopulate the row to entity list
|
||||||
|
for (int i = 0; i < Count; i += 1)
|
||||||
|
{
|
||||||
|
var entityId = archetype.World.CreateEntityOnArchetype(archetype);
|
||||||
|
archetype.RowToEntity.Add(entityId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,8 +7,13 @@ namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
public class World : IDisposable
|
public class World : IDisposable
|
||||||
{
|
{
|
||||||
|
// 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>();
|
||||||
|
|
||||||
// Lookup from ArchetypeSignature to Archetype
|
// Lookup from ArchetypeSignature to Archetype
|
||||||
Dictionary<ArchetypeSignature, Archetype> ArchetypeIndex = new Dictionary<ArchetypeSignature, Archetype>();
|
internal Dictionary<ArchetypeSignature, Archetype> ArchetypeIndex = new Dictionary<ArchetypeSignature, Archetype>();
|
||||||
|
|
||||||
// Going from EntityId to Archetype and storage row
|
// Going from EntityId to Archetype and storage row
|
||||||
Dictionary<EntityId, Record> EntityIndex = new Dictionary<EntityId, Record>();
|
Dictionary<EntityId, Record> EntityIndex = new Dictionary<EntityId, Record>();
|
||||||
|
@ -16,18 +21,13 @@ namespace MoonTools.ECS.Rev2
|
||||||
// Going from ComponentId to Archetype list
|
// Going from ComponentId to Archetype list
|
||||||
Dictionary<ComponentId, List<Archetype>> ComponentIndex = new Dictionary<ComponentId, List<Archetype>>();
|
Dictionary<ComponentId, List<Archetype>> ComponentIndex = new Dictionary<ComponentId, List<Archetype>>();
|
||||||
|
|
||||||
// Get ComponentId from a Type
|
|
||||||
Dictionary<Type, ComponentId> TypeToComponentId = new Dictionary<Type, ComponentId>();
|
|
||||||
|
|
||||||
// Get element size from a ComponentId
|
|
||||||
Dictionary<ComponentId, int> ElementSizes = new Dictionary<ComponentId, int>();
|
|
||||||
|
|
||||||
// ID Management
|
// ID Management
|
||||||
IdAssigner<ArchetypeId> ArchetypeIdAssigner = new IdAssigner<ArchetypeId>();
|
|
||||||
IdAssigner<EntityId> EntityIdAssigner = new IdAssigner<EntityId>();
|
IdAssigner<EntityId> EntityIdAssigner = new IdAssigner<EntityId>();
|
||||||
IdAssigner<ComponentId> ComponentIdAssigner = new IdAssigner<ComponentId>();
|
IdAssigner<ComponentId> ComponentIdAssigner = new IdAssigner<ComponentId>();
|
||||||
|
|
||||||
public readonly Archetype EmptyArchetype;
|
internal readonly Archetype EmptyArchetype;
|
||||||
|
|
||||||
|
public FilterBuilder FilterBuilder => new FilterBuilder(this);
|
||||||
|
|
||||||
private bool IsDisposed;
|
private bool IsDisposed;
|
||||||
|
|
||||||
|
@ -39,12 +39,11 @@ namespace MoonTools.ECS.Rev2
|
||||||
EmptyArchetype = CreateArchetype(ArchetypeSignature.Empty);
|
EmptyArchetype = CreateArchetype(ArchetypeSignature.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Archetype CreateArchetype(ArchetypeSignature signature)
|
internal Archetype CreateArchetype(ArchetypeSignature signature)
|
||||||
{
|
{
|
||||||
var archetypeId = ArchetypeIdAssigner.Assign();
|
var archetype = new Archetype(this, signature)
|
||||||
var archetype = new Archetype(archetypeId, signature)
|
|
||||||
{
|
{
|
||||||
Components = new List<Column>(signature.Count)
|
ComponentColumns = new List<Column>(signature.Count)
|
||||||
};
|
};
|
||||||
|
|
||||||
ArchetypeIndex.Add(signature, archetype);
|
ArchetypeIndex.Add(signature, archetype);
|
||||||
|
@ -52,9 +51,9 @@ namespace MoonTools.ECS.Rev2
|
||||||
for (int i = 0; i < signature.Count; i += 1)
|
for (int i = 0; i < signature.Count; i += 1)
|
||||||
{
|
{
|
||||||
var componentId = signature[i];
|
var componentId = signature[i];
|
||||||
ComponentIndex[componentId].Add(archetype); //, new ArchetypeRecord(i));
|
ComponentIndex[componentId].Add(archetype);
|
||||||
archetype.ComponentToColumnIndex.Add(componentId, archetype.Components.Count);
|
archetype.ComponentToColumnIndex.Add(componentId, archetype.ComponentColumns.Count);
|
||||||
archetype.Components.Add(new Column(ElementSizes[componentId]));
|
archetype.ComponentColumns.Add(new Column(ElementSizes[componentId]));
|
||||||
}
|
}
|
||||||
|
|
||||||
return archetype;
|
return archetype;
|
||||||
|
@ -63,12 +62,27 @@ namespace MoonTools.ECS.Rev2
|
||||||
public EntityId CreateEntity()
|
public EntityId CreateEntity()
|
||||||
{
|
{
|
||||||
var entityId = EntityIdAssigner.Assign();
|
var entityId = EntityIdAssigner.Assign();
|
||||||
var emptyArchetype = ArchetypeIndex[ArchetypeSignature.Empty];
|
EntityIndex.Add(entityId, new Record(EmptyArchetype, EmptyArchetype.Count));
|
||||||
EntityIndex.Add(entityId, new Record(emptyArchetype, emptyArchetype.Count));
|
EmptyArchetype.RowToEntity.Add(entityId);
|
||||||
emptyArchetype.RowToEntity.Add(entityId);
|
|
||||||
return entityId;
|
return entityId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: would be much more efficient to do all this at load time somehow
|
// FIXME: would be much more efficient to do all this at load time somehow
|
||||||
private void RegisterComponent<T>() where T : unmanaged
|
private void RegisterComponent<T>() where T : unmanaged
|
||||||
{
|
{
|
||||||
|
@ -80,12 +94,21 @@ namespace MoonTools.ECS.Rev2
|
||||||
|
|
||||||
private void TryRegisterComponentId<T>() where T : unmanaged
|
private void TryRegisterComponentId<T>() where T : unmanaged
|
||||||
{
|
{
|
||||||
if (!TypeToComponentId.TryGetValue(typeof(T), out var componentId))
|
if (!TypeToComponentId.ContainsKey(typeof(T)))
|
||||||
{
|
{
|
||||||
RegisterComponent<T>();
|
RegisterComponent<T>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// non-generic variant for use with Transfer
|
||||||
|
internal void AddComponentIndexEntry(ComponentId componentId)
|
||||||
|
{
|
||||||
|
if (!ComponentIndex.ContainsKey(componentId))
|
||||||
|
{
|
||||||
|
ComponentIndex.Add(componentId, new List<Archetype>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private ComponentId GetComponentId<T>() where T : unmanaged
|
private ComponentId GetComponentId<T>() where T : unmanaged
|
||||||
{
|
{
|
||||||
|
@ -106,7 +129,7 @@ namespace MoonTools.ECS.Rev2
|
||||||
|
|
||||||
var record = EntityIndex[entityId];
|
var record = EntityIndex[entityId];
|
||||||
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
|
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
|
||||||
var column = record.Archetype.Components[columnIndex];
|
var column = record.Archetype.ComponentColumns[columnIndex];
|
||||||
|
|
||||||
return ref ((T*) column.Elements)[record.Row];
|
return ref ((T*) column.Elements)[record.Row];
|
||||||
}
|
}
|
||||||
|
@ -120,7 +143,7 @@ namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
var record = EntityIndex[entityId];
|
var record = EntityIndex[entityId];
|
||||||
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
|
var columnIndex = record.Archetype.ComponentToColumnIndex[componentId];
|
||||||
var column = record.Archetype.Components[columnIndex];
|
var column = record.Archetype.ComponentColumns[columnIndex];
|
||||||
|
|
||||||
((T*) column.Elements)[record.Row] = component;
|
((T*) column.Elements)[record.Row] = component;
|
||||||
}
|
}
|
||||||
|
@ -165,7 +188,7 @@ namespace MoonTools.ECS.Rev2
|
||||||
|
|
||||||
// add the new component to the new archetype
|
// add the new component to the new archetype
|
||||||
var columnIndex = nextArchetype.ComponentToColumnIndex[componentId];
|
var columnIndex = nextArchetype.ComponentToColumnIndex[componentId];
|
||||||
var column = nextArchetype.Components[columnIndex];
|
var column = nextArchetype.ComponentColumns[columnIndex];
|
||||||
column.Append(component);
|
column.Append(component);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,9 +230,9 @@ namespace MoonTools.ECS.Rev2
|
||||||
var archetype = record.Archetype;
|
var archetype = record.Archetype;
|
||||||
var row = record.Row;
|
var row = record.Row;
|
||||||
|
|
||||||
for (int i = 0; i < archetype.Components.Count; i += 1)
|
for (int i = 0; i < archetype.ComponentColumns.Count; i += 1)
|
||||||
{
|
{
|
||||||
archetype.Components[i].Delete(row);
|
archetype.ComponentColumns[i].Delete(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (row != archetype.Count - 1)
|
if (row != archetype.Count - 1)
|
||||||
|
@ -227,16 +250,16 @@ namespace MoonTools.ECS.Rev2
|
||||||
|
|
||||||
private void MoveEntityToHigherArchetype(EntityId entityId, int row, Archetype from, Archetype to)
|
private void MoveEntityToHigherArchetype(EntityId entityId, int row, Archetype from, Archetype to)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < from.Components.Count; i += 1)
|
for (int i = 0; i < from.ComponentColumns.Count; i += 1)
|
||||||
{
|
{
|
||||||
var componentId = from.Signature[i];
|
var componentId = from.Signature[i];
|
||||||
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
|
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
|
||||||
|
|
||||||
// copy all components to higher archetype
|
// copy all components to higher archetype
|
||||||
from.Components[i].CopyToEnd(row, to.Components[destinationColumnIndex]);
|
from.ComponentColumns[i].CopyElementToEnd(row, to.ComponentColumns[destinationColumnIndex]);
|
||||||
|
|
||||||
// delete row on from archetype
|
// delete row on from archetype
|
||||||
from.Components[i].Delete(row);
|
from.ComponentColumns[i].Delete(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (row != from.Count - 1)
|
if (row != from.Count - 1)
|
||||||
|
@ -256,18 +279,18 @@ namespace MoonTools.ECS.Rev2
|
||||||
|
|
||||||
private void MoveEntityToLowerArchetype(EntityId entityId, int row, Archetype from, Archetype to, ComponentId removed)
|
private void MoveEntityToLowerArchetype(EntityId entityId, int row, Archetype from, Archetype to, ComponentId removed)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < from.Components.Count; i += 1)
|
for (int i = 0; i < from.ComponentColumns.Count; i += 1)
|
||||||
{
|
{
|
||||||
var componentId = from.Signature[i];
|
var componentId = from.Signature[i];
|
||||||
|
|
||||||
// delete the row
|
// delete the row
|
||||||
from.Components[i].Delete(row);
|
from.ComponentColumns[i].Delete(row);
|
||||||
|
|
||||||
// if this isn't the removed component, copy to the lower archetype
|
// if this isn't the removed component, copy to the lower archetype
|
||||||
if (componentId != removed)
|
if (componentId != removed)
|
||||||
{
|
{
|
||||||
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
|
var destinationColumnIndex = to.ComponentToColumnIndex[componentId];
|
||||||
from.Components[i].CopyToEnd(row, to.Components[destinationColumnIndex]);
|
from.ComponentColumns[i].CopyElementToEnd(row, to.ComponentColumns[destinationColumnIndex]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,11 +316,11 @@ namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
var componentIdOne = archetype.Signature[0];
|
var componentIdOne = archetype.Signature[0];
|
||||||
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
|
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
|
||||||
var columnOneElements = archetype.Components[columnIndexOne].Elements;
|
var columnOneElements = archetype.ComponentColumns[columnIndexOne].Elements;
|
||||||
|
|
||||||
var componentIdTwo = archetype.Signature[1];
|
var componentIdTwo = archetype.Signature[1];
|
||||||
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
|
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
|
||||||
var columnTwoElements = archetype.Components[columnIndexTwo].Elements;
|
var columnTwoElements = archetype.ComponentColumns[columnIndexTwo].Elements;
|
||||||
|
|
||||||
for (int i = archetype.Count - 1; i >= 0; i -= 1)
|
for (int i = archetype.Count - 1; i >= 0; i -= 1)
|
||||||
{
|
{
|
||||||
|
@ -312,11 +335,11 @@ namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
var componentIdOne = archetype.Signature[0];
|
var componentIdOne = archetype.Signature[0];
|
||||||
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
|
var columnIndexOne = archetype.ComponentToColumnIndex[componentIdOne];
|
||||||
var columnOneElements = archetype.Components[columnIndexOne].Elements;
|
var columnOneElements = archetype.ComponentColumns[columnIndexOne].Elements;
|
||||||
|
|
||||||
var componentIdTwo = archetype.Signature[1];
|
var componentIdTwo = archetype.Signature[1];
|
||||||
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
|
var columnIndexTwo = archetype.ComponentToColumnIndex[componentIdTwo];
|
||||||
var columnTwoElements = archetype.Components[columnIndexTwo].Elements;
|
var columnTwoElements = archetype.ComponentColumns[columnIndexTwo].Elements;
|
||||||
|
|
||||||
for (int i = archetype.Count - 1; i >= 0; i -= 1)
|
for (int i = archetype.Count - 1; i >= 0; i -= 1)
|
||||||
{
|
{
|
||||||
|
@ -336,7 +359,7 @@ namespace MoonTools.ECS.Rev2
|
||||||
{
|
{
|
||||||
for (var i = 0; i < archetype.Signature.Count; i += 1)
|
for (var i = 0; i < archetype.Signature.Count; i += 1)
|
||||||
{
|
{
|
||||||
archetype.Components[i].Dispose();
|
archetype.ComponentColumns[i].Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue