[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / model / item / InventoryItem.kt
diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt b/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt
index 6c1598315cb5037813c8a385ddb06f5f062846ea..acc0d2931efd026c4cfa3dcb9cba8b6e77322ded 100644 (file)
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import ru.deadsoftware.cavedroid.game.GameItemsHolder
+import ru.deadsoftware.cavedroid.game.mobs.player.Inventory
import ru.deadsoftware.cavedroid.misc.Assets
import ru.deadsoftware.cavedroid.misc.utils.drawSprite
import ru.deadsoftware.cavedroid.misc.utils.drawString
class InventoryItem @JvmOverloads constructor(
val itemKey: String,
- var amount: Int = 1,
+ _amount: Int = 1,
) : Serializable {
+ var amount = _amount
+ set(value) {
+ field = if (value < 0) {
+ 0
+ } else {
+ value
+ }
+ }
+
@Transient
lateinit var item: Item
private set
item = gameItemsHolder.getItem(itemKey)
}
+ @JvmOverloads
+ fun add(count: Int = 1) {
+ if (count > 0 && Int.MAX_VALUE - count < amount) {
+ throw IllegalArgumentException("$amount + $count exceeds Int.MAX_VALUE")
+ }
+
+ amount += count
+ }
+
+ @JvmOverloads
+ fun subtract(count: Int = 1) {
+ if (count < 0) {
+ throw IllegalArgumentException("Can't subtract negative amount")
+ }
+
+ add(-count)
+ }
+
+ @JvmOverloads
+ fun canBeAdded(count: Int = 1): Boolean {
+ return amount + count <= item.params.maxStack
+ }
+
private fun drawAmountText(spriteBatch: SpriteBatch, text: String, x: Float, y: Float) {
spriteBatch.drawString(text, x + 1, y + 1, Color.BLACK)
spriteBatch.drawString(text, x, y, Color.WHITE)
}
}
+ companion object {
+ fun InventoryItem?.isNoneOrNull() = this?.item == null || this.item.isNone()
+ }
}