Anonymous avatar Anonymous committed 1c08e5c

scalafied inventory

Comments (0)

Files changed (6)

src/main/scala/com/mojang/ld22/Game.scala

 package com.mojang.ld22
 
-import java.awt.image.BufferStrategy
-import java.awt.image.BufferedImage
-import java.awt.image.DataBufferInt
-import java.io.IOException
-import javax.imageio.ImageIO
 import com.mojang.ld22.entity.Player
 import com.mojang.ld22.gfx.Color
 import com.mojang.ld22.gfx.Font
 import com.mojang.ld22.screen.LevelTransitionMenu
 import com.mojang.ld22.screen.TitleMenu
 import com.mojang.ld22.screen.WonMenu
+import java.awt.Canvas
+import java.awt.image.BufferedImage
+import java.awt.image.DataBufferInt
+import javax.imageio.ImageIO
 import screen.Menu
-import java.awt.Canvas
 
 class Game extends Canvas with Runnable {
 
   private var tickCount = 0
   var gameTime = 0
   private var level: Level = null
-  private var levels = new Array[Level](5)
+  private var levels: Seq[Level] = Seq.empty
   private var currentLevel = 3
   var player: Player = null
   private var playerDeadTime = 0
     wonTimer = 0
     gameTime = 0
     hasWon = false
-    levels = new Array[Level](5)
     currentLevel = 3
-    levels(4) = new Level(128, 128, 1, null)
-    levels(3) = new Level(128, 128, 0, levels(4))
-    levels(2) = new Level(128, 128, -1, levels(3))
-    levels(1) = new Level(128, 128, -2, levels(2))
-    levels(0) = new Level(128, 128, -3, levels(1))
-    level = levels(currentLevel)
+    // there's more functional ways to do this, but who cares?
+    val lvl = new Array[Level](5)
+    lvl(4) = new Level(128, 128, 1, null)
+    lvl(3) = new Level(128, 128, 0, lvl(4))
+    lvl(2) = new Level(128, 128, -1, lvl(3))
+    lvl(1) = new Level(128, 128, -2, lvl(2))
+    lvl(0) = new Level(128, 128, -3, lvl(1))
+    level = lvl(currentLevel)
+
     player = new Player(this, input)
     player.findStartPos(level)
     level.add(player)
 
-    for (i <- 0 to 4) levels(i).trySpawn(5000)
+    levels = lvl.toSeq
+    levels.foreach(_.trySpawn(5000))
   }
 
   private def init() {
       pp += 1
     }
 
-    try {
-      screen = new Screen(WIDTH, HEIGHT, new SpriteSheet(ImageIO.read(classOf[Game].getResourceAsStream("/icons.png"))))
-      lightScreen = new Screen(WIDTH, HEIGHT, new SpriteSheet(ImageIO.read(classOf[Game].getResourceAsStream("/icons.png"))))
-    }
-    catch {
-      case e: IOException => {
-        e.printStackTrace()
-      }
-    }
+    screen = new Screen(WIDTH, HEIGHT, new SpriteSheet(ImageIO.read(classOf[Game].getResourceAsStream("/icons.png"))))
+    lightScreen = new Screen(WIDTH, HEIGHT, new SpriteSheet(ImageIO.read(classOf[Game].getResourceAsStream("/icons.png"))))
+
     resetGame()
     setMenu(new TitleMenu)
   }

src/main/scala/com/mojang/ld22/crafting/Crafting.scala

 import com.mojang.ld22.entity.Anvil
 import com.mojang.ld22.entity.Chest
 import com.mojang.ld22.entity.Furnace
+import com.mojang.ld22.entity.Lantern
 import com.mojang.ld22.entity.Oven
-import com.mojang.ld22.entity.Lantern
 import com.mojang.ld22.entity.Workbench
 import com.mojang.ld22.item.ToolType
 import com.mojang.ld22.item.resource.Resource
