Anonymous avatar Anonymous committed 62536a0

misc

Comments (0)

Files changed (1)

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

 package com.mojang.ld22
 package level
 
-
-import java.util.Collections
-import java.util.Comparator
 import com.mojang.ld22.entity.AirWizard
 import com.mojang.ld22.entity.Entity
 import com.mojang.ld22.entity.Mob
 import com.mojang.ld22.level.levelgen.LevelGen
 import com.mojang.ld22.level.tile.Tile
 import java.util
+import java.util.Collections
+import java.util.Comparator
 
-class Level(var w: Int, var h: Int, var level: Int, var parentLevel: Level) {
+class Level(val w: Int, val h: Int, val level: Int, var parentLevel: Level) {
 
-  var tiles: Array[Byte] = null
-  var data: Array[Byte] = null
-  var entitiesInTiles: util.List[util.List[Entity]] = null
-  var grassColor: Int = 141
-  var dirtColor: Int = 322
-  var sandColor: Int = 550
-  var depth: Int = 0
+  import com.mojang.ld22.util.Implicits._
+
+  val grassColor = 141
+  val dirtColor = if (level < 0) 222 else if (level == 1) 444 else 322
+  val sandColor = 550
   var monsterDensity: Int = 8
-  var entities: util.List[Entity] = new util.ArrayList[Entity]
+
+  private val depth: Int = level
+  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 val spriteSorter: Comparator[Entity] = new Comparator[Entity] {
     def compare(e0: Entity, e1: Entity): Int = {
       if (e1.y < e0.y) return +1
       0
     }
   }
-  var rowSprites: util.List[Entity] = new util.ArrayList[Entity]
+  private val rowSprites: util.List[Entity] = new util.ArrayList[Entity]
+
   var player: Player = null
+  var maps: Array[Array[Byte]] = null
 
-
-  if (level < 0) {
-    dirtColor = 222
-  }
-  this.depth = level
-
-  var maps: Array[Array[Byte]] = null
-  if (level == 1) {
-    dirtColor = 444
-  }
   if (level == 0) maps = LevelGen.createAndValidateTopMap(w, h)
   else if (level < 0) {
     maps = LevelGen.createAndValidateUndergroundMap(w, h, -level)
-    monsterDensity = (4)
+    monsterDensity = 4
   }
   else {
     maps = LevelGen.createAndValidateSkyMap(w, h)
-    monsterDensity = (4)
+    monsterDensity = 4
   }
   tiles = (maps(0))
   data = (maps(1))
       }
     }
   }
-  entitiesInTiles = (new util.ArrayList[util.List[Entity]](w * h))
+  entitiesInTiles = new util.ArrayList[util.List[Entity]](w * h)
+  for (i <- 0 until w * h) {
+    entitiesInTiles.add(new util.ArrayList[Entity])
+  }
 
