Anonymous avatar Anonymous committed b42a249

rewrote Level.tick() to remove index use and avoid CME

Comments (0)

Files changed (1)

src/main/scala/com/mojang/ld22/level/Level.scala

   private var tiles: Array[Byte] = null
   private var data: Array[Byte] = null
   private var entitiesInTiles: util.List[util.List[Entity]] = null
-  private val entities: util.List[Entity] = new util.ArrayList[Entity]
+  private var entities: util.List[Entity] = new util.ArrayList[Entity]
   private val spriteSorter: Comparator[Entity] = new Comparator[Entity] {
     def compare(e0: Entity, e1: Entity): Int = {
       if (e1.y < e0.y) return +1
       val yt = random.nextInt(w)
       getTile(xt, yt).tick(this, xt, yt)
     }
-    val ents: util.List[Entity] = entities
-    for (i <- (ents.size - 1) to 0 by -1) {
-      val e: Entity = ents.get(i)
+
+    val oldents = entities
+    val newents = new util.ArrayList[Entity]()
+    entities = newents
+
+    val it = oldents.iterator
+    while (it.hasNext) {
+      val e = it.next
       val xto = e.x >> 4
       val yto = e.y >> 4
       e.tick()
       if (e.removed) {
-        ents.remove(i)
+        it.remove()
         removeEntity(xto, yto, e)
       }
       else {
         }
       }
     }
+    // more efficient since oldents has slack at the end by now
+    oldents.addAll(entities)
+    entities = oldents
   }
 
   def entities(x0: Int, y0: Int, x1: Int, y1: Int): util.List[Entity] = {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.