From 9711e28341ae0e6863180e84ae8c66b870e9df4b Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 21 Nov 2022 20:42:20 +0100 Subject: [PATCH] refactor spritesheet texture loader --- ...eLoader.kt => SpritesheetTextureLoader.kt} | 34 +++++++++---------- .../kotlin/com/last/commit/map/MapState.kt | 18 +++++----- .../kotlin/com/last/commit/map/TimeMap.kt | 4 +-- .../kotlin/com/last/commit/stages/UIStage.kt | 11 ++---- 4 files changed, 30 insertions(+), 37 deletions(-) rename core/src/main/kotlin/com/last/commit/inventory/{InventoryItemTextureLoader.kt => SpritesheetTextureLoader.kt} (52%) diff --git a/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt b/core/src/main/kotlin/com/last/commit/inventory/SpritesheetTextureLoader.kt similarity index 52% rename from core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt rename to core/src/main/kotlin/com/last/commit/inventory/SpritesheetTextureLoader.kt index a8c917c..45614cd 100644 --- a/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt +++ b/core/src/main/kotlin/com/last/commit/inventory/SpritesheetTextureLoader.kt @@ -7,42 +7,42 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.utils.Array import com.badlogic.gdx.utils.XmlReader -class InventoryItemTextureLoader(path: String) { +/** + * Parses textures from a png file with the corresponding xml mapping file + */ +class SpritesheetTextureLoader(path: String) { private val itemsSpriteSheet: Texture private val textureMapping: FileHandle - private lateinit var subTextures: Array + private val subTextures = Array() private val textures: HashMap = HashMap() - private var initialized: Boolean = false init { + println("Loading textures from $path") itemsSpriteSheet = Texture("${path}.png") textureMapping = Gdx.files.local("${path}.xml") + parse() + println("Loaded ${subTextures.size} textures") } fun getTexture(itemName: String): TextureRegion { - if (!initialized) { - this.parse() - } - var itemTexture = textures.get(itemName) + var itemTexture = textures[itemName] if (itemTexture == null) { - var subtexture = subTextures.first { it.getAttribute("name") == itemName } - val x = subtexture.getIntAttribute("x") - val y = subtexture.getIntAttribute("y") - val width = subtexture.getIntAttribute("width") - val height = subtexture.getIntAttribute("height") + val subTexture = subTextures.first { it.getAttribute("name") == itemName } + val x = subTexture.getIntAttribute("x") + val y = subTexture.getIntAttribute("y") + val width = subTexture.getIntAttribute("width") + val height = subTexture.getIntAttribute("height") itemTexture = TextureRegion(itemsSpriteSheet, x, y, width, height) - this.textures.set(itemName, itemTexture) + this.textures[itemName] = itemTexture } return itemTexture } - fun parse() { + private fun parse() { val xml = XmlReader() val textureAtlasElement = xml.parse(textureMapping) - this.subTextures = textureAtlasElement.getChildrenByName("SubTexture") - println("Found ${subTextures.size} textures") - this.initialized = true + this.subTextures.addAll(textureAtlasElement.getChildrenByName("SubTexture")) } } diff --git a/core/src/main/kotlin/com/last/commit/map/MapState.kt b/core/src/main/kotlin/com/last/commit/map/MapState.kt index b5538dc..5196393 100644 --- a/core/src/main/kotlin/com/last/commit/map/MapState.kt +++ b/core/src/main/kotlin/com/last/commit/map/MapState.kt @@ -8,10 +8,10 @@ import com.badlogic.gdx.math.Rectangle import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.scenes.scene2d.ui.Image import com.last.commit.Wall -import com.last.commit.inventory.InventoryItemTextureLoader +import com.last.commit.inventory.SpritesheetTextureLoader import kotlin.math.round -class MapState(val map: TiledMap, val textureLoader: InventoryItemTextureLoader) { +class MapState(val map: TiledMap, val textureLoader: SpritesheetTextureLoader) { private val CELL_SIZE = 64 val size: Vector2 @@ -101,15 +101,15 @@ class MapState(val map: TiledMap, val textureLoader: InventoryItemTextureLoader) for (row in 0 until wallsLayer.height) { val cell = wallsLayer.getCell(column, row) ?: continue val isDoor: Boolean = - cell.getTile().getProperties().get("isDoor", false, Boolean::class.java) + cell.getTile().getProperties().get("isDoor", false, Boolean::class.java) val wallCollider = - Rectangle( - column.toFloat() * wallsLayer.tileWidth, - row.toFloat() * wallsLayer.tileHeight, - wallsLayer.tileWidth.toFloat(), - wallsLayer.tileHeight.toFloat() - ) + Rectangle( + column.toFloat() * wallsLayer.tileWidth, + row.toFloat() * wallsLayer.tileHeight, + wallsLayer.tileWidth.toFloat(), + wallsLayer.tileHeight.toFloat() + ) if (java.lang.Boolean.TRUE == isDoor) { doors.add(Door(column, row, wallCollider, cell)) } else { diff --git a/core/src/main/kotlin/com/last/commit/map/TimeMap.kt b/core/src/main/kotlin/com/last/commit/map/TimeMap.kt index c7801ee..55ed63e 100644 --- a/core/src/main/kotlin/com/last/commit/map/TimeMap.kt +++ b/core/src/main/kotlin/com/last/commit/map/TimeMap.kt @@ -13,12 +13,12 @@ import com.last.commit.Collidable import com.last.commit.GameState import com.last.commit.Player import com.last.commit.audio.GameSoundEffect -import com.last.commit.inventory.InventoryItemTextureLoader +import com.last.commit.inventory.SpritesheetTextureLoader class TimeMap(fileName: String, val state: GameState) { private val CELL_SIZE = 64 - val textureLoader = InventoryItemTextureLoader("sprites/genericItems_spritesheet_colored") + val textureLoader = SpritesheetTextureLoader("sprites/genericItems_spritesheet_colored") val mapLoader: TmxMapLoader = TmxMapLoader() var mapRenderer: OrthogonalTiledMapRenderer diff --git a/core/src/main/kotlin/com/last/commit/stages/UIStage.kt b/core/src/main/kotlin/com/last/commit/stages/UIStage.kt index 3b2cddf..92d0b58 100644 --- a/core/src/main/kotlin/com/last/commit/stages/UIStage.kt +++ b/core/src/main/kotlin/com/last/commit/stages/UIStage.kt @@ -2,27 +2,20 @@ package com.last.commit.stages import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.g2d.BitmapFont -import com.badlogic.gdx.graphics.glutils.ShapeRenderer import com.badlogic.gdx.scenes.scene2d.Stage import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.ui.Label -import com.badlogic.gdx.utils.viewport.FitViewport -import com.badlogic.gdx.utils.viewport.ScreenViewport import com.badlogic.gdx.utils.viewport.ExtendViewport import com.last.commit.GameState -import com.last.commit.inventory.InventoryItemTextureLoader +import com.last.commit.inventory.SpritesheetTextureLoader class UIStage(path: String, val state: GameState) : Stage(ExtendViewport(512f, 512f)) { - val textureLoader = InventoryItemTextureLoader(path) + val textureLoader = SpritesheetTextureLoader(path) private val labelStyle = Label.LabelStyle(BitmapFont(), Color.BLACK) var mapLabel = Label("unknown time", labelStyle) var fpsLabel = Label("0", labelStyle) private var lastFpsUpdate = 0L - init { - textureLoader.parse() - } - fun refresh() { super.clear() state.inventory.items.forEachIndexed { index, inventoryItem ->