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 b41ce50a0f
commit 2089ce4ee2
3 changed files with 54 additions and 34 deletions

View File

@@ -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)) {

View File

@@ -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) {

View File

@@ -24,7 +24,7 @@ class ActorInventory() {
/**
* HashMap<ReferenceID, Amounts>
*/
private val itemList: HashMap<Int, Int> = HashMap()
private val itemList: HashMap<InventoryItem, Int> = 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<Int, Int>? {
fun getItemList(): Map<InventoryItem, Int>? {
return itemList
}
@@ -121,16 +121,13 @@ class ActorInventory() {
* @return
*/
@Suppress("UNCHECKED_CAST")
fun getCopyOfItemList(): Map<Int, Int>? {
return itemList.clone() as Map<Int, Int>
fun getCopyOfItemList(): Map<InventoryItem, Int>? {
return itemList.clone() as Map<InventoryItem, Int>
}
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
}
/**