X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fmodel%2Fitem%2FInventoryItem.kt;h=3cd406b731f5e2e0dc8a128926a4baef24d5a246;hb=1e285247085ba04351feb486a0be6aa577f43093;hp=6cb2a9d85bf5232fb8e7b37750c336f7e0aa0f38;hpb=f34df6e5eb1052467333f1461c67e75b21eb0d95;p=cavedroid.git 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 6cb2a9d..3cd406b 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt @@ -9,26 +9,68 @@ import ru.deadsoftware.cavedroid.misc.utils.drawSprite import ru.deadsoftware.cavedroid.misc.utils.drawString import ru.deadsoftware.cavedroid.misc.utils.px import java.io.Serializable +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.contract 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 + private var _item: Item? = null + + var item: Item + get() { + requireNotNull(_item) { "_item is null" } + return _item.takeIf { amount > 0 } ?: throw IllegalArgumentException("Accessing item with zero amount") + } + private set (value) { + _item = value + } @JvmOverloads - constructor(_item: Item, amount: Int = 1) : this(_item.params.key, amount) { - item = _item + constructor(item: Item, amount: Int = 1) : this(item.params.key, amount) { + _item = item } fun init(gameItemsHolder: GameItemsHolder) { - if (this::item.isInitialized) { + if (_item != null) { return } - item = gameItemsHolder.getItem(itemKey) + _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) { @@ -42,11 +84,14 @@ class InventoryItem @JvmOverloads constructor( } val sprite = item.sprite - sprite.setOriginCenter() - sprite.setPosition(x, y) - sprite.setScale(1.25f) - sprite.draw(spriteBatch) - sprite.setScale(1f) + val amountString = amount.toString() + spriteBatch.drawSprite(sprite, x - 10f, y - 10f, rotation = 0f, width = 20f, height = 20f) + drawAmountText( + spriteBatch = spriteBatch, + text = amountString, + x = x + 10f - Assets.getStringWidth(amountString) + 1f, + y = y + 10f - Assets.getStringHeight(amountString) + 1f + ) } fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, x: Float, y: Float) { @@ -55,7 +100,9 @@ class InventoryItem @JvmOverloads constructor( } val sprite = item.sprite - spriteBatch.drawSprite(sprite, x, y) + val placeableMarginTop = (item as? Item.Placeable)?.block?.params?.spriteMargins?.top ?: 0 + val placeableMarginLeft = (item as? Item.Placeable)?.block?.params?.spriteMargins?.left ?: 0 + spriteBatch.drawSprite(sprite, x + placeableMarginLeft, y + placeableMarginTop) if (amount < 2) { return @@ -84,4 +131,12 @@ class InventoryItem @JvmOverloads constructor( } } + companion object { + + @OptIn(ExperimentalContracts::class) + fun InventoryItem?.isNoneOrNull(): Boolean { + contract { returns(false) implies(this@isNoneOrNull != null) } + return this?.item == null || this.item.isNone() + } + } }