From 2089ce4ee24e53f19689bfc34e6824cfa7572902 Mon Sep 17 00:00:00 2001 From: Song Minjae Date: Fri, 16 Dec 2016 23:02:21 +0900 Subject: [PATCH] Inventory now stores objects instead of item IDs Former-commit-id: 6d7397fe82c986a3ac48ce86fe9fa75a354cf5d3 Former-commit-id: 46b759e0707fe2f7cb207e215708118034aff4f4 --- src/net/torvald/terrarum/console/Inventory.kt | 7 +-- .../terrarum/gameactors/ActorHumanoid.kt | 33 +++++++++++-- .../terrarum/gameactors/ActorInventory.kt | 48 ++++++++----------- 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/net/torvald/terrarum/console/Inventory.kt b/src/net/torvald/terrarum/console/Inventory.kt index 2b58c72b2..32bd06917 100644 --- a/src/net/torvald/terrarum/console/Inventory.kt +++ b/src/net/torvald/terrarum/console/Inventory.kt @@ -21,7 +21,8 @@ internal object Inventory : ConsoleCommand { else { when (args[1]) { "list" -> listInventory() - "add" -> addItem(args[2].toInt(), args[3].toInt()) + "add" -> if (args.size > 3) addItem(args[2].toInt(), args[3].toInt()) + else addItem(args[2].toInt()) "target" -> setTarget(args[2].toInt()) "equip" -> equipItem(args[2].toInt()) else -> printUsage() @@ -54,11 +55,11 @@ internal object Inventory : ConsoleCommand { } private fun addItem(refId: Int, amount: Int = 1) { - target.inventory.add(ItemPropCodex.getProp(refId), amount) + target.inventory.add(ItemPropCodex[refId], amount) } private fun equipItem(refId: Int) { - val item = ItemPropCodex.getProp(refId) + val item = ItemPropCodex[refId] // if the item does not exist, add it first if (!target.inventory.contains(item)) { diff --git a/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt b/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt index 55cf9ad34..59edadd63 100644 --- a/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt +++ b/src/net/torvald/terrarum/gameactors/ActorHumanoid.kt @@ -8,7 +8,6 @@ import net.torvald.terrarum.gamecontroller.EnumKeyFunc import net.torvald.terrarum.gamecontroller.KeyMap import net.torvald.terrarum.gameitem.EquipPosition import net.torvald.terrarum.gameitem.InventoryItem -import net.torvald.terrarum.gameitem.InventoryItemAdapter import net.torvald.terrarum.realestate.RealEstateUtility import org.dyn4j.geometry.Vector2 import org.lwjgl.input.Controller @@ -134,13 +133,12 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) get() = this is Player // FIXME true iff composed by PlayableActorDelegate - private val nullItem = object : InventoryItemAdapter() { - override val itemID: Int = 0 + private val nullItem = object : InventoryItem() { + override val id: Int = 0 override val equipPosition: Int = EquipPosition.NULL override var mass: Double = 0.0 override var scale: Double = 1.0 } - override fun update(gc: GameContainer, delta: Int) { super.update(gc, delta) @@ -163,6 +161,33 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null) isRightDown = false isJumpDown = false } + + // update inventory items + inventory.forEach { item, amount -> + if (!itemEquipped.contains(item)) { // unequipped + item.effectWhileInPocket(gc, delta) + } + else { // equipped + item.effectWhenEquipped(gc, delta) + } + } + } + + fun unequipItem(item: InventoryItem) { + for (i in 0..itemEquipped.size - 1) { + val it = itemEquipped[i] + if (item == it) { + it.effectWhenUnEquipped(gameContainer, updateDelta) + itemEquipped[i] = null // remove from the array by nulling it + break + } + } + } + + fun equipItem(item: InventoryItem) { + if (item.equipPosition >= 0) { + itemEquipped[item.equipPosition] = item + } } private fun updateGamerControlBox(input: Input) { diff --git a/src/net/torvald/terrarum/gameactors/ActorInventory.kt b/src/net/torvald/terrarum/gameactors/ActorInventory.kt index 1f9d39655..24255e0a1 100644 --- a/src/net/torvald/terrarum/gameactors/ActorInventory.kt +++ b/src/net/torvald/terrarum/gameactors/ActorInventory.kt @@ -24,7 +24,7 @@ class ActorInventory() { /** * HashMap */ - private val itemList: HashMap = HashMap() + private val itemList: HashMap = HashMap() /** * Default constructor with no encumbrance. @@ -51,42 +51,42 @@ class ActorInventory() { } } - fun add(item: InventoryItem, count: Int = 1) = add(item.itemID, count) - fun add(itemID: Int, count: Int = 1) { - if (itemID == Player.PLAYER_REF_ID) + fun add(itemID: Int, count: Int = 1) = add(ItemPropCodex[itemID], count) + fun add(item: InventoryItem, count: Int = 1) { + if (item.id == Player.PLAYER_REF_ID) throw IllegalArgumentException("Attempted to put human player into the inventory.") if (Terrarum.ingame.playableActorDelegate != null && - itemID == Terrarum.ingame.player.referenceID) + item.id == Terrarum.ingame.player.referenceID) throw IllegalArgumentException("Attempted to put active player into the inventory.") // If we already have the item, increment the amount // If not, add item with specified amount - itemList.put(itemID, itemList[itemID] ?: 0 + count) + itemList.put(item, itemList[item] ?: 0 + count) } - fun remove(item: InventoryItem, count: Int = 1) = remove(item.itemID, count) - fun remove(itemID: Int, count: Int = 1) { + fun remove(itemID: Int, count: Int = 1) = remove(ItemPropCodex[itemID], count) + fun remove(item: InventoryItem, count: Int = 1) { // check if the item does NOT exist - if (itemList[itemID] == null) { + if (itemList[item] == null) { return } else { // remove the existence of the item if count <= 0 - if (itemList[itemID]!! - count <= 0) { - itemList.remove(itemID) + if (itemList[item]!! - count <= 0) { + itemList.remove(item) } // else, decrement the item count else { - itemList.put(itemID, itemList[itemID]!! - count) + itemList.put(item, itemList[item]!! - count) } } } - fun contains(item: InventoryItem) = itemList.containsKey(item.itemID) - fun contains(itemID: Int) = itemList.containsKey(itemID) + fun contains(item: InventoryItem) = itemList.containsKey(item) + fun contains(itemID: Int) = itemList.containsKey(ItemPropCodex[itemID]) - fun forEach(consumer: (Int, Int) -> Unit) = itemList.forEach(consumer) + fun forEach(consumer: (InventoryItem, Int) -> Unit) = itemList.forEach(consumer) /** * Get capacity of inventory @@ -112,7 +112,7 @@ class ActorInventory() { * Get reference to the itemList * @return */ - fun getItemList(): Map? { + fun getItemList(): Map? { return itemList } @@ -121,16 +121,13 @@ class ActorInventory() { * @return */ @Suppress("UNCHECKED_CAST") - fun getCopyOfItemList(): Map? { - return itemList.clone() as Map + fun getCopyOfItemList(): Map? { + return itemList.clone() as Map } fun getTotalWeight(): Double { var weight = 0.0 - - for (item in itemList.entries) { - weight += ItemPropCodex.getProp(item.key).mass * item.value - } + itemList.forEach { item, i -> weight += item.mass * i } return weight } @@ -140,10 +137,7 @@ class ActorInventory() { */ fun getTotalCount(): Int { var count = 0 - - for (item in itemList.entries) { - count += item.value - } + itemList.forEach { item, i -> count += i } return count } @@ -152,7 +146,7 @@ class ActorInventory() { * Unique amount, multiple items are calculated as one */ fun getTotalUniqueCount(): Int { - return itemList.entries.size + return itemList.size } /**