-import java.util
 
 object Crafting {
+
   import Resource._
 
-  final val workbenchRecipes: util.List[Recipe] = new util.ArrayList[Recipe] {
-    add(new FurnitureRecipe(classOf[Lantern]).addCost(wood, 5).addCost(slime, 10).addCost(glass, 4))
-    add(new FurnitureRecipe(classOf[Oven]).addCost(stone, 15))
-    add(new FurnitureRecipe(classOf[Furnace]).addCost(stone, 20))
-    add(new FurnitureRecipe(classOf[Workbench]).addCost(wood, 20))
-    add(new FurnitureRecipe(classOf[Chest]).addCost(wood, 20))
-    add(new FurnitureRecipe(classOf[Anvil]).addCost(ironIngot, 5))
-    add(new ToolRecipe(ToolType.sword, 0).addCost(wood, 5))
-    add(new ToolRecipe(ToolType.axe, 0).addCost(wood, 5))
-    add(new ToolRecipe(ToolType.hoe, 0).addCost(wood, 5))
-    add(new ToolRecipe(ToolType.pickaxe, 0).addCost(wood, 5))
-    add(new ToolRecipe(ToolType.shovel, 0).addCost(wood, 5))
-    add(new ToolRecipe(ToolType.sword, 1).addCost(wood, 5).addCost(stone, 5))
-    add(new ToolRecipe(ToolType.axe, 1).addCost(wood, 5).addCost(stone, 5))
-    add(new ToolRecipe(ToolType.hoe, 1).addCost(wood, 5).addCost(stone, 5))
-    add(new ToolRecipe(ToolType.pickaxe, 1).addCost(wood, 5).addCost(stone, 5))
-    add(new ToolRecipe(ToolType.shovel, 1).addCost(wood, 5).addCost(stone, 5))
-  }
+  final val workbenchRecipes = Seq(
+    new FurnitureRecipe(classOf[Lantern]).addCost(wood, 5).addCost(slime, 10).addCost(glass, 4),
+    new FurnitureRecipe(classOf[Oven]).addCost(stone, 15),
+    new FurnitureRecipe(classOf[Furnace]).addCost(stone, 20),
+    new FurnitureRecipe(classOf[Workbench]).addCost(wood, 20),
+    new FurnitureRecipe(classOf[Chest]).addCost(wood, 20),
+    new FurnitureRecipe(classOf[Anvil]).addCost(ironIngot, 5),
+    new ToolRecipe(ToolType.sword, 0).addCost(wood, 5),
+    new ToolRecipe(ToolType.axe, 0).addCost(wood, 5),
+    new ToolRecipe(ToolType.hoe, 0).addCost(wood, 5),
+    new ToolRecipe(ToolType.pickaxe, 0).addCost(wood, 5),
+    new ToolRecipe(ToolType.shovel, 0).addCost(wood, 5),
+    new ToolRecipe(ToolType.sword, 1).addCost(wood, 5).addCost(stone, 5),
+    new ToolRecipe(ToolType.axe, 1).addCost(wood, 5).addCost(stone, 5),
+    new ToolRecipe(ToolType.hoe, 1).addCost(wood, 5).addCost(stone, 5),
+    new ToolRecipe(ToolType.pickaxe, 1).addCost(wood, 5).addCost(stone, 5),
+    new ToolRecipe(ToolType.shovel, 1).addCost(wood, 5).addCost(stone, 5)
+  )
 
-  final val anvilRecipes: util.List[Recipe] = new util.ArrayList[Recipe] {
-    add(new ToolRecipe(ToolType.sword, 2).addCost(wood, 5).addCost(ironIngot, 5))
-    add(new ToolRecipe(ToolType.axe, 2).addCost(wood, 5).addCost(ironIngot, 5))
-    add(new ToolRecipe(ToolType.hoe, 2).addCost(wood, 5).addCost(ironIngot, 5))
-    add(new ToolRecipe(ToolType.pickaxe, 2).addCost(wood, 5).addCost(ironIngot, 5))
-    add(new ToolRecipe(ToolType.shovel, 2).addCost(wood, 5).addCost(ironIngot, 5))
-    add(new ToolRecipe(ToolType.sword, 3).addCost(wood, 5).addCost(goldIngot, 5))
-    add(new ToolRecipe(ToolType.axe, 3).addCost(wood, 5).addCost(goldIngot, 5))
-    add(new ToolRecipe(ToolType.hoe, 3).addCost(wood, 5).addCost(goldIngot, 5))
-    add(new ToolRecipe(ToolType.pickaxe, 3).addCost(wood, 5).addCost(goldIngot, 5))
-    add(new ToolRecipe(ToolType.shovel, 3).addCost(wood, 5).addCost(goldIngot, 5))
-    add(new ToolRecipe(ToolType.sword, 4).addCost(wood, 5).addCost(gem, 50))
-    add(new ToolRecipe(ToolType.axe, 4).addCost(wood, 5).addCost(gem, 50))
-    add(new ToolRecipe(ToolType.hoe, 4).addCost(wood, 5).addCost(gem, 50))
-    add(new ToolRecipe(ToolType.pickaxe, 4).addCost(wood, 5).addCost(gem, 50))
-    add(new ToolRecipe(ToolType.shovel, 4).addCost(wood, 5).addCost(gem, 50))
-  }
+  final val anvilRecipes = Seq(
+    new ToolRecipe(ToolType.sword, 2).addCost(wood, 5).addCost(ironIngot, 5),
+    new ToolRecipe(ToolType.axe, 2).addCost(wood, 5).addCost(ironIngot, 5),
+    new ToolRecipe(ToolType.hoe, 2).addCost(wood, 5).addCost(ironIngot, 5),
+    new ToolRecipe(ToolType.pickaxe, 2).addCost(wood, 5).addCost(ironIngot, 5),
+    new ToolRecipe(ToolType.shovel, 2).addCost(wood, 5).addCost(ironIngot, 5),
+    new ToolRecipe(ToolType.sword, 3).addCost(wood, 5).addCost(goldIngot, 5),
+    new ToolRecipe(ToolType.axe, 3).addCost(wood, 5).addCost(goldIngot, 5),
+    new ToolRecipe(ToolType.hoe, 3).addCost(wood, 5).addCost(goldIngot, 5),
+    new ToolRecipe(ToolType.pickaxe, 3).addCost(wood, 5).addCost(goldIngot, 5),
+    new ToolRecipe(ToolType.shovel, 3).addCost(wood, 5).addCost(goldIngot, 5),
+    new ToolRecipe(ToolType.sword, 4).addCost(wood, 5).addCost(gem, 50),
+    new ToolRecipe(ToolType.axe, 4).addCost(wood, 5).addCost(gem, 50),
+    new ToolRecipe(ToolType.hoe, 4).addCost(wood, 5).addCost(gem, 50),
+    new ToolRecipe(ToolType.pickaxe, 4).addCost(wood, 5).addCost(gem, 50),
+    new ToolRecipe(ToolType.shovel, 4).addCost(wood, 5).addCost(gem, 50)
+  )
 
-  final val furnaceRecipes: util.List[Recipe] = new util.ArrayList[Recipe] {
-    add(new ResourceRecipe(ironIngot).addCost(ironOre, 4).addCost(coal, 1))
-    add(new ResourceRecipe(goldIngot).addCost(goldOre, 4).addCost(coal, 1))
-    add(new ResourceRecipe(glass).addCost(sand, 4).addCost(coal, 1))
-  }
+  final val furnaceRecipes = Seq(
+    new ResourceRecipe(ironIngot).addCost(ironOre, 4).addCost(coal, 1),
+    new ResourceRecipe(goldIngot).addCost(goldOre, 4).addCost(coal, 1),
+    new ResourceRecipe(glass).addCost(sand, 4).addCost(coal, 1)
+  )
 
-  final val ovenRecipes: util.List[Recipe] = new util.ArrayList[Recipe] {
-    add(new ResourceRecipe(bread).addCost(wheat, 4))
-  }
+  final val ovenRecipes = Seq(
+    new ResourceRecipe(bread).addCost(wheat, 4)
+  )
 }

