Now capable of saving and loading stages

This commit is contained in:
dalyjame
2019-07-06 22:12:47 -04:00
parent 8e7937b3c6
commit 07df031670
8 changed files with 220 additions and 46 deletions

View File

@@ -9,7 +9,8 @@ case class Config(
val resistMult: Double,
val weakMult: Double,
val immuneMult: Double,
val maxBoost: Int
val maxBoost: Int,
val tileSize: Int
) {
}
@@ -24,4 +25,5 @@ object Config {
def weakMult = config.weakMult
def immuneMult = config.immuneMult
def maxBoost = config.maxBoost
def tileSize = config.tileSize
}

View File

@@ -6,3 +6,4 @@ resistMult: 0.5
immuneMult: 0.0
maxBoost: 6
newday: 02:00
tileSize: 32

View File

@@ -1,6 +1,6 @@
package fmon.draw
import java.io.InputStream
import java.io._
import scalafx.geometry.Rectangle2D
import scalafx.scene.image._
@@ -23,6 +23,7 @@ case class StillImg(val image: Image, val x: Double, val y: Double, val width: D
class Palette(val image: Image, val numAcross: Int, val numDown: Int) {
def this(url: String, numAcross: Int, numDown: Int) = this(new Image(url), numAcross, numDown)
def this(stream: InputStream, numAcross: Int, numDown: Int) = this(new Image(stream), numAcross, numDown)
def this(file: File, numAcross: Int, numDown: Int) = this(new FileInputStream(file), numAcross, numDown)
val imgWidth = image.width() / numAcross
val imgHeight = image.height() / numDown
@@ -57,10 +58,14 @@ object Palette {
new Palette(img, numAcross, numDown.toInt)
}
def bySize(stream: InputStream, width: Double, height: Double) = {
def bySize(stream: InputStream, width: Double, height: Double): Palette = {
val img = new Image(stream)
val numAcross = img.width() / width
val numDown = img.height() / height
new Palette(img, numAcross.toInt, numDown.toInt)
}
def bySize(file: File, width: Double, height: Double): Palette = {
bySize(new FileInputStream(file), width, height)
}
}

View File

@@ -12,7 +12,12 @@ import fmon.util._
import Direction._
class AutoFloorTile(val palette: Palette, val size: Int = 48) {
trait AutoTile {
def icon: StillImg
def build(dirs: Set[Direction.Value]): StillImg
}
class AutoFloorTile(val palette: Palette, val size: Int = 48) extends AutoTile {
def icon: StillImg = palette.apply(0, 0, 2, 2)
@@ -79,7 +84,7 @@ class AutoFloorTile(val palette: Palette, val size: Int = 48) {
}
}
class AutoWallTile(val palette: Palette, val size: Int = 48) {
class AutoWallTile(val palette: Palette, val size: Int = 48) extends AutoTile {
def icon: StillImg = {
val canvas = new scalafx.scene.canvas.Canvas(size, size)
val g = canvas.graphicsContext2D

View File

@@ -0,0 +1,41 @@
package fmon.world
import java.io._
import fmon.draw.tile._
import fmon.util._
class GameMap(val width: Int, val height: Int, val tileset: Tileset) {
var tiles: IndexedSeq[AutoTile] = for (y <- 0 until height; x <- 0 until width) yield tileset.groundTiles.head
def saveTo(file: File): Unit = {
val ostream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)))
ostream.writeInt(width)
ostream.writeInt(height)
ostream.writeObject(tileset.token.a2)// Tileset
val tileIndices = tileset.groundTiles.zipWithIndex.toMap
tiles.foreach(t => ostream.writeInt(tileIndices(t)))
//ostream.writeObject(tiles.map(tileIndices(_)).mkString(" "))
ostream.close()
}
def pt2index(x: Int, y: Int) = y * width + x
def index2x(i: Int) = i % width
def index2y(i: Int) = i / width
}
object GameMap {
def loadFrom(file: File): GameMap = {
//val istream = new BufferedReader(new FileReader(file))
val istream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))
val width = istream.readInt()
val height = istream.readInt()
val tileset = new Tileset(new TilesetToken(istream.readObject().toString))
val indices = for (y <- 0 until height; x <- 0 until width) yield istream.readInt()
val map = new GameMap(width, height, tileset)
val tiles = indices.map(i => tileset.groundTiles(i))
map.tiles = tiles
map
}
}

View File

@@ -0,0 +1,18 @@
package fmon.world
import java.io._
import fmon.Config
import fmon.draw.tile._
class Tileset(val token: TilesetToken) {
val groundTiles: IndexedSeq[AutoTile] = {
val file = new File(token.a2)
val palette = new AutoTilePalette(file, Config.tileSize)
for (y <- 0 until 4; x <- 0 until 8) yield {
palette(x, y)
}
}
}
case class TilesetToken(val a2: String)