1 package ru.fredboy.cavedroid.domain.items.model.inventory
3 import com.badlogic.gdx.graphics.Color
4 import com.badlogic.gdx.graphics.g2d.BitmapFont
5 import com.badlogic.gdx.graphics.g2d.SpriteBatch
6 import com.badlogic.gdx.graphics.glutils.ShapeRenderer
7 import ru.fredboy.cavedroid.common.utils.drawSprite
8 import ru.fredboy.cavedroid.common.utils.drawString
9 import ru.fredboy.cavedroid.common.utils.px
10 import ru.fredboy.cavedroid.domain.items.model.item.Item
11 import kotlin.contracts.ExperimentalContracts
12 import kotlin.contracts.contract
21 field = if (value < 0) {
28 fun add(count: Int = 1) {
29 if (count > 0 && Int.MAX_VALUE - count < amount) {
30 throw IllegalArgumentException("$amount + $count exceeds Int.MAX_VALUE")
36 fun subtract(count: Int = 1) {
38 throw IllegalArgumentException("Can't subtract negative amount")
44 fun canBeAdded(count: Int = 1): Boolean {
45 return amount + count <= item.params.maxStack
48 private fun drawAmountText(
49 spriteBatch: SpriteBatch,
55 spriteBatch.drawString(font, text, x + 1, y + 1, Color.BLACK)
56 spriteBatch.drawString(font, text, x, y, Color.WHITE)
60 spriteBatch: SpriteBatch,
64 getStringWidth: (String) -> Float,
65 getStringHeight: (String) -> Float,
71 val sprite = item.sprite
72 val amountString = amount.toString()
73 spriteBatch.drawSprite(sprite, x - 10f, y - 10f, rotation = 0f, width = 20f, height = 20f)
75 spriteBatch = spriteBatch,
78 x = x + 10f - getStringWidth(amountString) + 1f,
79 y = y + 10f - getStringHeight(amountString) + 1f
84 spriteBatch: SpriteBatch,
85 shapeRenderer: ShapeRenderer,
89 getStringWidth: (String) -> Float,
90 getStringHeight: (String) -> Float,
96 val sprite = item.sprite
97 val placeableMarginTop = (item as? Item.Placeable)?.block?.params?.spriteMargins?.top ?: 0
98 val placeableMarginLeft = (item as? Item.Placeable)?.block?.params?.spriteMargins?.left ?: 0
99 spriteBatch.drawSprite(sprite, x + placeableMarginLeft, y + placeableMarginTop)
107 shapeRenderer.begin(ShapeRenderer.ShapeType.Filled)
108 shapeRenderer.color = Color.GREEN
111 /* y = */ y + 1.px - 2,
112 /* width = */ 1.px * (amount.toFloat() / item.params.maxStack.toFloat()),
118 val amountString = amount.toString()
120 spriteBatch = spriteBatch,
123 x = x + 1.px - getStringWidth(amountString),
124 y = y + 1.px - getStringHeight(amountString)
130 @OptIn(ExperimentalContracts::class)
131 fun InventoryItem?.isNoneOrNull(): Boolean {
132 contract { returns(false) implies(this@isNoneOrNull != null) }
133 return this?.item == null || this.item.isNone()