package fmon.world import scalafx.scene.image.Image import java.io._ import fmon.draw._ import fmon.draw.tile._ import fmon.util._ import Direction._ 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 var doodads: IndexedSeq[AutoTile] = for (y <- 0 until height; x <- 0 until width) yield tileset.doodadTiles.head def tileInfo(index: Int) = tileset.infoOf(tiles(index)) 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(" ")) val doodadIndices = tileset.doodadTiles.zipWithIndex.toMap doodads.foreach(t => ostream.writeInt(doodadIndices(t))) ostream.close() } def apply(x: Int, y: Int): AutoTile = tiles(pt2index(x, y)) def doodad(x: Int, y: Int): AutoTile = doodads(pt2index(x, y)) def smoothed(x: Int, y: Int): Image = { val i = pt2index(x, y) val dirs = scala.collection.mutable.Set[Direction.Value]() val onLeft = x == 0 val onRight = x + 1 == width val onTop = y == 0 val onBottom = y + 1 == height if (onLeft || tiles(left(i)) == tiles(i)) dirs += West if (onRight || tiles(right(i)) == tiles(i)) dirs += East if (onTop || tiles(up(i)) == tiles(i)) dirs += North if (onBottom || tiles(down(i)) == tiles(i)) dirs += South if (onLeft || onTop || tiles(left(up(i))) == tiles(i)) dirs += Northwest if (onLeft || onBottom || tiles(down(left(i))) == tiles(i)) dirs += Southwest if (onTop || onRight || tiles(right(up(i))) == tiles(i)) dirs += Northeast if (onBottom || onRight || tiles(right(down(i))) == tiles(i)) dirs += Southeast val index = pt2index(x, y) tiles(index).build(dirs.toSet).croppedImage() } def pt2index(x: Int, y: Int) = y * width + x def index2x(i: Int) = i % width def index2y(i: Int) = i / width def left(i: Int) = i - 1 def right(i: Int) = i + 1 def up(i: Int) = i - width def down(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 dir = new File(raw"C:\Users\James\Documents\Design\Project\Progena\Data") val resourceDir = new File(raw"C:\Users\James\Documents\Design\Project\Progena\Resources\tilesets") val tokens = YamlHelper.extractSeq[TilesetToken](new FileInputStream(new File(dir, "Tilesets.yaml"))).map(t => (t.name, t)).toMap val tileset = tokens("Outside").load(resourceDir) val indices = for (y <- 0 until height; x <- 0 until width) yield istream.readInt() val dindices = 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)) val doodads = dindices.map(i => tileset.doodadTiles(i)) map.tiles = tiles map.doodads = doodads map } }