src/main/scala/com/mojang/ld22/entity/Inventory.scala

 import com.mojang.ld22.item.Item
 import com.mojang.ld22.item.ResourceItem
 import com.mojang.ld22.item.resource.Resource
+import collection.mutable
 
 class Inventory {
-  var items: List[Item] = new ArrayList[Item]
+  var items: mutable.Buffer[Item] = mutable.Buffer.empty  // XXX not ideal
 
   def add(item: Item) {
     add(items.size, item)
 
   def add(slot: Int, item: Item) {
     if (item.isInstanceOf[ResourceItem]) {
-      val toTake: ResourceItem = item.asInstanceOf[ResourceItem]
-      val has: ResourceItem = findResource(toTake.resource)
+      val toTake = item.asInstanceOf[ResourceItem]
+      val has = findResource(toTake.resource)
       if (has == null) {
-        items.add(slot, toTake)
+        items.insert(slot, toTake)
       }
       else {
         has.count += toTake.count
       }
     }
     else {
-      items.add(slot, item)
+      items.insert(slot, item)
     }
   }
 
   private def findResource(resource: Resource): ResourceItem = {
     for (i <- 0 until items.size) {
-      if (items.get(i).isInstanceOf[ResourceItem]) {
-        val has: ResourceItem = items.get(i).asInstanceOf[ResourceItem]
+      if (items(i).isInstanceOf[ResourceItem]) {
+        val has: ResourceItem = items(i).asInstanceOf[ResourceItem]
         if (has.resource eq resource) return has
       }
     }
     if (ri == null) return false
     if (ri.count < count) return false
     ri.count -= count
-    if (ri.count <= 0) items.remove(ri)
+//    if (ri.count <= 0) items.remove(ri)
+    if (ri.count <= 0) items -= ri
+
     true
   }
 
     else {
       var count: Int = 0
       for (i <- 0 until items.size) {
-        if (items.get(i).matches(item))
+        if (items(i).matches(item))
           count += 1
       }
       return count

src/main/scala/com/mojang/ld22/screen/CraftingMenu.scala

 import scala.collection.mutable
 import java.util
 
-class CraftingMenu(recips: util.List[Recipe], player: Player) extends Menu {
+class CraftingMenu(recips: Seq[Recipe], player: Player) extends Menu {
   private var selected = 0
 
   recips.foreach(_.checkCanCraft(player))
-  private val recipes: mutable.Seq[Recipe] = recips.sortWith((a, b) => a.canCraft)
+  private val recipes = recips.sortWith((a, b) => a.canCraft)
 
   override def tick() {
     if (input.menu.clicked) game.setMenu(None)

src/main/scala/com/mojang/ld22/screen/InventoryMenu.scala

   private var selected: Int = 0
 
   if (player.activeItem != null) {
-    player.inventory.items.add(0, player.activeItem)
+    player.inventory.items.insert(0, player.activeItem)
     player.activeItem = (null)
   }
 

src/main/scala/com/mojang/ld22/screen/Menu.scala

     this.game = game
   }
 
-  def renderItemList(screen: Screen, xo: Int, yo: Int, x1: Int, y1: Int, listItems: java.util.List[_ <: ListItem], sel: Int) {
+  def renderItemList(screen: Screen, xo: Int, yo: Int, x1: Int, y1: Int, listItems: Seq[_ <: ListItem], sel: Int) {
     var selected = sel
     var renderCursor: Boolean = true
     if (selected < 0) {
     if (io > listItems.size - h) io = listItems.size - h
     if (io < 0) io = 0
     for (i <- i0 until i1)
-      listItems.get(i + io).renderInventory(screen, (1 + xo) * 8, (i + 1 + yo) * 8)
+      listItems(i + io).renderInventory(screen, (1 + xo) * 8, (i + 1 + yo) * 8)
 
     if (renderCursor) {
       val yy: Int = selected + 1 - io + yo
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.