refactor spritesheet texture loader

main
Matthias 1 year ago
parent 7bd1cd6b5f
commit 9711e28341

@ -7,42 +7,42 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.utils.Array import com.badlogic.gdx.utils.Array
import com.badlogic.gdx.utils.XmlReader 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 itemsSpriteSheet: Texture
private val textureMapping: FileHandle private val textureMapping: FileHandle
private lateinit var subTextures: Array<XmlReader.Element> private val subTextures = Array<XmlReader.Element>()
private val textures: HashMap<String, TextureRegion> = HashMap() private val textures: HashMap<String, TextureRegion> = HashMap()
private var initialized: Boolean = false
init { init {
println("Loading textures from $path")
itemsSpriteSheet = Texture("${path}.png") itemsSpriteSheet = Texture("${path}.png")
textureMapping = Gdx.files.local("${path}.xml") textureMapping = Gdx.files.local("${path}.xml")
parse()
println("Loaded ${subTextures.size} textures")
} }
fun getTexture(itemName: String): TextureRegion { fun getTexture(itemName: String): TextureRegion {
if (!initialized) { var itemTexture = textures[itemName]
this.parse()
}
var itemTexture = textures.get(itemName)
if (itemTexture == null) { if (itemTexture == null) {
var subtexture = subTextures.first { it.getAttribute("name") == itemName } val subTexture = subTextures.first { it.getAttribute("name") == itemName }
val x = subtexture.getIntAttribute("x") val x = subTexture.getIntAttribute("x")
val y = subtexture.getIntAttribute("y") val y = subTexture.getIntAttribute("y")
val width = subtexture.getIntAttribute("width") val width = subTexture.getIntAttribute("width")
val height = subtexture.getIntAttribute("height") val height = subTexture.getIntAttribute("height")
itemTexture = TextureRegion(itemsSpriteSheet, x, y, width, height) itemTexture = TextureRegion(itemsSpriteSheet, x, y, width, height)
this.textures.set(itemName, itemTexture) this.textures[itemName] = itemTexture
} }
return itemTexture return itemTexture
} }
fun parse() { private fun parse() {
val xml = XmlReader() val xml = XmlReader()
val textureAtlasElement = xml.parse(textureMapping) val textureAtlasElement = xml.parse(textureMapping)
this.subTextures = textureAtlasElement.getChildrenByName("SubTexture") this.subTextures.addAll(textureAtlasElement.getChildrenByName("SubTexture"))
println("Found ${subTextures.size} textures")
this.initialized = true
} }
} }

@ -8,10 +8,10 @@ import com.badlogic.gdx.math.Rectangle
import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.last.commit.Wall import com.last.commit.Wall
import com.last.commit.inventory.InventoryItemTextureLoader import com.last.commit.inventory.SpritesheetTextureLoader
import kotlin.math.round 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 private val CELL_SIZE = 64
val size: Vector2 val size: Vector2
@ -101,15 +101,15 @@ class MapState(val map: TiledMap, val textureLoader: InventoryItemTextureLoader)
for (row in 0 until wallsLayer.height) { for (row in 0 until wallsLayer.height) {
val cell = wallsLayer.getCell(column, row) ?: continue val cell = wallsLayer.getCell(column, row) ?: continue
val isDoor: Boolean = val isDoor: Boolean =
cell.getTile().getProperties().get("isDoor", false, Boolean::class.java) cell.getTile().getProperties().get("isDoor", false, Boolean::class.java)
val wallCollider = val wallCollider =
Rectangle( Rectangle(
column.toFloat() * wallsLayer.tileWidth, column.toFloat() * wallsLayer.tileWidth,
row.toFloat() * wallsLayer.tileHeight, row.toFloat() * wallsLayer.tileHeight,
wallsLayer.tileWidth.toFloat(), wallsLayer.tileWidth.toFloat(),
wallsLayer.tileHeight.toFloat() wallsLayer.tileHeight.toFloat()
) )
if (java.lang.Boolean.TRUE == isDoor) { if (java.lang.Boolean.TRUE == isDoor) {
doors.add(Door(column, row, wallCollider, cell)) doors.add(Door(column, row, wallCollider, cell))
} else { } else {

@ -13,12 +13,12 @@ import com.last.commit.Collidable
import com.last.commit.GameState import com.last.commit.GameState
import com.last.commit.Player import com.last.commit.Player
import com.last.commit.audio.GameSoundEffect 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) { class TimeMap(fileName: String, val state: GameState) {
private val CELL_SIZE = 64 private val CELL_SIZE = 64
val textureLoader = InventoryItemTextureLoader("sprites/genericItems_spritesheet_colored") val textureLoader = SpritesheetTextureLoader("sprites/genericItems_spritesheet_colored")
val mapLoader: TmxMapLoader = TmxMapLoader() val mapLoader: TmxMapLoader = TmxMapLoader()
var mapRenderer: OrthogonalTiledMapRenderer var mapRenderer: OrthogonalTiledMapRenderer

@ -2,27 +2,20 @@ package com.last.commit.stages
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.BitmapFont 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.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Image import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.badlogic.gdx.scenes.scene2d.ui.Label 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.badlogic.gdx.utils.viewport.ExtendViewport
import com.last.commit.GameState 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)) { 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) private val labelStyle = Label.LabelStyle(BitmapFont(), Color.BLACK)
var mapLabel = Label("unknown time", labelStyle) var mapLabel = Label("unknown time", labelStyle)
var fpsLabel = Label("0", labelStyle) var fpsLabel = Label("0", labelStyle)
private var lastFpsUpdate = 0L private var lastFpsUpdate = 0L
init {
textureLoader.parse()
}
fun refresh() { fun refresh() {
super.clear() super.clear()
state.inventory.items.forEachIndexed { index, inventoryItem -> state.inventory.items.forEachIndexed { index, inventoryItem ->

Loading…
Cancel
Save