DEADSOFTWARE

Bug fixes
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / mobs / player / Inventory.kt
index 44ee6dd17e6bb94393475dc22c2778f4a2569338..d99a90ec60e8762c5e6b0c16f4fdfeccb02b361c 100644 (file)
@@ -47,23 +47,27 @@ class Inventory(
         tooltipManager.showHotbarTooltip(activeItem.item.params.name)
     }
 
-    val activeItem get() = items[activeSlot]
+    val activeItem get() = _items[activeSlot]
 
     fun initItems(gameItemsHolder: GameItemsHolder, tooltipManager: TooltipManager) {
         this.tooltipManager = tooltipManager
         fallbackItem = gameItemsHolder.fallbackItem.toInventoryItem()
-        items.forEach { item ->
+        _items.forEach { item ->
             item.init(gameItemsHolder)
         }
     }
 
     private fun getItemPickSlot(item: Item): Int {
-        for (i in items.indices) {
-            val inventoryItem = items[i]
+        for (i in _items.indices) {
+            val inventoryItem = _items[i]
 
             if (item == inventoryItem.item && inventoryItem.canBeAdded()) {
                 return i
             }
+        }
+
+        for (i in _items.indices) {
+            val inventoryItem = _items[i]
 
             if (inventoryItem.item.isNone()) {
                 return i
@@ -79,12 +83,12 @@ class Inventory(
 
     fun pickDrop(drop: Drop) {
         val slot = getItemPickSlot(drop.item).takeIf { it >= 0 } ?: return
-        val inventoryItem = items[slot]
+        val inventoryItem = _items[slot]
 
         if (inventoryItem.item == drop.item) {
-            inventoryItem.add()
+            inventoryItem.add(drop.amount)
         } else {
-            _items[slot] = drop.item.toInventoryItem()
+            _items[slot] = drop.item.toInventoryItem(drop.amount)
             if (slot == activeSlot) {
                 showCurrentItemTooltip()
             }
@@ -106,10 +110,16 @@ class Inventory(
     }
 
     @JvmOverloads
-    fun decreaseCurrentItemAmount(count: Int = 1) {
-        activeItem.subtract(count)
-        if (activeItem.amount <= 0) {
-            _items[activeSlot] = fallbackItem
+    fun decreaseItemAmount(slot: Int, count: Int = 1) {
+        val item = _items[slot]
+        item.subtract(count)
+        if (item.amount <= 0) {
+            _items[slot] = fallbackItem
         }
     }
+
+    @JvmOverloads
+    fun decreaseCurrentItemAmount(count: Int = 1) {
+        decreaseItemAmount(activeSlot, count)
+    }
 }
\ No newline at end of file