-  {
-    for (i <- 0 until w * h) {
-      entitiesInTiles.add(new util.ArrayList[Entity])
-    }
-  }
   if (level == 1) {
     val aw: AirWizard = new AirWizard
     aw.x = w * 8
   }
 
   def renderSprites(screen: Screen, xScroll: Int, yScroll: Int) {
-    val xo: Int = xScroll >> 4
-    val yo: Int = yScroll >> 4
-    val w: Int = (screen.w + 15) >> 4
-    val h: Int = (screen.h + 15) >> 4
+    val xo = xScroll >> 4
+    val yo = yScroll >> 4
+    val w = (screen.w + 15) >> 4
+    val h = (screen.h + 15) >> 4
     screen.setOffset(xScroll, yScroll)
 
-    for (y <- yo to h + yo) {
-      for (x <- xo to w + xo) {
-        if (!(x < 0 || y < 0 || x >= this.w || y >= this.h)) rowSprites.addAll(entitiesInTiles.get(x + y * this.w))
-      }
-      if (rowSprites.size > 0) {
+    for (y <- (yo max 0) to ((h + yo) min (this.h - 1));
+         x <- (xo max 0) to ((w + xo) min (this.w - 1))) {
+      rowSprites.addAll(entitiesInTiles.get(x + y * this.w))
+
+      if (rowSprites.size > 0)
         sortAndRender(screen, rowSprites)
-      }
+
       rowSprites.clear()
     }
+
+
     screen.setOffset(0, 0)
   }
 
   def renderLight(screen: Screen, xScroll: Int, yScroll: Int) {
-    val xo: Int = xScroll >> 4
-    val yo: Int = yScroll >> 4
-    val w: Int = (screen.w + 15) >> 4
-    val h: Int = (screen.h + 15) >> 4
+    val xo = xScroll >> 4
+    val yo = yScroll >> 4
+    val w = (screen.w + 15) >> 4
+    val h = (screen.h + 15) >> 4
     screen.setOffset(xScroll, yScroll)
     val r: Int = 4
 
         val entities: util.List[Entity] = entitiesInTiles.get(x + y * this.w)
         import scala.collection.JavaConversions._
         for (e <- entities) {
-          val lr: Int = e.lightRadius
+          val lr = e.lightRadius
           if (lr > 0) screen.renderLight(e.x - 1, e.y - 4, lr * 8)
         }
         val lr: Int = getTile(x, y).getLightRadius(this, x, y)
     data(x + y * w) & 0xff
   }
 
-  def setData(x: Int, y: Int, `val`: Int) {
+  def setData(x: Int, y: Int, v: Int) {
     if (x < 0 || y < 0 || x >= w || y >= h) return
-    data(x + y * w) = `val`.asInstanceOf[Byte]
+    data(x + y * w) = v.asInstanceOf[Byte]
   }
 
   def add(entity: Entity) {
 
   def remove(e: Entity) {
     entities.remove(e)
-    val xto: Int = e.x >> 4
-    val yto: Int = e.y >> 4
+    val xto = e.x >> 4
+    val yto = e.y >> 4
     removeEntity(xto, yto, e)
   }
 
   def tick() {
     trySpawn(1)
 
-    for (i <- 0 until w * h / 50) {
-      val xt: Int = random.nextInt(w)
-      val yt: Int = random.nextInt(w)
+    (w * h / 50) times {
+      val xt = random.nextInt(w)
+      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 xto: Int = e.x >> 4
-      val yto: Int = e.y >> 4
+      val xto = e.x >> 4
+      val yto = e.y >> 4
       e.tick()
       if (e.removed) {
         ents.remove(i)
         removeEntity(xto, yto, e)
       }
       else {
-        val xt: Int = e.x >> 4
-        val yt: Int = e.y >> 4
+        val xt = e.x >> 4
+        val yt = e.y >> 4
         if (xto != xt || yto != yt) {
           removeEntity(xto, yto, e)
           insertEntity(xt, yt, e)
 
   def entities(x0: Int, y0: Int, x1: Int, y1: Int): util.List[Entity] = {
     val result: util.List[Entity] = new util.ArrayList[Entity]
-    val xt0: Int = (x0 >> 4) - 1
-    val yt0: Int = (y0 >> 4) - 1
-    val xt1: Int = (x1 >> 4) + 1
-    val yt1: Int = (y1 >> 4) + 1
+    val xt0 = ((x0 >> 4) - 1) max 0
+    val yt0 = ((y0 >> 4) - 1) max 0
+    val xt1 = ((x1 >> 4) + 1) min w - 1
+    val yt1 = ((y1 >> 4) + 1) min h - 1
 
     for (y <- yt0 to yt1; x <- xt0 to xt1) {
-      if (!(x < 0 || y < 0 || x >= w || y >= h)) {
-        val entities: util.List[Entity] = entitiesInTiles.get(x + y * this.w)
-        import scala.collection.JavaConversions._
-        for (e <- entities) {
-          if (e.intersects(x0, y0, x1, y1)) result.add(e)
-        }
+      val ents = entitiesInTiles.get(x + y * this.w)
+      import scala.collection.JavaConversions._
+      for (e <- ents) {
+        if (e.intersects(x0, y0, x1, y1)) result.add(e)
       }
     }
     result
   }
-}
+}
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.