From 4d108068b3843db1247b96e78cb863b27fd45b03 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 20 Nov 2022 11:00:40 +0100 Subject: [PATCH] add inventory check items can only be used once --- assets/tiled/level_herbst.tmx | 12 ++++++++- .../src/main/kotlin/com/last/commit/Player.kt | 4 --- .../com/last/commit/inventory/Inventory.kt | 26 ++++++++++++++----- .../kotlin/com/last/commit/map/Collectible.kt | 5 ++-- .../com/last/commit/screen/FirstScreen.kt | 1 - .../com/last/commit/stages/DialogStage.kt | 4 +++ 6 files changed, 37 insertions(+), 15 deletions(-) diff --git a/assets/tiled/level_herbst.tmx b/assets/tiled/level_herbst.tmx index 647bfbf..f6941a1 100644 --- a/assets/tiled/level_herbst.tmx +++ b/assets/tiled/level_herbst.tmx @@ -1,5 +1,5 @@ - + @@ -504,6 +504,16 @@ + + + + + + + + + + diff --git a/core/src/main/kotlin/com/last/commit/Player.kt b/core/src/main/kotlin/com/last/commit/Player.kt index 0d320b9..65e0af5 100644 --- a/core/src/main/kotlin/com/last/commit/Player.kt +++ b/core/src/main/kotlin/com/last/commit/Player.kt @@ -22,10 +22,6 @@ class Player(private val textureRegion: TextureRegion, private val gameState: Ga position = Vector2() } - fun addItemToInventory(name: String) { - gameState.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 index b76e193..6dbfcc0 100644 --- a/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt +++ b/core/src/main/kotlin/com/last/commit/inventory/Inventory.kt @@ -1,21 +1,33 @@ package com.last.commit.inventory -class Inventory { +import com.last.commit.GameState + +class Inventory() { val items: MutableList = ArrayList() public var updated = false /** * @param name the name of the subtexture loaded from xml + * @return wether the item was added */ - fun add(name: String) { - if (this.items.size < 8) { - items.add(InventoryItem(name)) - this.updated = true + fun add(name: String, state: GameState): Boolean { + if (this.items.find { it.name == name } == null) { + if (this.items.size < 8) { + items.add(InventoryItem(name)) + this.updated = true + + return true + } + } else { + // Item is already in inventory + state.dialogStage.setTexts("You already have this item.") + state.dialogStage.show() } + return false } - + fun remove(name: String) { - items.removeIf() {item -> item.name == name} + items.removeIf() { item -> item.name == name } } } \ 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 ce1a9e7..d4b8b7a 100644 --- a/core/src/main/kotlin/com/last/commit/map/Collectible.kt +++ b/core/src/main/kotlin/com/last/commit/map/Collectible.kt @@ -24,8 +24,9 @@ class Collectible( override fun interact(otherCollider: Rectangle, state: GameState) { println("Interacting with item $name") state.soundEngine.play(GameSoundEffect.GRAB) - state.inventory.add(this.name) - state.map?.collectibles?.remove(this) + if (state.inventory.add(this.name, state)) { + state.map?.collectibles?.remove(this) + } } override fun canInteract(state: GameState): Boolean { diff --git a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt index 8c7662d..34eb4fc 100644 --- a/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt +++ b/core/src/main/kotlin/com/last/commit/screen/FirstScreen.kt @@ -63,7 +63,6 @@ class FirstScreen(private val parent: Game) : TimeTravelScreen() { uiStage = UIStage("sprites/genericItems_spritesheet_colored", gameState) shapeRenderer.setAutoShapeType(true) - player.addItemToInventory("drill") gameState.soundEngine.play(GameMusic.WORLD_MUSIC, 0.25f) viewport.camera = camera diff --git a/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt b/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt index 92fca30..82f37b5 100644 --- a/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt +++ b/core/src/main/kotlin/com/last/commit/stages/DialogStage.kt @@ -76,6 +76,10 @@ class DialogStage(skin: Skin?) : Stage() { return true } + override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean { + return keyDown(Input.Keys.SPACE) + } + private operator fun next() { area.clear() area.text = texts.first()