From 796f2bead07221fc85a700896d99ca1a7ff3defc Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 19 Nov 2022 10:01:01 +0100 Subject: [PATCH] update inventory add readable names for easier reference update loading of files consistent (need to have same names) --- .../genericItems_spritesheet_colored.xml | 40 +++++++++---------- .../kotlin/com/last/commit/FirstScreen.kt | 2 +- .../com/last/commit/inventory/Inventory.kt | 2 +- .../inventory/InventoryItemTextureLoader.kt | 13 ++++-- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/assets/sprites/genericItems_spritesheet_colored.xml b/assets/sprites/genericItems_spritesheet_colored.xml index 3eaec21..9680ead 100644 --- a/assets/sprites/genericItems_spritesheet_colored.xml +++ b/assets/sprites/genericItems_spritesheet_colored.xml @@ -1,5 +1,5 @@ - + @@ -31,7 +31,7 @@ - + @@ -44,21 +44,21 @@ - + - - - + + + - + - + @@ -73,10 +73,10 @@ - + - + @@ -112,7 +112,7 @@ - + @@ -123,7 +123,7 @@ - + @@ -131,22 +131,22 @@ - + - + - - - - + + + + @@ -156,10 +156,10 @@ - + - + \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/FirstScreen.kt b/core/src/main/kotlin/com/last/commit/FirstScreen.kt index 8f7143d..1931b4a 100644 --- a/core/src/main/kotlin/com/last/commit/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/FirstScreen.kt @@ -43,7 +43,7 @@ class FirstScreen : Screen, InputProcessor { this.spawnPlayer() this.updateCamera() - player.addItemToInventory("genericItem_color_001.png") + player.addItemToInventory("drill") inventoryStage = InventoryStage(player.inventory) Gdx.input.setInputProcessor(this) diff --git a/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt b/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt index 072367a..4a99aac 100644 --- a/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt +++ b/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt @@ -3,7 +3,7 @@ package com.last.commit.inventory class Inventory { val items: MutableList = ArrayList() - val textureLoader = InventoryItemTextureLoader() + val textureLoader = InventoryItemTextureLoader("sprites/genericItems_spritesheet_colored") init { textureLoader.parse() diff --git a/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt b/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt index 83e546e..9afb8f0 100644 --- a/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt +++ b/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt @@ -1,16 +1,23 @@ package com.last.commit.inventory import com.badlogic.gdx.Gdx +import com.badlogic.gdx.files.FileHandle import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.utils.Array import com.badlogic.gdx.utils.XmlReader -class InventoryItemTextureLoader { +class InventoryItemTextureLoader(path: String) { - private val itemsSpriteSheet = Texture("sprites/genericItems_spritesheet_colored.png") + private val itemsSpriteSheet: Texture + private val textureMapping: FileHandle private lateinit var subTextures: Array + init { + itemsSpriteSheet = Texture("${path}.png") + textureMapping = Gdx.files.local("${path}.xml") + } + fun loadTexture(itemName: String): TextureRegion { var subtexture = subTextures.first { it.getAttribute("name") == itemName } val x = subtexture.getIntAttribute("x") @@ -22,7 +29,7 @@ class InventoryItemTextureLoader { fun parse() { val xml = XmlReader() - val textureAtlasElement = xml.parse(Gdx.files.local("sprites/genericItems_spritesheet_colored.xml")) + val textureAtlasElement = xml.parse(textureMapping) this.subTextures = textureAtlasElement.getChildrenByName("SubTexture") println("Found ${subTextures.size} textures") }