From 5ba5c2f5cdc444311808f6e72ea3ddef87541d72 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 31 Oct 2023 15:00:42 -0700 Subject: [PATCH] split Id to EntityId and TypeId --- src/Rev2/Archetype.cs | 8 +- src/Rev2/ArchetypeSignature.cs | 6 +- .../Compatibility/EntityComponentReader.cs | 24 ++--- src/Rev2/Compatibility/Manipulator.cs | 10 +- src/Rev2/EntityId.cs | 11 +++ src/Rev2/Filter.cs | 18 ++-- src/Rev2/FilterBuilder.cs | 10 +- src/Rev2/Id.cs | 11 --- src/Rev2/IdAssigner.cs | 8 +- src/Rev2/RelationStorage.cs | 56 +++++------ src/Rev2/Snapshot.cs | 28 +++--- src/Rev2/TypeId.cs | 11 +++ src/Rev2/World.cs | 98 +++++++++---------- 13 files changed, 152 insertions(+), 147 deletions(-) create mode 100644 src/Rev2/EntityId.cs delete mode 100644 src/Rev2/Id.cs create mode 100644 src/Rev2/TypeId.cs diff --git a/src/Rev2/Archetype.cs b/src/Rev2/Archetype.cs index 0780bfa..334b6cc 100644 --- a/src/Rev2/Archetype.cs +++ b/src/Rev2/Archetype.cs @@ -8,11 +8,11 @@ internal class Archetype public World World; public ArchetypeSignature Signature; public NativeArray[] ComponentColumns; - public NativeArray RowToEntity = new NativeArray(); + public NativeArray RowToEntity = new NativeArray(); - public Dictionary ComponentToColumnIndex = - new Dictionary(); - public SortedDictionary Edges = new SortedDictionary(); + public Dictionary ComponentToColumnIndex = + new Dictionary(); + public SortedDictionary Edges = new SortedDictionary(); public int Count => RowToEntity.Count; diff --git a/src/Rev2/ArchetypeSignature.cs b/src/Rev2/ArchetypeSignature.cs index 5a33783..af5dc99 100644 --- a/src/Rev2/ArchetypeSignature.cs +++ b/src/Rev2/ArchetypeSignature.cs @@ -11,7 +11,7 @@ internal class ArchetypeSignature : IEquatable public int Count => Ids.Count; - public Id this[int i] => new Id(Ids[i]); + public TypeId this[int i] => new TypeId(Ids[i]); public ArchetypeSignature() { @@ -24,7 +24,7 @@ internal class ArchetypeSignature : IEquatable } // Maintains sorted order - public void Insert(Id componentId) + public void Insert(TypeId componentId) { var index = Ids.BinarySearch(componentId.Value); @@ -34,7 +34,7 @@ internal class ArchetypeSignature : IEquatable } } - public void Remove(Id componentId) + public void Remove(TypeId componentId) { var index = Ids.BinarySearch(componentId.Value); diff --git a/src/Rev2/Compatibility/EntityComponentReader.cs b/src/Rev2/Compatibility/EntityComponentReader.cs index 715c247..300d0e5 100644 --- a/src/Rev2/Compatibility/EntityComponentReader.cs +++ b/src/Rev2/Compatibility/EntityComponentReader.cs @@ -9,21 +9,21 @@ public abstract class EntityComponentReader World = world; } - protected bool Has(in Id entityId) where T : unmanaged => World.Has(entityId); + protected bool Has(in EntityId entityId) where T : unmanaged => World.Has(entityId); protected bool Some() where T : unmanaged => World.Some(); - protected ref T Get(in Id entityId) where T : unmanaged => ref World.Get(entityId); + protected ref T Get(in EntityId entityId) where T : unmanaged => ref World.Get(entityId); protected ref T GetSingleton() where T : unmanaged => ref World.GetSingleton(); - protected Id GetSingletonEntity() where T : unmanaged => World.GetSingletonEntity(); + protected EntityId GetSingletonEntity() where T : unmanaged => World.GetSingletonEntity(); - protected ReverseSpanEnumerator<(Id, Id)> Relations() where T : unmanaged => World.Relations(); - protected bool Related(in Id entityA, in Id entityB) where T : unmanaged => World.Related(entityA, entityB); - protected T GetRelationData(in Id entityA, in Id entityB) where T : unmanaged => World.GetRelationData(entityA, entityB); + protected ReverseSpanEnumerator<(EntityId, EntityId)> Relations() where T : unmanaged => World.Relations(); + protected bool Related(in EntityId entityA, in EntityId entityB) where T : unmanaged => World.Related(entityA, entityB); + protected T GetRelationData(in EntityId entityA, in EntityId entityB) where T : unmanaged => World.GetRelationData(entityA, entityB); - protected ReverseSpanEnumerator OutRelations(in Id entity) where T : unmanaged => World.OutRelations(entity); - protected Id OutRelationSingleton(in Id entity) where T : unmanaged => World.OutRelationSingleton(entity); - protected bool HasOutRelation(in Id entity) where T : unmanaged => World.HasOutRelation(entity); + protected ReverseSpanEnumerator OutRelations(in EntityId entity) where T : unmanaged => World.OutRelations(entity); + protected EntityId OutRelationSingleton(in EntityId entity) where T : unmanaged => World.OutRelationSingleton(entity); + protected bool HasOutRelation(in EntityId entity) where T : unmanaged => World.HasOutRelation(entity); - protected ReverseSpanEnumerator InRelations(in Id entity) where T : unmanaged => World.InRelations(entity); - protected Id InRelationSingleton(in Id entity) where T : unmanaged => World.InRelationSingleton(entity); - protected bool HasInRelation(in Id entity) where T : unmanaged => World.HasInRelation(entity); + protected ReverseSpanEnumerator InRelations(in EntityId entity) where T : unmanaged => World.InRelations(entity); + protected EntityId InRelationSingleton(in EntityId entity) where T : unmanaged => World.InRelationSingleton(entity); + protected bool HasInRelation(in EntityId entity) where T : unmanaged => World.HasInRelation(entity); } diff --git a/src/Rev2/Compatibility/Manipulator.cs b/src/Rev2/Compatibility/Manipulator.cs index 61bce02..cef513c 100644 --- a/src/Rev2/Compatibility/Manipulator.cs +++ b/src/Rev2/Compatibility/Manipulator.cs @@ -4,10 +4,10 @@ public class Manipulator : EntityComponentReader { public Manipulator(World world) : base(world) { } - protected void Set(in Id entity, in TComponent component) where TComponent : unmanaged => World.Set(entity, component); - protected void Remove(in Id entity) where TComponent : unmanaged => World.Remove(entity); + protected void Set(in EntityId entity, in TComponent component) where TComponent : unmanaged => World.Set(entity, component); + protected void Remove(in EntityId entity) where TComponent : unmanaged => World.Remove(entity); - protected void Unrelate(in Id entityA, in Id entityB) where TRelationKind : unmanaged => World.Unrelate(entityA, entityB); - protected void UnrelateAll(in Id entity) where TRelationKind : unmanaged => World.UnrelateAll(entity); - protected void Destroy(in Id entity) => World.Destroy(entity); + protected void Unrelate(in EntityId entityA, in EntityId entityB) where TRelationKind : unmanaged => World.Unrelate(entityA, entityB); + protected void UnrelateAll(in EntityId entity) where TRelationKind : unmanaged => World.UnrelateAll(entity); + protected void Destroy(in EntityId entity) => World.Destroy(entity); } diff --git a/src/Rev2/EntityId.cs b/src/Rev2/EntityId.cs new file mode 100644 index 0000000..d6d6f31 --- /dev/null +++ b/src/Rev2/EntityId.cs @@ -0,0 +1,11 @@ +using System; + +namespace MoonTools.ECS.Rev2; + +public readonly record struct EntityId(uint Value) : IComparable +{ + public int CompareTo(EntityId other) + { + return Value.CompareTo(other.Value); + } +} diff --git a/src/Rev2/Filter.cs b/src/Rev2/Filter.cs index aabe087..5a17fa8 100644 --- a/src/Rev2/Filter.cs +++ b/src/Rev2/Filter.cs @@ -7,8 +7,8 @@ namespace MoonTools.ECS.Rev2; public class Filter { private Archetype EmptyArchetype; - private HashSet Included; - private HashSet Excluded; + private HashSet Included; + private HashSet Excluded; public EntityEnumerator Entities => new EntityEnumerator(this); internal ArchetypeEnumerator Archetypes => new ArchetypeEnumerator(this); @@ -47,7 +47,7 @@ public class Filter } } - public Id RandomEntity + public EntityId RandomEntity { get { @@ -57,7 +57,7 @@ public class Filter } // WARNING: this WILL crash if the index is out of range! - public Id NthEntity(int index) + public EntityId NthEntity(int index) { var count = 0; @@ -83,7 +83,7 @@ public class Filter } } - internal Filter(Archetype emptyArchetype, HashSet included, HashSet excluded) + internal Filter(Archetype emptyArchetype, HashSet included, HashSet excluded) { EmptyArchetype = emptyArchetype; Included = included; @@ -155,12 +155,12 @@ public class Filter public ref struct EntityEnumerator { - private Id CurrentEntity; + private EntityId CurrentEntity; public EntityEnumerator GetEnumerator() => this; // TODO: pool this - Queue EntityQueue = new Queue(); + Queue EntityQueue = new Queue(); internal EntityEnumerator(Filter filter) { @@ -180,7 +180,7 @@ public class Filter return EntityQueue.TryDequeue(out CurrentEntity); } - public Id Current => CurrentEntity; + public EntityId Current => CurrentEntity; } public ref struct RandomEntityEnumerator @@ -198,6 +198,6 @@ public class Filter } public bool MoveNext() => LinearCongruentialEnumerator.MoveNext(); - public Id Current => Filter.NthEntity(LinearCongruentialEnumerator.Current); + public EntityId Current => Filter.NthEntity(LinearCongruentialEnumerator.Current); } } diff --git a/src/Rev2/FilterBuilder.cs b/src/Rev2/FilterBuilder.cs index a8c0595..89d7e66 100644 --- a/src/Rev2/FilterBuilder.cs +++ b/src/Rev2/FilterBuilder.cs @@ -5,17 +5,17 @@ namespace MoonTools.ECS.Rev2; public ref struct FilterBuilder { World World; - HashSet Included; - HashSet Excluded; + HashSet Included; + HashSet Excluded; internal FilterBuilder(World world) { World = world; - Included = new HashSet(); - Excluded = new HashSet(); + Included = new HashSet(); + Excluded = new HashSet(); } - private FilterBuilder(World world, HashSet included, HashSet excluded) + private FilterBuilder(World world, HashSet included, HashSet excluded) { World = world; Included = included; diff --git a/src/Rev2/Id.cs b/src/Rev2/Id.cs deleted file mode 100644 index cc291de..0000000 --- a/src/Rev2/Id.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace MoonTools.ECS.Rev2; - -public readonly record struct Id(uint Value) : IComparable -{ - public int CompareTo(Id other) - { - return Value.CompareTo(other.Value); - } -} diff --git a/src/Rev2/IdAssigner.cs b/src/Rev2/IdAssigner.cs index 3b0a482..502e8b8 100644 --- a/src/Rev2/IdAssigner.cs +++ b/src/Rev2/IdAssigner.cs @@ -7,7 +7,7 @@ internal class IdAssigner uint Next; NativeArray AvailableIds = new NativeArray(); - public Id Assign() + public uint Assign() { if (!AvailableIds.TryPop(out var id)) { @@ -15,12 +15,12 @@ internal class IdAssigner Next += 1; } - return new Id(id); + return id; } - public void Unassign(Id id) + public void Unassign(uint id) { - AvailableIds.Add(id.Value); + AvailableIds.Add(id); } public void CopyTo(IdAssigner other) diff --git a/src/Rev2/RelationStorage.cs b/src/Rev2/RelationStorage.cs index b249b17..7bd41d0 100644 --- a/src/Rev2/RelationStorage.cs +++ b/src/Rev2/RelationStorage.cs @@ -10,25 +10,25 @@ internal class RelationStorage { internal NativeArray relations; internal NativeArray relationDatas; - internal Dictionary<(Id, Id), int> indices = new Dictionary<(Id, Id), int>(16); - internal Dictionary> outRelations = new Dictionary>(16); - internal Dictionary> inRelations = new Dictionary>(16); - private Stack> listPool = new Stack>(); + internal Dictionary<(EntityId, EntityId), int> indices = new Dictionary<(EntityId, EntityId), int>(16); + internal Dictionary> outRelations = new Dictionary>(16); + internal Dictionary> inRelations = new Dictionary>(16); + private Stack> listPool = new Stack>(); private bool disposed; public RelationStorage(int relationDataSize) { - relations = new NativeArray(Unsafe.SizeOf<(Id, Id)>()); + relations = new NativeArray(Unsafe.SizeOf<(EntityId, EntityId)>()); relationDatas = new NativeArray(relationDataSize); } - public ReverseSpanEnumerator<(Id, Id)> All() + public ReverseSpanEnumerator<(EntityId, EntityId)> All() { - return new ReverseSpanEnumerator<(Id, Id)>(relations.ToSpan<(Id, Id)>()); + return new ReverseSpanEnumerator<(EntityId, EntityId)>(relations.ToSpan<(EntityId, EntityId)>()); } - public unsafe void Set(in Id entityA, in Id entityB, in T relationData) where T : unmanaged + public unsafe void Set(in EntityId entityA, in EntityId entityB, in T relationData) where T : unmanaged { var relation = (entityA, entityB); @@ -55,18 +55,18 @@ internal class RelationStorage indices.Add(relation, relations.Count - 1); } - public ref T Get(in Id entityA, in Id entityB) where T : unmanaged + public ref T Get(in EntityId entityA, in EntityId entityB) where T : unmanaged { var relationIndex = indices[(entityA, entityB)]; return ref relationDatas.Get(relationIndex); } - public bool Has(Id entityA, Id entityB) + public bool Has(EntityId entityA, EntityId entityB) { return indices.ContainsKey((entityA, entityB)); } - public ReverseSpanEnumerator OutRelations(Id entityID) + public ReverseSpanEnumerator OutRelations(EntityId entityID) { if (outRelations.TryGetValue(entityID, out var entityOutRelations)) { @@ -74,16 +74,16 @@ internal class RelationStorage } else { - return ReverseSpanEnumerator.Empty; + return ReverseSpanEnumerator.Empty; } } - public Id OutFirst(Id entityID) + public EntityId OutFirst(EntityId entityID) { return OutNth(entityID, 0); } - public Id OutNth(Id entityID, int n) + public EntityId OutNth(EntityId entityID, int n) { #if DEBUG if (!outRelations.ContainsKey(entityID) || outRelations[entityID].Count == 0) @@ -94,17 +94,17 @@ internal class RelationStorage return outRelations[entityID][n]; } - public bool HasOutRelation(Id entityID) + public bool HasOutRelation(EntityId entityID) { return outRelations.ContainsKey(entityID) && outRelations[entityID].Count > 0; } - public int OutRelationCount(Id entityID) + public int OutRelationCount(EntityId entityID) { return outRelations.TryGetValue(entityID, out var entityOutRelations) ? entityOutRelations.Count : 0; } - public ReverseSpanEnumerator InRelations(Id entityID) + public ReverseSpanEnumerator InRelations(EntityId entityID) { if (inRelations.TryGetValue(entityID, out var entityInRelations)) { @@ -112,16 +112,16 @@ internal class RelationStorage } else { - return ReverseSpanEnumerator.Empty; + return ReverseSpanEnumerator.Empty; } } - public Id InFirst(Id entityID) + public EntityId InFirst(EntityId entityID) { return InNth(entityID, 0); } - public Id InNth(Id entityID, int n) + public EntityId InNth(EntityId entityID, int n) { #if DEBUG if (!inRelations.ContainsKey(entityID) || inRelations[entityID].Count == 0) @@ -133,17 +133,17 @@ internal class RelationStorage return inRelations[entityID][n]; } - public bool HasInRelation(Id entityID) + public bool HasInRelation(EntityId entityID) { return inRelations.ContainsKey(entityID) && inRelations[entityID].Count > 0; } - public int InRelationCount(Id entityID) + public int InRelationCount(EntityId entityID) { return inRelations.TryGetValue(entityID, out var entityInRelations) ? entityInRelations.Count : 0; } - public (bool, bool) Remove(in Id entityA, in Id entityB) + public (bool, bool) Remove(in EntityId entityA, in EntityId entityB) { var aEmpty = false; var bEmpty = false; @@ -177,7 +177,7 @@ internal class RelationStorage // move an element into the hole if (index != lastElementIndex) { - var lastRelation = relations.Get<(Id, Id)>(lastElementIndex); + var lastRelation = relations.Get<(EntityId, EntityId)>(lastElementIndex); indices[lastRelation] = index; } @@ -187,7 +187,7 @@ internal class RelationStorage return (aEmpty, bEmpty); } - public void RemoveEntity(in Id entity) + public void RemoveEntity(in EntityId entity) { if (outRelations.TryGetValue(entity, out var entityOutRelations)) { @@ -212,17 +212,17 @@ internal class RelationStorage } } - internal IndexableSet AcquireHashSetFromPool() + internal IndexableSet AcquireHashSetFromPool() { if (listPool.Count == 0) { - listPool.Push(new IndexableSet()); + listPool.Push(new IndexableSet()); } return listPool.Pop(); } - private void ReturnHashSetToPool(IndexableSet hashSet) + private void ReturnHashSetToPool(IndexableSet hashSet) { hashSet.Clear(); listPool.Push(hashSet); diff --git a/src/Rev2/Snapshot.cs b/src/Rev2/Snapshot.cs index 6dd7231..50dafa6 100644 --- a/src/Rev2/Snapshot.cs +++ b/src/Rev2/Snapshot.cs @@ -9,15 +9,15 @@ public class Snapshot private Dictionary ArchetypeSnapshots = new Dictionary(); - private Dictionary RelationSnapshots = - new Dictionary(); + private Dictionary RelationSnapshots = + new Dictionary(); - private Dictionary EntityIndex = new Dictionary(); + private Dictionary EntityIndex = new Dictionary(); - private Dictionary> EntityRelationIndex = - new Dictionary>(); + private Dictionary> EntityRelationIndex = + new Dictionary>(); - private IdAssigner IdAssigner = new IdAssigner(); + private IdAssigner EntityIdAssigner = new IdAssigner(); public int Count { @@ -51,7 +51,7 @@ public class Snapshot } // restore id assigner state - IdAssigner.CopyTo(world.IdAssigner); + EntityIdAssigner.CopyTo(world.EntityIdAssigner); // restore relation state foreach (var (typeId, relationSnapshot) in RelationSnapshots) @@ -76,7 +76,7 @@ public class Snapshot public void Take(World world) { // copy id assigner state - world.IdAssigner.CopyTo(IdAssigner); + world.EntityIdAssigner.CopyTo(EntityIdAssigner); // copy entity index EntityIndex.Clear(); @@ -103,7 +103,7 @@ public class Snapshot { if (!EntityRelationIndex.ContainsKey(id)) { - EntityRelationIndex.Add(id, new IndexableSet()); + EntityRelationIndex.Add(id, new IndexableSet()); } EntityRelationIndex[id].Clear(); @@ -126,7 +126,7 @@ public class Snapshot archetypeSnapshot.Take(archetype); } - private void TakeRelationSnapshot(Id typeId, RelationStorage relationStorage) + private void TakeRelationSnapshot(TypeId typeId, RelationStorage relationStorage) { if (!RelationSnapshots.TryGetValue(typeId, out var snapshot)) { @@ -140,14 +140,14 @@ public class Snapshot private class ArchetypeSnapshot { private readonly NativeArray[] ComponentColumns; - private readonly NativeArray RowToEntity; + private readonly NativeArray RowToEntity; public int Count => RowToEntity.Count; public ArchetypeSnapshot(ArchetypeSignature signature) { ComponentColumns = new NativeArray[signature.Count]; - RowToEntity = new NativeArray(); + RowToEntity = new NativeArray(); for (int i = 0; i < signature.Count; i += 1) { @@ -185,7 +185,7 @@ public class Snapshot public RelationSnapshot(int elementSize) { - Relations = new NativeArray(Unsafe.SizeOf<(Id, Id)>()); + Relations = new NativeArray(Unsafe.SizeOf<(EntityId, EntityId)>()); RelationDatas = new NativeArray(elementSize); } @@ -204,7 +204,7 @@ public class Snapshot for (int index = 0; index < Relations.Count; index += 1) { - var relation = Relations.Get<(Id, Id)>(index); + var relation = Relations.Get<(EntityId, EntityId)>(index); relationStorage.indices[relation] = index; relationStorage.indices[relation] = index; diff --git a/src/Rev2/TypeId.cs b/src/Rev2/TypeId.cs new file mode 100644 index 0000000..728f89f --- /dev/null +++ b/src/Rev2/TypeId.cs @@ -0,0 +1,11 @@ +using System; + +namespace MoonTools.ECS.Rev2; + +public readonly record struct TypeId(uint Value) : IComparable +{ + public int CompareTo(TypeId other) + { + return Value.CompareTo(other.Value); + } +} diff --git a/src/Rev2/World.cs b/src/Rev2/World.cs index 864aa39..76c0598 100644 --- a/src/Rev2/World.cs +++ b/src/Rev2/World.cs @@ -7,35 +7,35 @@ namespace MoonTools.ECS.Rev2; public class World : IDisposable { - // Get ComponentId from a Type - internal static Dictionary TypeToId = new Dictionary(); - // Get element size from a ComponentId - internal static Dictionary ElementSizes = new Dictionary(); + // Get TypeId from a Type + internal static Dictionary TypeToId = new Dictionary(); + // Get element size from a TypeId + internal static Dictionary ElementSizes = new Dictionary(); // Lookup from ArchetypeSignature to Archetype internal Dictionary ArchetypeIndex = new Dictionary(); // Going from EntityId to Archetype and storage row - internal Dictionary EntityIndex = new Dictionary(); + internal Dictionary EntityIndex = new Dictionary(); // Relation Storages - internal Dictionary RelationIndex = - new Dictionary(); + internal Dictionary RelationIndex = + new Dictionary(); // Entity Relation Tracking - internal Dictionary> EntityRelationIndex = - new Dictionary>(); + internal Dictionary> EntityRelationIndex = + new Dictionary>(); // Message Storages - private Dictionary MessageIndex = - new Dictionary(); + private Dictionary MessageIndex = + new Dictionary(); // Filters with a single Include type for Singleton/Some implementation - private Dictionary SingleTypeFilters = new Dictionary(); + private Dictionary SingleTypeFilters = new Dictionary(); // ID Management - // FIXME: Entity and Type Ids should be separated - internal IdAssigner IdAssigner = new IdAssigner(); + internal IdAssigner EntityIdAssigner = new IdAssigner(); + internal IdAssigner TypeIdAssigner = new IdAssigner(); internal readonly Archetype EmptyArchetype; @@ -67,28 +67,28 @@ public class World : IDisposable return archetype; } - public Id CreateEntity(string tag = "") + public EntityId CreateEntity(string tag = "") { - var entityId = IdAssigner.Assign(); + var entityId = new EntityId(EntityIdAssigner.Assign()); EntityIndex.Add(entityId, new Record(EmptyArchetype, EmptyArchetype.Count)); EmptyArchetype.RowToEntity.Add(entityId); if (!EntityRelationIndex.ContainsKey(entityId)) { - EntityRelationIndex.Add(entityId, new IndexableSet()); + EntityRelationIndex.Add(entityId, new IndexableSet()); } return entityId; } - internal Id GetTypeId() where T : unmanaged + internal TypeId GetTypeId() where T : unmanaged { if (TypeToId.ContainsKey(typeof(T))) { return TypeToId[typeof(T)]; } - var typeId = IdAssigner.Assign(); + var typeId = new TypeId(TypeIdAssigner.Assign()); TypeToId.Add(typeof(T), typeId); ElementSizes.Add(typeId, Unsafe.SizeOf()); return typeId; @@ -104,12 +104,12 @@ public class World : IDisposable } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private Id GetComponentId() where T : unmanaged + private TypeId GetComponentId() where T : unmanaged { return TypeToId[typeof(T)]; } - private void RegisterRelationType(Id typeId) + private void RegisterRelationType(TypeId typeId) { RelationIndex.Add(typeId, new RelationStorage(ElementSizes[typeId])); } @@ -131,7 +131,7 @@ public class World : IDisposable // Messages - private Id GetMessageTypeId() where T : unmanaged + private TypeId GetMessageTypeId() where T : unmanaged { var typeId = GetTypeId(); @@ -176,7 +176,7 @@ public class World : IDisposable } // Components - public bool Has(Id entityId) where T : unmanaged + public bool Has(EntityId entityId) where T : unmanaged { var componentId = GetComponentId(); var record = EntityIndex[entityId]; @@ -190,7 +190,7 @@ public class World : IDisposable } // will throw if non-existent - public unsafe ref T Get(Id entityId) where T : unmanaged + public unsafe ref T Get(EntityId entityId) where T : unmanaged { var componentId = GetComponentId(); @@ -217,7 +217,7 @@ public class World : IDisposable throw new InvalidOperationException("No component of this type exists!"); } - public Id GetSingletonEntity() where T : unmanaged + public EntityId GetSingletonEntity() where T : unmanaged { var componentId = GetComponentId(); @@ -232,7 +232,7 @@ public class World : IDisposable throw new InvalidOperationException("No entity with this component type exists!"); } - public unsafe void Set(in Id entityId, in T component) where T : unmanaged + public unsafe void Set(in EntityId entityId, in T component) where T : unmanaged { TryRegisterComponentId(); var componentId = GetComponentId(); @@ -251,7 +251,7 @@ public class World : IDisposable } } - private void Add(Id entityId, in T component) where T : unmanaged + private void Add(EntityId entityId, in T component) where T : unmanaged { Archetype? nextArchetype; @@ -290,7 +290,7 @@ public class World : IDisposable column.Append(component); } - public void Remove(Id entityId) where T : unmanaged + public void Remove(EntityId entityId) where T : unmanaged { Archetype? nextArchetype; @@ -322,7 +322,7 @@ public class World : IDisposable MoveEntityToLowerArchetype(entityId, row, archetype, nextArchetype, componentId); } - public void Relate(in Id entityA, in Id entityB, in T relation) where T : unmanaged + public void Relate(in EntityId entityA, in EntityId entityB, in T relation) where T : unmanaged { TryRegisterRelationType(); var relationStorage = GetRelationStorage(); @@ -331,83 +331,77 @@ public class World : IDisposable EntityRelationIndex[entityB].Add(TypeToId[typeof(T)]); } - public void Unrelate(in Id entityA, in Id entityB) where T : unmanaged + public void Unrelate(in EntityId entityA, in EntityId entityB) where T : unmanaged { var relationStorage = GetRelationStorage(); relationStorage.Remove(entityA, entityB); } - public void UnrelateAll(in Id entity) where T : unmanaged + public void UnrelateAll(in EntityId entity) where T : unmanaged { var relationStorage = GetRelationStorage(); relationStorage.RemoveEntity(entity); } - public bool Related(in Id entityA, in Id entityB) where T : unmanaged + public bool Related(in EntityId entityA, in EntityId entityB) where T : unmanaged { var relationStorage = GetRelationStorage(); return relationStorage.Has(entityA, entityB); } - public T GetRelationData(in Id entityA, in Id entityB) where T : unmanaged + public T GetRelationData(in EntityId entityA, in EntityId entityB) where T : unmanaged { var relationStorage = GetRelationStorage(); return relationStorage.Get(entityA, entityB); } - public ReverseSpanEnumerator<(Id, Id)> Relations() where T : unmanaged + public ReverseSpanEnumerator<(EntityId, EntityId)> Relations() where T : unmanaged { var relationStorage = GetRelationStorage(); return relationStorage.All(); } - public ReverseSpanEnumerator OutRelations(Id entity) where T : unmanaged + public ReverseSpanEnumerator OutRelations(EntityId entity) where T : unmanaged { var relationStorage = GetRelationStorage(); return relationStorage.OutRelations(entity); } - public Id OutRelationSingleton(in Id entity) where T : unmanaged + public EntityId OutRelationSingleton(in EntityId entity) where T : unmanaged { var relationStorage = GetRelationStorage(); return relationStorage.OutFirst(entity); } - public bool HasOutRelation(in Id entity) where T : unmanaged + public bool HasOutRelation(in EntityId entity) where T : unmanaged { var relationStorage = GetRelationStorage(); return relationStorage.HasOutRelation(entity); } - public ReverseSpanEnumerator InRelations(Id entity) where T : unmanaged + public ReverseSpanEnumerator InRelations(EntityId entity) where T : unmanaged { var relationStorage = GetRelationStorage(); return relationStorage.InRelations(entity); } - public Id InRelationSingleton(in Id entity) where T : unmanaged + public EntityId InRelationSingleton(in EntityId entity) where T : unmanaged { var relationStorage = GetRelationStorage(); return relationStorage.InFirst(entity); } - public bool HasInRelation(in Id entity) where T : unmanaged + public bool HasInRelation(in EntityId entity) where T : unmanaged { var relationStorage = GetRelationStorage(); return relationStorage.HasInRelation(entity); } - private bool Has(Id entityId, Id typeId) - { - var record = EntityIndex[entityId]; - return record.Archetype.ComponentToColumnIndex.ContainsKey(typeId); - } - // used as a fast path by Archetype.ClearAll and snapshot restore - internal void FreeEntity(Id entityId) + internal void FreeEntity(EntityId entityId) { EntityIndex.Remove(entityId); - IdAssigner.Unassign(entityId); + EntityIdAssigner.Unassign(entityId.Value); foreach (var relationTypeIndex in EntityRelationIndex[entityId]) { @@ -418,7 +412,7 @@ public class World : IDisposable EntityRelationIndex[entityId].Clear(); } - public void Destroy(Id entityId) + public void Destroy(EntityId entityId) { var record = EntityIndex[entityId]; var archetype = record.Archetype; @@ -439,7 +433,7 @@ public class World : IDisposable archetype.RowToEntity.RemoveLastElement(); EntityIndex.Remove(entityId); - IdAssigner.Unassign(entityId); + EntityIdAssigner.Unassign(entityId.Value); foreach (var relationTypeIndex in EntityRelationIndex[entityId]) { @@ -450,7 +444,7 @@ public class World : IDisposable EntityRelationIndex[entityId].Clear(); } - private void MoveEntityToHigherArchetype(Id entityId, int row, Archetype from, Archetype to) + private void MoveEntityToHigherArchetype(EntityId entityId, int row, Archetype from, Archetype to) { for (int i = 0; i < from.Signature.Count; i += 1) { @@ -479,7 +473,7 @@ public class World : IDisposable to.RowToEntity.Add(entityId); } - private void MoveEntityToLowerArchetype(Id entityId, int row, Archetype from, Archetype to, Id removed) + private void MoveEntityToLowerArchetype(EntityId entityId, int row, Archetype from, Archetype to, TypeId removed) { for (int i = 0; i < from.Signature.Count; i += 1) {