refactor spritesheet texture loader

main
Matthias 2 years 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.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<XmlReader.Element>
private val subTextures = Array<XmlReader.Element>()
private val textures: HashMap<String, TextureRegion> = 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"))
}
}

@ -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 {

@ -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

@ -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 ->

Loading…
Cancel
Save