X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;ds=sidebyside;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fmodel%2Fitem%2FInventoryItem.kt;h=89186595d360ee3f2642306baefeb893ab94e223;hb=dfe9ff3e9253c250f62ee029411f773916948601;hp=1e1f43eeb85ad707d4851b045aa25c6ec4de46bc;hpb=289536374d18bb05cde615c04d9fe576d6ac26bc;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 1e1f43e..8918659 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt @@ -1,11 +1,17 @@ package ru.deadsoftware.cavedroid.game.model.item +import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import com.badlogic.gdx.graphics.glutils.ShapeRenderer import ru.deadsoftware.cavedroid.game.GameItemsHolder +import ru.deadsoftware.cavedroid.misc.Assets +import ru.deadsoftware.cavedroid.misc.utils.drawString +import ru.deadsoftware.cavedroid.misc.utils.px import java.io.Serializable class InventoryItem @JvmOverloads constructor( val itemKey: String, - val amount: Int = 1, + var amount: Int = 1, ) : Serializable { @Transient @@ -24,4 +30,57 @@ class InventoryItem @JvmOverloads constructor( item = gameItemsHolder.getItem(itemKey) } + 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) + } + + fun drawSelected(spriteBatch: SpriteBatch, x: Float, y: Float) { + if (item.isNone()) { + return + } + + val sprite = item.sprite + sprite.setOriginCenter() + sprite.setPosition(x, y) + sprite.setScale(1.25f) + sprite.draw(spriteBatch) + sprite.setScale(1f) + } + + fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, x: Float, y: Float) { + if (item.isNone()) { + return + } + + val sprite = item.sprite + spriteBatch.draw(sprite, x, y) + + if (amount < 2) { + return + } + + if (item.isTool()) { + spriteBatch.end() + shapeRenderer.begin(ShapeRenderer.ShapeType.Filled) + shapeRenderer.color = Color.GREEN + shapeRenderer.rect( + /* x = */ x, + /* y = */ y + 1.px - 2, + /* width = */ 1.px * (amount.toFloat() / item.params.maxStack.toFloat()), + /* height = */ 2f + ) + shapeRenderer.end() + spriteBatch.begin() + } else { + val amountString = amount.toString() + drawAmountText( + spriteBatch = spriteBatch, + text = amountString, + x = x + 1.px - Assets.getStringWidth(amountString), + y = y + 1.px - Assets.getStringHeight(amountString) + ) + } + } + }