Inventory now stores objects instead of item IDs

Former-commit-id: 6d7397fe82c986a3ac48ce86fe9fa75a354cf5d3
Former-commit-id: 46b759e0707fe2f7cb207e215708118034aff4f4
This commit is contained in:
Song Minjae
2016-12-16 23:02:21 +09:00
parent fe994621c0
commit 4552d7b7db
3 changed files with 54 additions and 34 deletions

View File

@@ -21,7 +21,8 @@ internal object Inventory : ConsoleCommand {
else { else {
when (args[1]) { when (args[1]) {
"list" -> listInventory() "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()) "target" -> setTarget(args[2].toInt())
"equip" -> equipItem(args[2].toInt()) "equip" -> equipItem(args[2].toInt())
else -> printUsage() else -> printUsage()
@@ -54,11 +55,11 @@ internal object Inventory : ConsoleCommand {
} }
private fun addItem(refId: Int, amount: Int = 1) { 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) { private fun equipItem(refId: Int) {
val item = ItemPropCodex.getProp(refId) val item = ItemPropCodex[refId]
// if the item does not exist, add it first // if the item does not exist, add it first
if (!target.inventory.contains(item)) { if (!target.inventory.contains(item)) {

View File

@@ -8,7 +8,6 @@ import net.torvald.terrarum.gamecontroller.EnumKeyFunc
import net.torvald.terrarum.gamecontroller.KeyMap import net.torvald.terrarum.gamecontroller.KeyMap
import net.torvald.terrarum.gameitem.EquipPosition import net.torvald.terrarum.gameitem.EquipPosition
import net.torvald.terrarum.gameitem.InventoryItem import net.torvald.terrarum.gameitem.InventoryItem
import net.torvald.terrarum.gameitem.InventoryItemAdapter
import net.torvald.terrarum.realestate.RealEstateUtility import net.torvald.terrarum.realestate.RealEstateUtility
import org.dyn4j.geometry.Vector2 import org.dyn4j.geometry.Vector2
import org.lwjgl.input.Controller 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 get() = this is Player // FIXME true iff composed by PlayableActorDelegate
private val nullItem = object : InventoryItemAdapter() { private val nullItem = object : InventoryItem() {
override val itemID: Int = 0 override val id: Int = 0
override val equipPosition: Int = EquipPosition.NULL override val equipPosition: Int = EquipPosition.NULL
override var mass: Double = 0.0 override var mass: Double = 0.0
override var scale: Double = 1.0 override var scale: Double = 1.0
} }
override fun update(gc: GameContainer, delta: Int) { override fun update(gc: GameContainer, delta: Int) {
super.update(gc, delta) super.update(gc, delta)
@@ -163,6 +161,33 @@ open class ActorHumanoid(birth: GameDate, death: GameDate? = null)
isRightDown = false isRightDown = false
isJumpDown = 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) { private fun updateGamerControlBox(input: Input) {

View File

@@ -24,7 +24,7 @@ class ActorInventory() {
/** /**
* HashMap<ReferenceID, Amounts> * HashMap<ReferenceID, Amounts>
*/ */
private val itemList: HashMap<Int, Int> = HashMap() private val itemList: HashMap<InventoryItem, Int> = HashMap()
/** /**
* Default constructor with no encumbrance. * 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) = add(ItemPropCodex[itemID], count)
fun add(itemID: Int, count: Int = 1) { fun add(item: InventoryItem, count: Int = 1) {
if (itemID == Player.PLAYER_REF_ID) if (item.id == Player.PLAYER_REF_ID)
throw IllegalArgumentException("Attempted to put human player into the inventory.") throw IllegalArgumentException("Attempted to put human player into the inventory.")
if (Terrarum.ingame.playableActorDelegate != null && 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.") throw IllegalArgumentException("Attempted to put active player into the inventory.")
// If we already have the item, increment the amount // If we already have the item, increment the amount
// If not, add item with specified 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) = remove(ItemPropCodex[itemID], count)
fun remove(itemID: Int, count: Int = 1) { fun remove(item: InventoryItem, count: Int = 1) {
// check if the item does NOT exist // check if the item does NOT exist
if (itemList[itemID] == null) { if (itemList[item] == null) {
return return
} }
else { else {
// remove the existence of the item if count <= 0 // remove the existence of the item if count <= 0
if (itemList[itemID]!! - count <= 0) { if (itemList[item]!! - count <= 0) {
itemList.remove(itemID) itemList.remove(item)
} }
// else, decrement the item count // else, decrement the item count
else { else {
itemList.put(itemID, itemList[itemID]!! - count) itemList.put(item, itemList[item]!! - count)
} }
} }
} }
fun contains(item: InventoryItem) = itemList.containsKey(item.itemID) fun contains(item: InventoryItem) = itemList.containsKey(item)
fun contains(itemID: Int) = itemList.containsKey(itemID) 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 * Get capacity of inventory
@@ -112,7 +112,7 @@ class ActorInventory() {
* Get reference to the itemList * Get reference to the itemList
* @return * @return
*/ */
fun getItemList(): Map<Int, Int>? { fun getItemList(): Map<InventoryItem, Int>? {
return itemList return itemList
} }
@@ -121,16 +121,13 @@ class ActorInventory() {
* @return * @return
*/ */
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
fun getCopyOfItemList(): Map<Int, Int>? { fun getCopyOfItemList(): Map<InventoryItem, Int>? {
return itemList.clone() as Map<Int, Int> return itemList.clone() as Map<InventoryItem, Int>
} }
fun getTotalWeight(): Double { fun getTotalWeight(): Double {
var weight = 0.0 var weight = 0.0
itemList.forEach { item, i -> weight += item.mass * i }
for (item in itemList.entries) {
weight += ItemPropCodex.getProp(item.key).mass * item.value
}
return weight return weight
} }
@@ -140,10 +137,7 @@ class ActorInventory() {
*/ */
fun getTotalCount(): Int { fun getTotalCount(): Int {
var count = 0 var count = 0
itemList.forEach { item, i -> count += i }
for (item in itemList.entries) {
count += item.value
}
return count return count
} }
@@ -152,7 +146,7 @@ class ActorInventory() {
* Unique amount, multiple items are calculated as one * Unique amount, multiple items are calculated as one
*/ */
fun getTotalUniqueCount(): Int { fun getTotalUniqueCount(): Int {
return itemList.entries.size return itemList.size
} }
/** /**