DEADSOFTWARE

Fix drop picking
authorfredboy <fredboy@protonmail.com>
Fri, 10 May 2024 17:16:29 +0000 (00:16 +0700)
committerfredboy <fredboy@protonmail.com>
Fri, 10 May 2024 17:16:29 +0000 (00:16 +0700)
core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt
core/src/ru/deadsoftware/cavedroid/game/objects/drop/Drop.kt

index c6dea8bc878f0d523be0fc447979961080fc94df..3012d6675a3346bed978a5183b342120d31ab0b9 100644 (file)
@@ -65,7 +65,7 @@ class Inventory(
         for (i in _items.indices) {
             val inventoryItem = _items[i]
 
-            if (item == inventoryItem.item && inventoryItem.canBeAdded(drop.amount)) {
+            if (item == inventoryItem.item && inventoryItem.canBeAdded()) {
                 return i
             }
         }
@@ -90,15 +90,22 @@ class Inventory(
         val inventoryItem = _items[slot]
 
         if (inventoryItem.item == drop.item) {
-            inventoryItem.add(drop.amount)
+            if (inventoryItem.canBeAdded(drop.amount)) {
+                inventoryItem.add(drop.amount)
+                drop.pickedUp = true
+            } else {
+                val addCount = inventoryItem.item.params.maxStack - inventoryItem.amount
+                inventoryItem.add(addCount)
+                drop.subtract(addCount)
+                pickDrop(drop)
+            }
         } else {
             _items[slot] = drop.item.toInventoryItem(drop.amount)
             if (slot == activeSlot) {
                 showCurrentItemTooltip()
             }
+            drop.pickedUp = true
         }
-
-        drop.pickedUp = true
     }
 
     fun addItem(item: Item) {
index 9099864b39f8c12db0f2362d41a2eecd2739d393..edb3c81ff657a63fd41b865e4dbaea9a7dc01230 100644 (file)
@@ -10,9 +10,12 @@ class Drop @JvmOverloads constructor(
     x: Float,
     y: Float,
     _item: Item,
-    val amount: Int = 1,
+    _amount: Int = 1,
 ) : Rectangle(x, y, DROP_SIZE, DROP_SIZE) {
 
+    var amount: Int = _amount
+        private set
+
     val itemKey = _item.params.key
     val velocity = getInitialVelocity()
     var pickedUp = false
@@ -39,6 +42,15 @@ class Drop @JvmOverloads constructor(
         return Intersector.overlaps(magnetArea, rectangle)
     }
 
+    @JvmOverloads
+    fun subtract(count: Int = 1) {
+        if (count < 0) {
+            throw IllegalArgumentException("Can't subtract negative amount")
+        }
+
+        amount -= count
+    }
+
     private fun getMagnetArea(): Rectangle {
         return Rectangle(
             /* x = */ x - MAGNET_DISTANCE,