From 4f339b3a792cf0b066acc9e630090d6a1389df0b Mon Sep 17 00:00:00 2001 From: fredboy Date: Sat, 11 May 2024 00:16:29 +0700 Subject: [PATCH] Fix drop picking --- .../cavedroid/game/mobs/player/Inventory.kt | 15 +++++++++++---- .../cavedroid/game/objects/drop/Drop.kt | 14 +++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt index c6dea8b..3012d66 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt @@ -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) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/drop/Drop.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/drop/Drop.kt index 9099864..edb3c81 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/drop/Drop.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/drop/Drop.kt @@ -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, -- 2.29.2