MoonTools.ECS/src/Rev2/Snapshot.cs

133 lines
2.9 KiB
C#
Raw Normal View History

2023-10-25 01:44:41 +00:00
using System.Collections.Generic;
2023-10-30 19:11:50 +00:00
using MoonTools.ECS.Collections;
2023-10-25 01:44:41 +00:00
namespace MoonTools.ECS.Rev2;
public class Snapshot
{
private Dictionary<ArchetypeSignature, ArchetypeSnapshot> ArchetypeSnapshots =
new Dictionary<ArchetypeSignature, ArchetypeSnapshot>();
2023-10-30 19:11:50 +00:00
private Dictionary<Id, Record> EntityIndex = new Dictionary<Id, Record>();
private IdAssigner IdAssigner = new IdAssigner();
2023-10-25 01:44:41 +00:00
public int Count
{
get
{
var count = 0;
foreach (var snapshot in ArchetypeSnapshots.Values)
{
count += snapshot.Count;
}
return count;
}
}
public void Restore(World world)
{
2023-10-30 19:11:50 +00:00
// restore archetype storage
2023-10-25 01:44:41 +00:00
foreach (var (archetypeSignature, archetypeSnapshot) in ArchetypeSnapshots)
{
var archetype = world.ArchetypeIndex[archetypeSignature];
RestoreArchetypeSnapshot(archetype);
}
2023-10-30 19:11:50 +00:00
// restore entity index
world.EntityIndex.Clear();
foreach (var (id, record) in EntityIndex)
{
world.EntityIndex[id] = record;
}
// restore id assigner state
IdAssigner.CopyTo(world.IdAssigner);
2023-10-25 01:44:41 +00:00
}
2023-10-30 19:11:50 +00:00
public void Take(World world)
2023-10-25 01:44:41 +00:00
{
2023-10-30 19:11:50 +00:00
// copy id assigner state
world.IdAssigner.CopyTo(IdAssigner);
// copy entity index
EntityIndex.Clear();
foreach (var (id, record) in world.EntityIndex)
{
EntityIndex[id] = record;
}
// copy archetypes
foreach (var archetype in world.ArchetypeIndex.Values)
2023-10-25 01:44:41 +00:00
{
2023-10-30 19:11:50 +00:00
TakeArchetypeSnapshot(archetype);
2023-10-25 01:44:41 +00:00
}
}
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;
2023-10-30 19:11:50 +00:00
private readonly Column[] ComponentColumns;
private readonly NativeArray<Id> RowToEntity;
2023-10-25 01:44:41 +00:00
2023-10-30 19:11:50 +00:00
public int Count => RowToEntity.Count;
2023-10-25 01:44:41 +00:00
public ArchetypeSnapshot(ArchetypeSignature signature)
{
Signature = signature;
2023-10-30 19:11:50 +00:00
ComponentColumns = new Column[signature.Count];
RowToEntity = new NativeArray<Id>();
2023-10-25 01:44:41 +00:00
for (int i = 0; i < signature.Count; i += 1)
{
var componentId = signature[i];
2023-10-30 19:11:50 +00:00
ComponentColumns[i] = new Column(World.ElementSizes[componentId]);
2023-10-25 01:44:41 +00:00
}
}
2023-10-30 19:11:50 +00:00
public void Clear()
{
RowToEntity.Clear();
}
2023-10-25 01:44:41 +00:00
public void Take(Archetype archetype)
{
2023-10-30 19:11:50 +00:00
for (int i = 0; i < ComponentColumns.Length; i += 1)
2023-10-25 01:44:41 +00:00
{
archetype.ComponentColumns[i].CopyAllTo(ComponentColumns[i]);
}
2023-10-30 19:11:50 +00:00
archetype.RowToEntity.CopyTo(RowToEntity);
2023-10-25 01:44:41 +00:00
}
public void Restore(Archetype archetype)
{
// Copy all component data
2023-10-30 19:11:50 +00:00
for (int i = 0; i < ComponentColumns.Length; i += 1)
2023-10-25 01:44:41 +00:00
{
ComponentColumns[i].CopyAllTo(archetype.ComponentColumns[i]);
}
2023-10-30 19:11:50 +00:00
RowToEntity.CopyTo(archetype.RowToEntity);
2023-10-25 01:44:41 +00:00
}
}
}