From 88457efd1dda3fedee21f015bcb99113d6e17c67 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Fri, 10 May 2024 15:34:27 +0200 Subject: [PATCH] Only swap when not last row --- lib/init.lua | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/init.lua b/lib/init.lua index 10c0448..d517414 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -82,23 +82,27 @@ local function transitionArchetype( column[last] = nil end - local dense, sparse = entityIndex.dense, entityIndex.sparse - -- Move the entity from the source to the destination archetype. - local atSourceRow = sourceEntities[sourceRow] - destinationEntities[destinationRow] = atSourceRow - local record = sparse[atSourceRow] - record.row = destinationRow + local sparse = entityIndex.sparse + local movedAway = #sourceEntities + -- Move the entity from the source to the destination archetype. -- Because we have swapped columns we now have to update the records -- corresponding to the entities' rows that were swapped. - local movedAway = #sourceEntities - if sourceRow ~= movedAway then - local atMovedAway = sourceEntities[movedAway] - sourceEntities[sourceRow] = atMovedAway - sparse[atMovedAway].row = sourceRow + local e1 = sourceEntities[sourceRow] + local e2 = sourceEntities[movedAway] + + if sourceRow ~= movedAway then + sourceEntities[sourceRow] = e2 end sourceEntities[movedAway] = nil + destinationEntities[destinationRow] = e1 + + local record1 = sparse[e1] + local record2 = sparse[e2] + + record1.row = destinationRow + record2.row = sourceRow end local function archetypeAppend(entity: number, archetype: Archetype): number