remove state from inventory

main
Matthias 1 year ago
parent fb3f82726c
commit 7bd1cd6b5f

@ -1,7 +1,5 @@
package com.last.commit.inventory package com.last.commit.inventory
import com.last.commit.GameState
class Inventory() { class Inventory() {
val items: MutableList<InventoryItem> = ArrayList() val items: MutableList<InventoryItem> = ArrayList()
@ -12,28 +10,25 @@ class Inventory() {
* @param name the name of the subtexture loaded from xml * @param name the name of the subtexture loaded from xml
* @return wether the item was added * @return wether the item was added
*/ */
fun add(name: String, state: GameState): Boolean { fun add(name: String) {
if (this.items.find { it.name == name } == null) { items.add(InventoryItem(name))
if (this.items.size < 8) { this.updated = true
items.add(InventoryItem(name)) }
this.updated = true
return true fun hasItem(name: String): Boolean {
} return this.items.find { it.name == name } == null
} else { }
// Item is already in inventory
state.dialogStage.setTexts("You already have this item.") fun isFull(): Boolean {
state.dialogStage.show() return items.size >= 7
}
return false
} }
fun remove(name: String) { fun remove(name: String) {
items.removeIf() { item -> item.name == name } items.removeIf() { item -> item.name == name }
} }
fun checkVictoryCondition(): Boolean { fun checkVictoryCondition(): Boolean {
return requiredItems.all {itemName -> return requiredItems.all { itemName ->
items.find { it.name == itemName } != null items.find { it.name == itemName } != null
} }
} }

@ -26,8 +26,13 @@ class Collectible(
override fun interact(otherCollider: Rectangle, state: GameState) { override fun interact(otherCollider: Rectangle, state: GameState) {
println("Interacting with item $name") println("Interacting with item $name")
state.soundEngine.play(GameSoundEffect.GRAB) state.soundEngine.play(GameSoundEffect.GRAB)
if (state.inventory.add(this.name, state)) { if (state.inventory.hasItem(this.name)) {
state.map?.collectibles?.remove(this) state.dialogStage.setTexts("You already have this item.")
state.dialogStage.show()
} else if (state.inventory.isFull()) {
state.dialogStage.setTexts("You can't carry anymore items.")
} else {
state.inventory.add(this.name)
} }
} }

Loading…
Cancel
Save