Commits

Michael Ludwig committed 50a1ada

Fix compact() bug when removed entities are at the end of the array.

Comments (0)

Files changed (1)

src/main/java/com/lhkbob/entreri/EntitySystem.java

                 }
             } else {
                 // found an entity to preserve
-                if (startRemove > 0) {
+                if (startRemove >= 0) {
                     // we have a gap from [startRemove, i - 1] that can be compacted
                     System.arraycopy(entities, i, entities, startRemove, entityInsert - i);
 
             }
         }
 
+        if (startRemove >= 0) {
+            // the last gap of entities to remove is at the end of the array,
+            // so all we have to do is update the size
+            entityInsert = startRemove;
+        }
+
         // Build a map from oldIndex to newIndex and repair entity's index
         int[] oldToNew = new int[entities.length];
         for (int i = 1; i < entityInsert; i++) {