From cf99f9b6f72b0392e039c80253a594acf70215f6 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 19 Nov 2022 13:52:47 +0100 Subject: [PATCH] Add adding an item to the inventory when interacting with it --- .../kotlin/com/last/commit/FirstScreen.kt | 11 +++++---- core/src/main/kotlin/com/last/commit/Game.kt | 13 +++++++++- .../main/kotlin/com/last/commit/GameState.kt | 5 ++++ .../src/main/kotlin/com/last/commit/Player.kt | 7 +++--- .../com/last/commit/inventory/Inventory.kt | 16 +++++++------ .../last/commit/inventory/InventoryItem.kt | 3 +-- .../inventory/InventoryItemTextureLoader.kt | 24 ++++++++++++------- .../kotlin/com/last/commit/map/Collectible.kt | 4 +++- .../main/kotlin/com/last/commit/map/Door.kt | 17 ++++++++----- .../com/last/commit/map/Interactable.kt | 3 ++- .../kotlin/com/last/commit/map/TimeMap.kt | 5 ++-- .../com/last/commit/stages/InventoryStage.kt | 13 ++++++++-- 12 files changed, 81 insertions(+), 40 deletions(-) create mode 100644 core/src/main/kotlin/com/last/commit/GameState.kt diff --git a/core/src/main/kotlin/com/last/commit/FirstScreen.kt b/core/src/main/kotlin/com/last/commit/FirstScreen.kt index cd5e862..663bf25 100644 --- a/core/src/main/kotlin/com/last/commit/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/FirstScreen.kt @@ -20,9 +20,10 @@ import com.last.commit.map.Interactable import com.last.commit.map.TimeMap import com.last.commit.stages.InventoryStage import kotlin.math.floor +import GameState /** First screen of the application. Displayed after the application is created. */ -class FirstScreen : Screen, InputProcessor { +class FirstScreen(val gameState: GameState) : Screen, InputProcessor { val viewportSize = 800f @@ -34,7 +35,7 @@ class FirstScreen : Screen, InputProcessor { val camera = OrthographicCamera(viewportSize, viewportSize) lateinit var map: TimeMap // = TimeMap("tiled/base.tmx") val playerTexture = Texture("sprites/characters.png") - val player = Player(TextureRegion(playerTexture, 300, 44, 35, 43)) + val player = Player(TextureRegion(playerTexture, 300, 44, 35, 43), gameState) var shapeRenderer = ShapeRenderer() val highlightColor = Color(0f, 0f, 1f, 0.5f) @@ -46,14 +47,14 @@ class FirstScreen : Screen, InputProcessor { val gameConfig = this.loadGameConfig() val randomMap = gameConfig.getRandomMap() - map = TimeMap(randomMap) + map = TimeMap(randomMap, gameState) handleRatioChange() this.spawnPlayer() this.updateCamera() player.addItemToInventory("drill") - inventoryStage = InventoryStage(player.inventory) + inventoryStage = InventoryStage("sprites/genericItems_spritesheet_colored", gameState.inventory) shapeRenderer.setAutoShapeType(true) Gdx.input.setInputProcessor(this) @@ -258,7 +259,7 @@ class FirstScreen : Screen, InputProcessor { } else if (character == 'i') { inventoryStage.visible = !inventoryStage.visible } else if (character == 'p') { - player.inventory.add("compass") + gameState.inventory.add("compass") inventoryStage.refresh() } return false diff --git a/core/src/main/kotlin/com/last/commit/Game.kt b/core/src/main/kotlin/com/last/commit/Game.kt index 898697c..a10d043 100644 --- a/core/src/main/kotlin/com/last/commit/Game.kt +++ b/core/src/main/kotlin/com/last/commit/Game.kt @@ -1,11 +1,22 @@ package com.last.commit import com.badlogic.gdx.Game +import com.last.commit.inventory.Inventory +import GameState /** [com.badlogic.gdx.ApplicationListener] implementation shared by all platforms. */ class Game : Game() { + private lateinit var state: GameState + override fun create() { - setScreen(FirstScreen()) + createState() + setScreen(FirstScreen(state)) + } + + fun createState() { + state = GameState( + Inventory() + ) } } \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/GameState.kt b/core/src/main/kotlin/com/last/commit/GameState.kt new file mode 100644 index 0000000..0ea8cfe --- /dev/null +++ b/core/src/main/kotlin/com/last/commit/GameState.kt @@ -0,0 +1,5 @@ +import com.last.commit.inventory.Inventory + +public data class GameState( + public val inventory: Inventory, +) \ No newline at end of file diff --git a/core/src/main/kotlin/com/last/commit/Player.kt b/core/src/main/kotlin/com/last/commit/Player.kt index bc63d8d..1f39d08 100644 --- a/core/src/main/kotlin/com/last/commit/Player.kt +++ b/core/src/main/kotlin/com/last/commit/Player.kt @@ -5,9 +5,10 @@ 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 +import GameState -class Player(private val textureRegion: TextureRegion) : Collidable { +class Player(private val textureRegion: TextureRegion, private val gameState: GameState) : Collidable { private var collider: Rectangle = Rectangle(0f, 0f, 0f, 0f) var position: Vector2 = Vector2.Zero @@ -15,8 +16,6 @@ class Player(private val textureRegion: TextureRegion) : Collidable { private val movementSpeed = 200f private val interactionRange = 60f - val inventory = Inventory("sprites/genericItems_spritesheet_colored") - init { val size = Math.max(textureRegion.regionWidth, textureRegion.regionHeight).toFloat() collider = Rectangle(0f, 0f, size, size) @@ -24,7 +23,7 @@ class Player(private val textureRegion: TextureRegion) : Collidable { } fun addItemToInventory(name: String) { - this.inventory.add(name) + gameState.inventory.add(name) } fun getX(): Float { 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 61a8777..41aa062 100644 --- a/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt +++ b/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt @@ -1,18 +1,20 @@ package com.last.commit.inventory -class Inventory(path: String) { +class Inventory { val items: MutableList = ArrayList() - val textureLoader = InventoryItemTextureLoader(path) - - init { - textureLoader.parse() - } + public var updated = false + private set /** * @param name the name of the subtexture loaded from xml */ fun add(name: String) { - items.add(InventoryItem(name, textureLoader.loadTexture(name))) + items.add(InventoryItem(name)) + this.updated = true + } + + fun remove(name: String) { + items.removeIf() {item -> item.name == 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 index b899efa..f4c2112 100644 --- a/core/src/main/kotlin/com/last/commit/inventory/InventoryItem.kt +++ b/core/src/main/kotlin/com/last/commit/inventory/InventoryItem.kt @@ -2,6 +2,5 @@ package com.last.commit.inventory import com.badlogic.gdx.graphics.g2d.TextureRegion -class InventoryItem(val name: String, val texture: TextureRegion) { - +data class InventoryItem(public val name: String) { } \ 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 index 9afb8f0..4c2322c 100644 --- a/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt +++ b/core/src/main/kotlin/com/last/commit/inventory/InventoryItemTextureLoader.kt @@ -12,19 +12,26 @@ class InventoryItemTextureLoader(path: String) { private val itemsSpriteSheet: Texture private val textureMapping: FileHandle private lateinit var subTextures: Array + private val textures: HashMap = HashMap() 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") - val y = subtexture.getIntAttribute("y") - val width = subtexture.getIntAttribute("width") - val height = subtexture.getIntAttribute("height") - return TextureRegion(itemsSpriteSheet, x, y, width, height) + fun getTexture(itemName: String): TextureRegion { + var itemTexture = textures.get(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") + itemTexture = TextureRegion(itemsSpriteSheet, x, y, width, height) + this.textures.set(itemName, itemTexture) + } + return itemTexture } fun parse() { @@ -33,5 +40,4 @@ class InventoryItemTextureLoader(path: String) { 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/map/Collectible.kt b/core/src/main/kotlin/com/last/commit/map/Collectible.kt index 7da3d86..43cc65d 100644 --- a/core/src/main/kotlin/com/last/commit/map/Collectible.kt +++ b/core/src/main/kotlin/com/last/commit/map/Collectible.kt @@ -2,6 +2,7 @@ package com.last.commit.map import com.badlogic.gdx.math.Rectangle import Position +import GameState class Collectible( name: String, @@ -18,8 +19,9 @@ class Collectible( this.collider = Rectangle(pos.x, pos.y, width, height) } - override fun interact(otherCollider: Rectangle) { + override fun interact(otherCollider: Rectangle, state: GameState) { println("Interacting with item $name") + state.inventory.add(this.name) } override fun getCollider(): Rectangle { diff --git a/core/src/main/kotlin/com/last/commit/map/Door.kt b/core/src/main/kotlin/com/last/commit/map/Door.kt index f71a101..2a9df91 100644 --- a/core/src/main/kotlin/com/last/commit/map/Door.kt +++ b/core/src/main/kotlin/com/last/commit/map/Door.kt @@ -1,13 +1,14 @@ package com.last.commit.map +import GameState import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell import com.badlogic.gdx.math.Rectangle import com.last.commit.Wall - class Door(gridX: Int, gridY: Int, wallCollider: Rectangle, cell: Cell) : - Wall(gridX, gridY, wallCollider, cell), Interactable { - override fun interact(otherCollider: Rectangle) { + Wall(gridX, gridY, wallCollider, cell), Interactable { + + override fun interact(otherCollider: Rectangle, state: GameState) { println("interacting with door $this") if (isClosed) { isOpen = true @@ -42,8 +43,12 @@ class Door(gridX: Int, gridY: Int, wallCollider: Rectangle, cell: Cell) : override fun toString(): String { return String.format( - "Door: %f:%f - %f:%f (isOpen: %b)", wallCollider.x, wallCollider.y, wallCollider.width, - wallCollider.height, isOpen + "Door: %f:%f - %f:%f (isOpen: %b)", + wallCollider.x, + wallCollider.y, + wallCollider.width, + wallCollider.height, + isOpen ) } -} \ No newline at end of file +} diff --git a/core/src/main/kotlin/com/last/commit/map/Interactable.kt b/core/src/main/kotlin/com/last/commit/map/Interactable.kt index a5ca6bf..b111726 100644 --- a/core/src/main/kotlin/com/last/commit/map/Interactable.kt +++ b/core/src/main/kotlin/com/last/commit/map/Interactable.kt @@ -1,9 +1,10 @@ package com.last.commit.map import com.badlogic.gdx.math.Rectangle +import GameState interface Interactable { fun getCollider(): Rectangle - fun interact(otherCollider: Rectangle) + fun interact(otherCollider: Rectangle, state: GameState) } \ No newline at end of file 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 4383123..1f7627b 100644 --- a/core/src/main/kotlin/com/last/commit/map/TimeMap.kt +++ b/core/src/main/kotlin/com/last/commit/map/TimeMap.kt @@ -15,9 +15,10 @@ import com.last.commit.Collidable import com.last.commit.Player import com.last.commit.Wall import Position +import GameState -class TimeMap(fileName: String) { +class TimeMap(fileName: String, val state: GameState) { private val CELL_SIZE = 64 private val walls = Array() @@ -168,7 +169,7 @@ class TimeMap(fileName: String) { val interactable: Interactable = this.findInteractableAtPosition(gridX, gridY) ?: return //else continue - interactable.interact(blockingCollider) + interactable.interact(blockingCollider, state) } diff --git a/core/src/main/kotlin/com/last/commit/stages/InventoryStage.kt b/core/src/main/kotlin/com/last/commit/stages/InventoryStage.kt index d4d6143..e7c4d30 100644 --- a/core/src/main/kotlin/com/last/commit/stages/InventoryStage.kt +++ b/core/src/main/kotlin/com/last/commit/stages/InventoryStage.kt @@ -3,8 +3,14 @@ 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 +import com.last.commit.inventory.InventoryItemTextureLoader -class InventoryStage(val inventory: Inventory) : Stage() { +class InventoryStage(path: String, val inventory: Inventory) : Stage() { + val textureLoader = InventoryItemTextureLoader(path) + + init { + textureLoader.parse() + } var visible = false set(visible) { @@ -17,7 +23,7 @@ class InventoryStage(val inventory: Inventory) : Stage() { fun refresh() { super.clear() inventory.items.forEachIndexed { index, inventoryItem -> - val image = Image(inventoryItem.texture) + val image = Image(textureLoader.getTexture(inventoryItem.name)) image.x = index * 32f image.width = 32f image.height = 32f @@ -31,6 +37,9 @@ class InventoryStage(val inventory: Inventory) : Stage() { } override fun draw() { + if (inventory.updated) { + this.refresh() + } if (visible) { super.draw() }