diff --git a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt index c310abe..55bfa02 100644 --- a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt +++ b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt @@ -1,6 +1,8 @@ package com.persesgames.map.tiled +import com.persesgames.game.Game import com.persesgames.net.getUrlAsString +import com.persesgames.texture.Texture import com.persesgames.texture.Textures import java.util.* @@ -10,13 +12,6 @@ class MapRenderer(data: MapData) { - fun drawTile(tile: Int, x: Int, y: Int) { - - } - - fun drawLayer(layer: MapLayer) { - - } } class MapData { @@ -67,10 +62,24 @@ var tileproperties: MutableMap> = HashMap() } +class TilesetIndex( + val texture: Texture, + val tcLeft: Float, + val tcTop: Float, + val tcRight: Float, + val tcBottom: Float + ) { + fun render(x: Float, y: Float) { + texture.queueTileDraw(x, y, tcLeft, tcTop, tcRight, tcBottom) + } +} + class TiledMap(dir: String = "", url: String) { val properties: Map = HashMap() val data: MapData val tiles: Array + var first = true + //var tilesetIndex: Array = Array(0, { TilesetIndex() }) init { var tileDir = dir @@ -91,4 +100,77 @@ tiles = Array(0, { "" }) } } + + fun drawTile(tile: Int, x: Float, y: Float) { + if (first) { + println("Draw $tile on ($x, $y)") + } + val tilesets = data.tilesets + var name: String? = null + var gid: Int + var tcLeft = 0f + var tcTop = 0f + var tcRight = 0f + var tcBottom = 0f + + if (tilesets != null) { + for (tileset in tilesets) { + val tilesHor = tileset.imagewidth / tileset.tilewidth + val tilesVer = tileset.imageheight / tileset.tileheight + + if (tile >= tileset.firstgid && tile < tileset.firstgid + tileset.tilecount) { + name = tileset.name + gid = tile - tileset.firstgid + + val xi = gid % tilesHor + val yi = gid / tilesHor + val tw = 1f / tilesHor.toFloat() + val th = 1f / tilesVer.toFloat() + + tcLeft = xi * tw + tcTop = yi * th + tcRight = tcLeft + tw + tcBottom = tcTop - th + } + } + } + + if (name != null) { + val texture = Textures.get(name) + + texture.queueTileDraw(x, y, tcLeft, tcTop, tcRight, tcBottom) + } + } + + fun drawLayer(layerIndex: Int, xo: Float, yo: Float) { + var x = 0f + var y = 0f + val layers = data.layers ?: throw IllegalArgumentException("MapData has no layers ($data)") + val layer = layers[layerIndex] + + val layerData = layer.data + if (layerData != null) { + for (index in layerData.indices) { + // todo: determine if in view + // todo: determine tilewidth + if (xo+x*128f < Game.view.width && yo + y * 128 < Game.view.height) { + drawTile(layerData[index], xo + x * 128f, yo + y * 128f) + + when (data.renderorder) { + "right-down" -> { + x++ + if (x > layer.width) { + x = 0f + y++ + } + } + else -> { + throw IllegalStateException("Renderorder ${data.renderorder} not supported in $this") + } + } + } + } + } + first = false + } } diff --git a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt index c310abe..55bfa02 100644 --- a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt +++ b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt @@ -1,6 +1,8 @@ package com.persesgames.map.tiled +import com.persesgames.game.Game import com.persesgames.net.getUrlAsString +import com.persesgames.texture.Texture import com.persesgames.texture.Textures import java.util.* @@ -10,13 +12,6 @@ class MapRenderer(data: MapData) { - fun drawTile(tile: Int, x: Int, y: Int) { - - } - - fun drawLayer(layer: MapLayer) { - - } } class MapData { @@ -67,10 +62,24 @@ var tileproperties: MutableMap> = HashMap() } +class TilesetIndex( + val texture: Texture, + val tcLeft: Float, + val tcTop: Float, + val tcRight: Float, + val tcBottom: Float + ) { + fun render(x: Float, y: Float) { + texture.queueTileDraw(x, y, tcLeft, tcTop, tcRight, tcBottom) + } +} + class TiledMap(dir: String = "", url: String) { val properties: Map = HashMap() val data: MapData val tiles: Array + var first = true + //var tilesetIndex: Array = Array(0, { TilesetIndex() }) init { var tileDir = dir @@ -91,4 +100,77 @@ tiles = Array(0, { "" }) } } + + fun drawTile(tile: Int, x: Float, y: Float) { + if (first) { + println("Draw $tile on ($x, $y)") + } + val tilesets = data.tilesets + var name: String? = null + var gid: Int + var tcLeft = 0f + var tcTop = 0f + var tcRight = 0f + var tcBottom = 0f + + if (tilesets != null) { + for (tileset in tilesets) { + val tilesHor = tileset.imagewidth / tileset.tilewidth + val tilesVer = tileset.imageheight / tileset.tileheight + + if (tile >= tileset.firstgid && tile < tileset.firstgid + tileset.tilecount) { + name = tileset.name + gid = tile - tileset.firstgid + + val xi = gid % tilesHor + val yi = gid / tilesHor + val tw = 1f / tilesHor.toFloat() + val th = 1f / tilesVer.toFloat() + + tcLeft = xi * tw + tcTop = yi * th + tcRight = tcLeft + tw + tcBottom = tcTop - th + } + } + } + + if (name != null) { + val texture = Textures.get(name) + + texture.queueTileDraw(x, y, tcLeft, tcTop, tcRight, tcBottom) + } + } + + fun drawLayer(layerIndex: Int, xo: Float, yo: Float) { + var x = 0f + var y = 0f + val layers = data.layers ?: throw IllegalArgumentException("MapData has no layers ($data)") + val layer = layers[layerIndex] + + val layerData = layer.data + if (layerData != null) { + for (index in layerData.indices) { + // todo: determine if in view + // todo: determine tilewidth + if (xo+x*128f < Game.view.width && yo + y * 128 < Game.view.height) { + drawTile(layerData[index], xo + x * 128f, yo + y * 128f) + + when (data.renderorder) { + "right-down" -> { + x++ + if (x > layer.width) { + x = 0f + y++ + } + } + else -> { + throw IllegalStateException("Renderorder ${data.renderorder} not supported in $this") + } + } + } + } + } + first = false + } } diff --git a/lib/kotludens/com/persesgames/texture/Textures.kt b/lib/kotludens/com/persesgames/texture/Textures.kt index 93078c3..32d8c81 100644 --- a/lib/kotludens/com/persesgames/texture/Textures.kt +++ b/lib/kotludens/com/persesgames/texture/Textures.kt @@ -103,6 +103,19 @@ } } + fun queueTileDraw(x: Float, y: Float, tcLeft: Float, tcTop: Float, tcRight: Float, tcBottom: Float) { + shaderProgramMesh.queue( x, y, left, bottom, tcLeft, tcBottom, 1f/8f, 0f) + shaderProgramMesh.queue( x, y, left, top, tcLeft, tcTop, 1f/8f, 0f) + shaderProgramMesh.queue( x, y, right, top, tcRight, tcTop, 1f/8f, 0f) + shaderProgramMesh.queue( x, y, right, top, tcRight, tcTop, 1f/8f, 0f) + shaderProgramMesh.queue( x, y, right, bottom, tcRight, tcBottom, 1f/8f, 0f) + shaderProgramMesh.queue( x, y, left, bottom, tcLeft, tcBottom, 1f/8f, 0f) + + if (shaderProgramMesh.remaining() < 36) { + render() + } + } + fun render() { Game.gl().activeTexture(WebGLRenderingContext.TEXTURE0) Game.gl().bindTexture(WebGLRenderingContext.TEXTURE_2D, glTexture) @@ -217,7 +230,7 @@ fun ready() = loaded == startedLoading - fun get(name: String) = textures.get(name) ?: throw IllegalArgumentException("Texture with name $name is not loaded!") + fun get(name: String) = textures[name] ?: throw IllegalArgumentException("Texture with name $name is not loaded!") fun clear() { // delete and unbind all textures... diff --git a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt index c310abe..55bfa02 100644 --- a/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt +++ b/lib/kotludens/com/persesgames/map/tiled/TiledMap.kt @@ -1,6 +1,8 @@ package com.persesgames.map.tiled +import com.persesgames.game.Game import com.persesgames.net.getUrlAsString +import com.persesgames.texture.Texture import com.persesgames.texture.Textures import java.util.* @@ -10,13 +12,6 @@ class MapRenderer(data: MapData) { - fun drawTile(tile: Int, x: Int, y: Int) { - - } - - fun drawLayer(layer: MapLayer) { - - } } class MapData { @@ -67,10 +62,24 @@ var tileproperties: MutableMap> = HashMap() } +class TilesetIndex( + val texture: Texture, + val tcLeft: Float, + val tcTop: Float, + val tcRight: Float, + val tcBottom: Float + ) { + fun render(x: Float, y: Float) { + texture.queueTileDraw(x, y, tcLeft, tcTop, tcRight, tcBottom) + } +} + class TiledMap(dir: String = "", url: String) { val properties: Map = HashMap() val data: MapData val tiles: Array + var first = true + //var tilesetIndex: Array = Array(0, { TilesetIndex() }) init { var tileDir = dir @@ -91,4 +100,77 @@ tiles = Array(0, { "" }) } } + + fun drawTile(tile: Int, x: Float, y: Float) { + if (first) { + println("Draw $tile on ($x, $y)") + } + val tilesets = data.tilesets + var name: String? = null + var gid: Int + var tcLeft = 0f + var tcTop = 0f + var tcRight = 0f + var tcBottom = 0f + + if (tilesets != null) { + for (tileset in tilesets) { + val tilesHor = tileset.imagewidth / tileset.tilewidth + val tilesVer = tileset.imageheight / tileset.tileheight + + if (tile >= tileset.firstgid && tile < tileset.firstgid + tileset.tilecount) { + name = tileset.name + gid = tile - tileset.firstgid + + val xi = gid % tilesHor + val yi = gid / tilesHor + val tw = 1f / tilesHor.toFloat() + val th = 1f / tilesVer.toFloat() + + tcLeft = xi * tw + tcTop = yi * th + tcRight = tcLeft + tw + tcBottom = tcTop - th + } + } + } + + if (name != null) { + val texture = Textures.get(name) + + texture.queueTileDraw(x, y, tcLeft, tcTop, tcRight, tcBottom) + } + } + + fun drawLayer(layerIndex: Int, xo: Float, yo: Float) { + var x = 0f + var y = 0f + val layers = data.layers ?: throw IllegalArgumentException("MapData has no layers ($data)") + val layer = layers[layerIndex] + + val layerData = layer.data + if (layerData != null) { + for (index in layerData.indices) { + // todo: determine if in view + // todo: determine tilewidth + if (xo+x*128f < Game.view.width && yo + y * 128 < Game.view.height) { + drawTile(layerData[index], xo + x * 128f, yo + y * 128f) + + when (data.renderorder) { + "right-down" -> { + x++ + if (x > layer.width) { + x = 0f + y++ + } + } + else -> { + throw IllegalStateException("Renderorder ${data.renderorder} not supported in $this") + } + } + } + } + } + first = false + } } diff --git a/lib/kotludens/com/persesgames/texture/Textures.kt b/lib/kotludens/com/persesgames/texture/Textures.kt index 93078c3..32d8c81 100644 --- a/lib/kotludens/com/persesgames/texture/Textures.kt +++ b/lib/kotludens/com/persesgames/texture/Textures.kt @@ -103,6 +103,19 @@ } } + fun queueTileDraw(x: Float, y: Float, tcLeft: Float, tcTop: Float, tcRight: Float, tcBottom: Float) { + shaderProgramMesh.queue( x, y, left, bottom, tcLeft, tcBottom, 1f/8f, 0f) + shaderProgramMesh.queue( x, y, left, top, tcLeft, tcTop, 1f/8f, 0f) + shaderProgramMesh.queue( x, y, right, top, tcRight, tcTop, 1f/8f, 0f) + shaderProgramMesh.queue( x, y, right, top, tcRight, tcTop, 1f/8f, 0f) + shaderProgramMesh.queue( x, y, right, bottom, tcRight, tcBottom, 1f/8f, 0f) + shaderProgramMesh.queue( x, y, left, bottom, tcLeft, tcBottom, 1f/8f, 0f) + + if (shaderProgramMesh.remaining() < 36) { + render() + } + } + fun render() { Game.gl().activeTexture(WebGLRenderingContext.TEXTURE0) Game.gl().bindTexture(WebGLRenderingContext.TEXTURE_2D, glTexture) @@ -217,7 +230,7 @@ fun ready() = loaded == startedLoading - fun get(name: String) = textures.get(name) ?: throw IllegalArgumentException("Texture with name $name is not loaded!") + fun get(name: String) = textures[name] ?: throw IllegalArgumentException("Texture with name $name is not loaded!") fun clear() { // delete and unbind all textures... diff --git a/src/com/persesgames/shooter/Shooter.kt b/src/com/persesgames/shooter/Shooter.kt index 6608bca..77b30c0 100644 --- a/src/com/persesgames/shooter/Shooter.kt +++ b/src/com/persesgames/shooter/Shooter.kt @@ -5,6 +5,7 @@ import com.persesgames.input.EmptyInputProcessor import com.persesgames.input.KeyCode import com.persesgames.input.Keys +import com.persesgames.map.tiled.MapRenderer import com.persesgames.map.tiled.TiledMap import com.persesgames.sound.Music import com.persesgames.sound.Sound @@ -41,28 +42,15 @@ var showFPS: Boolean = true class WelcomeScreen: Screen() { - val map = TiledMap("maps", "level_1_01.json") override fun loadResources() { println("loading resource!") + //music = Music.play("music/DST-TechnoBasic.mp3", 1.0, looping = true) Textures.loadSpriteSheet("images/data-0.json") Keys.setInputProcessor(GameInputProcessor()) - - println("width: ${map.data.width}") - println("height: ${map.data.height}") - println("layers: ${map.data.layers?.size}") - val layers = map.data.layers - if (layers != null) { - println("layer0: ${layers[0].name}") - } - val tilesets = map.data.tilesets - if (tilesets != null) { - println("tilesets ${tilesets.size}") - println("tileset0: ${tilesets[0].name}") - } } override fun update(time: Float, delta: Float) { @@ -77,6 +65,8 @@ } class GameScreen: Screen() { + val map = TiledMap("maps", "level_1_01.json") + var sprites = SpriteBatch() var x = 0f var y = 0f @@ -93,6 +83,19 @@ music = Music.play("music/DST-TechnoBasic.mp3", 0.5, looping = true) Keys.setInputProcessor(GameInputProcessor()) + + println("width: ${map.data.width}") + println("height: ${map.data.height}") + println("layers: ${map.data.layers?.size}") + val layers = map.data.layers + if (layers != null) { + println("layer0: ${layers[0].name}") + } + val tilesets = map.data.tilesets + if (tilesets != null) { + println("tilesets ${tilesets.size}") + println("tileset0: ${tilesets[0].name}") + } } override fun update(time: Float, delta: Float) { @@ -132,6 +135,8 @@ var x = 0f var y = 0f + map.drawLayer(1, 0f, 0f) + val time = this.time / 10f for (index in 0..numberOfSprites) { r = index * 0.05f