Allowed the size of the levels to be changed and created the initial window that the world map will play in.

This commit is contained in:
James Daly
2019-07-17 22:42:44 -04:00
parent 0ff64797d9
commit 32542df86c
8 changed files with 281 additions and 33 deletions

View File

@@ -1,13 +1,19 @@
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 saveTo(file: File): Unit = {
val ostream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)))
@@ -17,12 +23,43 @@ class GameMap(val width: Int, val height: Int, val tileset: 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 {
@@ -36,9 +73,12 @@ object GameMap {
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
}
}

View File

@@ -7,7 +7,7 @@ import fmon.draw.tile._
class Tileset(val groundTiles: IndexedSeq[AutoTile], val doodadTiles: IndexedSeq[AutoTile], val info: IndexedSeq[TileInfo]) {
def groundInfo(index: Int) = info(index)
def doodadTiles(index: Int) = info(index - groundTiles.size)
def doodadInfo(index: Int) = info(index - groundTiles.size)
}
case class TileInfo(

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.TilePane?>
<ScrollPane fx:id="scroller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fmon.world.ui.GameView">
<content>
<StackPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308">
<children>
<TilePane fx:id="background" />
<TilePane fx:id="doodads" />
</children>
</StackPane>
</content>
</ScrollPane>

View File

@@ -0,0 +1,75 @@
package fmon.world.ui
import java.io._
import javafx.application.Application
import javafx.fxml.FXML
import javafx.fxml.FXMLLoader
import javafx.fxml.JavaFXBuilderFactory
import javafx.scene.{control => jfxsc, layout => jfxsl, Parent, Scene}
import javafx.stage.Stage
import scalafx.Includes._
import scalafx.beans.property._
import scalafx.collections.ObservableBuffer
import scalafx.scene.control._
import scalafx.scene.image.ImageView
import scalafx.scene.input.MouseEvent
import scalafx.scene.paint.Color
import scalafx.scene.layout._
import scalafx.stage.FileChooser
import scalafx.stage.Modality
import FileChooser.ExtensionFilter
import fmon._
import fmon.world._
import fmon.util.YamlHelper
class GameView {
@FXML var scroller: jfxsc.ScrollPane = _
@FXML var background: jfxsl.TilePane = _
@FXML var doodads: jfxsl.TilePane = _
def loadLevel(level: GameMap): Unit = {
background.prefColumns = level.width
val tiles = for (y <- 0 until level.height; x <- 0 until level.width) yield {
new ImageView(level.smoothed(x, y)).delegate
}
background.children ++= tiles
doodads.prefColumns = level.width
val dtiles = for (y <- 0 until level.height; x <- 0 until level.width) yield {
new ImageView(level.doodad(x, y).icon.croppedImage()).delegate
}
doodads.children ++= dtiles
}
}
object GameView {
class GameApp extends Application {
override def start(primaryStage: Stage): Unit = {
val frameLoader = new FXMLLoader(getClass.getResource("GameView.fxml"))
val root: Parent = frameLoader.load()
val controller = frameLoader.getController[GameView]()
val stageFile = raw"C:\Users\James\Documents\Design\Project\Progena\Data\outside.map"
val level = GameMap.loadFrom(new File(stageFile))
controller.loadLevel(level)
//val builderLoader = new FXMLLoader(getClass.getResource("ElementBuilder.fxml"))
//val builder: Parent = builderLoader.load()
//val builderController = builderLoader.getController[ElementBuilder]()
//controller.pane.children = builder
//controller.builder = builderController
val scene: Scene = new Scene(root)
primaryStage.setScene(scene)
primaryStage.show()
}
}
def main(args: Array[String]): Unit = {
Application.launch(classOf[GameApp], args: _*)
}
}