[Editor] Refactor map model
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
package com.bartlomiejpluta.base.editor.controller.map
|
package com.bartlomiejpluta.base.editor.controller.map
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.controller.tileset.TileSetController
|
import com.bartlomiejpluta.base.editor.controller.tileset.TileSetController
|
||||||
import com.bartlomiejpluta.base.editor.model.map.GameMap
|
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
||||||
import com.bartlomiejpluta.base.editor.model.map.tileset.TileSet
|
|
||||||
import tornadofx.Controller
|
import tornadofx.Controller
|
||||||
|
|
||||||
class MapController : Controller() {
|
class MapController : Controller() {
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
package com.bartlomiejpluta.base.editor.model.map.layer
|
package com.bartlomiejpluta.base.editor.model.map.layer
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.view.render.Renderable
|
interface Layer
|
||||||
|
|
||||||
interface Layer : Renderable
|
|
||||||
@@ -3,17 +3,7 @@ package com.bartlomiejpluta.base.editor.model.map.layer
|
|||||||
import com.bartlomiejpluta.base.editor.model.map.tileset.Tile
|
import com.bartlomiejpluta.base.editor.model.map.tileset.Tile
|
||||||
import javafx.scene.canvas.GraphicsContext
|
import javafx.scene.canvas.GraphicsContext
|
||||||
|
|
||||||
class TileLayer(private val layer: Array<Array<Tile?>>) : Layer {
|
class TileLayer(val layer: Array<Array<Tile?>>) : Layer {
|
||||||
|
|
||||||
fun setTile(row: Int, column: Int, tile: Tile?) = apply { layer[row][column] = tile }
|
fun setTile(row: Int, column: Int, tile: Tile?) = apply { layer[row][column] = tile }
|
||||||
|
|
||||||
override fun render(gc: GraphicsContext) {
|
|
||||||
for ((row, columns) in layer.withIndex()) {
|
|
||||||
for ((column, tile) in columns.withIndex()) {
|
|
||||||
if (tile != null) {
|
|
||||||
gc.drawImage(tile.image, column * tile.image.width, row * tile.image.height)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,39 +1,12 @@
|
|||||||
package com.bartlomiejpluta.base.editor.model.map
|
package com.bartlomiejpluta.base.editor.model.map.map
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.model.map.layer.Layer
|
import com.bartlomiejpluta.base.editor.model.map.layer.Layer
|
||||||
import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer
|
import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer
|
||||||
import com.bartlomiejpluta.base.editor.model.map.tileset.Tile
|
|
||||||
import com.bartlomiejpluta.base.editor.model.map.tileset.TileSet
|
import com.bartlomiejpluta.base.editor.model.map.tileset.TileSet
|
||||||
import com.bartlomiejpluta.base.editor.view.render.Renderable
|
|
||||||
import javafx.beans.property.SimpleIntegerProperty
|
|
||||||
import javafx.scene.canvas.GraphicsContext
|
|
||||||
|
|
||||||
class Grid(private val tileSet: TileSet, private val rows: Int, private val columns: Int) : Renderable {
|
|
||||||
private var tileWidth = tileSet.tileWidth.toDouble()
|
|
||||||
private var tileHeight = tileSet.tileHeight.toDouble()
|
|
||||||
private var mapWidth = columns * tileWidth
|
|
||||||
private var mapHeight = rows * tileHeight
|
|
||||||
|
|
||||||
override fun render(gc: GraphicsContext) {
|
class GameMap(val tileSet: TileSet, val rows: Int, val columns: Int) {
|
||||||
gc.lineWidth = LINE_WIDTH
|
|
||||||
|
|
||||||
for (row in 0 until rows) {
|
|
||||||
gc.strokeLine(0.0, row * tileHeight, mapWidth, row * tileHeight)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (column in 0 until columns) {
|
|
||||||
gc.strokeLine(column * tileWidth, 0.0, column * tileWidth, mapHeight)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val LINE_WIDTH = 1.5
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class GameMap(private val tileSet: TileSet, private val rows: Int, private val columns: Int) : Renderable {
|
|
||||||
val layers = mutableListOf<Layer>()
|
val layers = mutableListOf<Layer>()
|
||||||
private val grid = Grid(tileSet, rows, columns)
|
|
||||||
|
|
||||||
val width = columns * tileSet.tileWidth
|
val width = columns * tileSet.tileWidth
|
||||||
|
|
||||||
@@ -66,9 +39,4 @@ class GameMap(private val tileSet: TileSet, private val rows: Int, private val c
|
|||||||
fun setTile(layer: Int, row: Int, column: Int, tileRow: Int, tileColumn: Int) = apply {
|
fun setTile(layer: Int, row: Int, column: Int, tileRow: Int, tileColumn: Int) = apply {
|
||||||
(layers[layer] as TileLayer).setTile(row, column, tileSet.getTile(tileRow, tileColumn))
|
(layers[layer] as TileLayer).setTile(row, column, tileSet.getTile(tileRow, tileColumn))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun render(gc: GraphicsContext) {
|
|
||||||
layers.forEach { it.render(gc) }
|
|
||||||
grid.render(gc)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.render.canvas.layer
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer
|
||||||
|
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||||
|
import javafx.scene.canvas.GraphicsContext
|
||||||
|
|
||||||
|
class TileLayerCanvas(private val tileLayer: TileLayer) : Renderable {
|
||||||
|
|
||||||
|
override fun render(gc: GraphicsContext) {
|
||||||
|
for ((row, columns) in tileLayer.layer.withIndex()) {
|
||||||
|
for ((column, tile) in columns.withIndex()) {
|
||||||
|
if (tile != null) {
|
||||||
|
gc.drawImage(tile.image, column * tile.image.width, row * tile.image.height)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package com.bartlomiejpluta.base.editor.render.canvas.map
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
||||||
|
import com.bartlomiejpluta.base.editor.model.map.layer.Layer
|
||||||
|
import com.bartlomiejpluta.base.editor.model.map.layer.TileLayer
|
||||||
|
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||||
|
import javafx.scene.canvas.GraphicsContext
|
||||||
|
|
||||||
|
class MapCanvas(gameMap: GameMap) : Renderable {
|
||||||
|
private var layers: List<Layer>
|
||||||
|
private var rows: Int
|
||||||
|
private var columns: Int
|
||||||
|
private var tileWidth: Double
|
||||||
|
private var tileHeight: Double
|
||||||
|
private var mapWidth: Double
|
||||||
|
private var mapHeight: Double
|
||||||
|
|
||||||
|
init {
|
||||||
|
layers = gameMap.layers
|
||||||
|
rows = gameMap.rows
|
||||||
|
columns = gameMap.columns
|
||||||
|
tileWidth = gameMap.tileSet.tileWidth.toDouble()
|
||||||
|
tileHeight = gameMap.tileSet.tileHeight.toDouble()
|
||||||
|
mapWidth = columns * tileWidth
|
||||||
|
mapHeight = rows * tileHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateMap(gameMap: GameMap) {
|
||||||
|
layers = gameMap.layers
|
||||||
|
rows = gameMap.rows
|
||||||
|
columns = gameMap.columns
|
||||||
|
tileWidth = gameMap.tileSet.tileWidth.toDouble()
|
||||||
|
tileHeight = gameMap.tileSet.tileHeight.toDouble()
|
||||||
|
mapWidth = columns * tileWidth
|
||||||
|
mapHeight = rows * tileHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun render(gc: GraphicsContext) {
|
||||||
|
layers.forEach { dispatchLayerRender(gc, it) }
|
||||||
|
renderGrid(gc)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun dispatchLayerRender(gc: GraphicsContext, layer: Layer) {
|
||||||
|
when (layer) {
|
||||||
|
is TileLayer -> renderTileLayer(gc, layer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun renderTileLayer(gc: GraphicsContext, tileLayer: TileLayer) {
|
||||||
|
for ((row, columns) in tileLayer.layer.withIndex()) {
|
||||||
|
for ((column, tile) in columns.withIndex()) {
|
||||||
|
if (tile != null) {
|
||||||
|
gc.drawImage(tile.image, column * tile.image.width, row * tile.image.height)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun renderGrid(gc: GraphicsContext) {
|
||||||
|
gc.lineWidth = 1.5
|
||||||
|
|
||||||
|
for (row in 0 until rows) {
|
||||||
|
gc.strokeLine(0.0, row * tileHeight, mapWidth, row * tileHeight)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (column in 0 until columns) {
|
||||||
|
gc.strokeLine(column * tileWidth, 0.0, column * tileWidth, mapHeight)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.bartlomiejpluta.base.editor.view.render
|
package com.bartlomiejpluta.base.editor.render.model
|
||||||
|
|
||||||
import javafx.scene.canvas.GraphicsContext
|
import javafx.scene.canvas.GraphicsContext
|
||||||
|
|
||||||
@@ -1,15 +1,21 @@
|
|||||||
package com.bartlomiejpluta.base.editor.view.component.map
|
package com.bartlomiejpluta.base.editor.view.component.map
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.model.map.GameMap
|
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
||||||
import com.bartlomiejpluta.base.editor.view.render.Renderer
|
import com.bartlomiejpluta.base.editor.render.canvas.map.MapCanvas
|
||||||
|
import com.bartlomiejpluta.base.editor.view.render.renderer.Renderer
|
||||||
import javafx.scene.canvas.Canvas
|
import javafx.scene.canvas.Canvas
|
||||||
|
|
||||||
class MapPane(map: GameMap) : Canvas() {
|
class MapPane(map: GameMap) : Canvas() {
|
||||||
private val renderer = Renderer(graphicsContext2D, map)
|
private val mapCanvas = MapCanvas(map)
|
||||||
|
private val renderer = Renderer(graphicsContext2D, mapCanvas)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
width = map.width.toDouble()
|
width = map.width.toDouble()
|
||||||
height = map.height.toDouble()
|
height = map.height.toDouble()
|
||||||
renderer.start()
|
renderer.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun updateMap(map: GameMap) {
|
||||||
|
mapCanvas.updateMap(map)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.editor.view.fragment
|
package com.bartlomiejpluta.base.editor.view.fragment
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.editor.model.map.GameMap
|
import com.bartlomiejpluta.base.editor.model.map.map.GameMap
|
||||||
import com.bartlomiejpluta.base.editor.view.component.map.MapPane
|
import com.bartlomiejpluta.base.editor.view.component.map.MapPane
|
||||||
import tornadofx.Fragment
|
import tornadofx.Fragment
|
||||||
import tornadofx.group
|
import tornadofx.group
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.editor.view.render
|
package com.bartlomiejpluta.base.editor.view.render.renderer
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.render.model.Renderable
|
||||||
import javafx.animation.AnimationTimer
|
import javafx.animation.AnimationTimer
|
||||||
import javafx.scene.canvas.GraphicsContext
|
import javafx.scene.canvas.GraphicsContext
|
||||||
|
|
||||||
Reference in New Issue
Block a user