diff --git a/assets/sprites/genericItems_spritesheet_colored.png b/assets/sprites/genericItems_spritesheet_colored.png new file mode 100644 index 0000000..163206b Binary files /dev/null and b/assets/sprites/genericItems_spritesheet_colored.png differ diff --git a/assets/sprites/genericItems_spritesheet_colored.xml b/assets/sprites/genericItems_spritesheet_colored.xml new file mode 100644 index 0000000..3eaec21 --- /dev/null +++ b/assets/sprites/genericItems_spritesheet_colored.xml @@ -0,0 +1,165 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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 3b3b365..8f7143d 100644 --- a/core/src/main/kotlin/com/last/commit/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/FirstScreen.kt @@ -15,6 +15,7 @@ import com.badlogic.gdx.math.Vector3 import com.badlogic.gdx.utils.Json import com.last.commit.config.GameConfig import com.last.commit.map.TimeMap +import com.last.commit.stages.InventoryStage import kotlin.math.floor @@ -30,6 +31,8 @@ class FirstScreen : Screen, InputProcessor { val playerTexture = Texture("sprites/characters.png") val player = Player(TextureRegion(playerTexture, 300, 44, 35, 43)) + lateinit var inventoryStage: InventoryStage + override fun show() { // Prepare your screen here. @@ -40,6 +43,8 @@ class FirstScreen : Screen, InputProcessor { this.spawnPlayer() this.updateCamera() + player.addItemToInventory("genericItem_color_001.png") + inventoryStage = InventoryStage(player.inventory) Gdx.input.setInputProcessor(this) } @@ -69,6 +74,8 @@ class FirstScreen : Screen, InputProcessor { this.map.render(batch, camera, delta) this.player.render(batch) batch.end() + + inventoryStage.draw() } private fun getMousePosition(): Vector2 { @@ -195,6 +202,8 @@ class FirstScreen : Screen, InputProcessor { openDoor() } else if (character == 't') { map.teleport(player) + } else if (character == 'i') { + inventoryStage.visible = !inventoryStage.visible } // TODO Auto-generated method stub return false diff --git a/core/src/main/kotlin/com/last/commit/Player.kt b/core/src/main/kotlin/com/last/commit/Player.kt index dff382b..c24e96f 100644 --- a/core/src/main/kotlin/com/last/commit/Player.kt +++ b/core/src/main/kotlin/com/last/commit/Player.kt @@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.TextureRegion import com.badlogic.gdx.math.Rectangle import com.badlogic.gdx.math.Vector2 +import com.last.commit.inventory.Inventory class Player(private val textureRegion: TextureRegion) : Collidable { @@ -14,12 +15,18 @@ class Player(private val textureRegion: TextureRegion) : Collidable { private val movementSpeed = 200f private val interactionRange = 60f + val inventory = Inventory() + init { val size = Math.max(textureRegion.regionWidth, textureRegion.regionHeight).toFloat() collider = Rectangle(0f, 0f, size, size) position = Vector2() } + fun addItemToInventory(name: String) { + this.inventory.add(name) + } + fun getX(): Float { return position.x } diff --git a/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt b/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt new file mode 100644 index 0000000..072367a --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt @@ -0,0 +1,15 @@ +package com.last.commit.inventory + +class Inventory { + + val items: MutableList = ArrayList() + val textureLoader = InventoryItemTextureLoader() + + init { + textureLoader.parse() + } + + fun add(name: String) { + items.add(InventoryItem(name, textureLoader.loadTexture(name))) + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/inventory/InventoryItem.kt b/core/src/main/kotlin/com/last/commit/inventory/InventoryItem.kt new file mode 100644 index 0000000..2604cf1 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/inventory/InventoryItem.kt @@ -0,0 +1,13 @@ +package com.last.commit.inventory + +import com.badlogic.gdx.graphics.g2d.TextureRegion + +class InventoryItem(name: String, texture: TextureRegion) { + val texture: TextureRegion + val name: String + + init { + this.name = name + this.texture = texture + } +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt b/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt new file mode 100644 index 0000000..83e546e --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt @@ -0,0 +1,30 @@ +package com.last.commit.inventory + +import com.badlogic.gdx.Gdx +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 { + + private val itemsSpriteSheet = Texture("sprites/genericItems_spritesheet_colored.png") + private lateinit var subTextures: Array + + fun loadTexture(itemName: String): TextureRegion { + 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") + return TextureRegion(itemsSpriteSheet, x, y, width, height) + } + + fun parse() { + val xml = XmlReader() + val textureAtlasElement = xml.parse(Gdx.files.local("sprites/genericItems_spritesheet_colored.xml")) + this.subTextures = textureAtlasElement.getChildrenByName("SubTexture") + println("Found ${subTextures.size} textures") + } + +} \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/stages/InventoryStage.kt b/core/src/main/kotlin/com/last/commit/stages/InventoryStage.kt new file mode 100644 index 0000000..8924e35 --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/stages/InventoryStage.kt @@ -0,0 +1,31 @@ +package com.last.commit.stages + +import com.badlogic.gdx.scenes.scene2d.Stage +import com.badlogic.gdx.scenes.scene2d.ui.Image +import com.last.commit.inventory.Inventory + +class InventoryStage(inventory: Inventory) : Stage() { + + var visible = false + + init { + for (item in inventory.items) { + + val image = Image(item.texture) + image.width = 32f + image.height = 32f + + addActor(image) + } + } + + fun resize(width: Int, height: Int) { + viewport.update(width, height, true) + } + + override fun draw() { + if (visible) { + super.draw() + } + } +} \ No newline at end of file