From e7f221a49902472640ada8a1d2c9407ba940099a Mon Sep 17 00:00:00 2001 From: Song Minjae Date: Tue, 11 Apr 2017 21:12:51 +0900 Subject: [PATCH] sanity check on equipping the item --- src/net/torvald/terrarum/gameactors/Pocketed.kt | 10 +++++++++- src/net/torvald/terrarum/gameitem/InventoryItem.kt | 10 ++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/net/torvald/terrarum/gameactors/Pocketed.kt b/src/net/torvald/terrarum/gameactors/Pocketed.kt index 5e6d5db3f..79108b642 100644 --- a/src/net/torvald/terrarum/gameactors/Pocketed.kt +++ b/src/net/torvald/terrarum/gameactors/Pocketed.kt @@ -11,7 +11,9 @@ interface Pocketed { var inventory: ActorInventory - + /** + * Equips an item. If the item is not in the inventory, an error will be thrown. + */ fun unequipItem(item: InventoryItem) { if (item.equipPosition == InventoryItem.EquipPosition.NULL) throw Error("Unequipping the item that cannot be equipped") @@ -23,7 +25,13 @@ interface Pocketed { item.effectOnUnequip(Terrarum.appgc, Terrarum.UPDATE_DELTA) } + /** + * Equips an item. If the item is not in the inventory, adds the item first. + */ fun equipItem(item: InventoryItem) { + if (!inventory.hasItem(item)) + inventory.add(item) + if (item.equipPosition >= 0) { inventory.itemEquipped[item.equipPosition] = item item.effectWhenEquipped(Terrarum.appgc, Terrarum.UPDATE_DELTA) diff --git a/src/net/torvald/terrarum/gameitem/InventoryItem.kt b/src/net/torvald/terrarum/gameitem/InventoryItem.kt index 948e60dee..3bf2fefa3 100644 --- a/src/net/torvald/terrarum/gameitem/InventoryItem.kt +++ b/src/net/torvald/terrarum/gameitem/InventoryItem.kt @@ -109,16 +109,18 @@ abstract class InventoryItem : Comparable { open fun effectWhenPickedUp(gc: GameContainer, delta: Int) { } /** - * Effects applied (continuously or not) while primary button (usually left mouse button) is down + * Apply effects (continuously or not) while primary button (usually left mouse button) is down. + * The item will NOT be consumed, so you will want to consume it yourself by inventory.consumeItem(item) * - * @return true when use successfully, false otherwise + * @return true when used successfully, false otherwise */ open fun primaryUse(gc: GameContainer, delta: Int): Boolean = false /** - * Effects applied (continuously or not) while secondary button (usually right mouse button) is down + * Apply effects (continuously or not) while secondary button (usually right mouse button) is down + * The item will NOT be consumed, so you will want to consume it yourself by inventory.consumeItem(item) * - * @return true when use successfully, false otherwise + * @return true when used successfully, false otherwise */ open fun secondaryUse(gc: GameContainer, delta: Int): Boolean = false