diff --git a/encompass-cs/Collections/TypedComponentStore.cs b/encompass-cs/Collections/TypedComponentStore.cs index a32fea4..fa766b5 100644 --- a/encompass-cs/Collections/TypedComponentStore.cs +++ b/encompass-cs/Collections/TypedComponentStore.cs @@ -50,10 +50,13 @@ namespace Encompass private unsafe void InternalSet(int entityID, TComponent component) { - var index = _idManager.NextID(); + if (!indices.ContainsKey(entityID)) + { + indices[entityID] = _idManager.NextID(); + } + var ptr = Unsafe.AsPointer(ref component); - indices[entityID] = index; - components[index] = Unsafe.AsRef(ptr); + components[indices[entityID]] = Unsafe.AsRef(ptr); } public override bool Remove(int entityID, int priority) @@ -84,6 +87,10 @@ namespace Encompass public override void Clear() { + foreach (var entityID in indices.Keys) + { + _idManager.Free(entityID); + } indices.Clear(); priorities.Clear(); }