[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 acc0d2931efd026c4cfa3dcb9cba8b6e77322ded..3cd406b731f5e2e0dc8a128926a4baef24d5a246 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
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,
}
@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
}
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
}
companion object {
- fun InventoryItem?.isNoneOrNull() = this?.item == null || this.item.isNone()
+
+ @OptIn(ExperimentalContracts::class)
+ fun InventoryItem?.isNoneOrNull(): Boolean {
+ contract { returns(false) implies(this@isNoneOrNull != null) }
+ return this?.item == null || this.item.isNone()
+ }
}
}