DEADSOFTWARE

765224334d1d29b8441e0f59d1a9b0bd07df503e
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / model / item / InventoryItem.kt
1 package ru.deadsoftware.cavedroid.game.model.item
3 import com.badlogic.gdx.graphics.Color
4 import com.badlogic.gdx.graphics.g2d.SpriteBatch
5 import com.badlogic.gdx.graphics.glutils.ShapeRenderer
6 import ru.deadsoftware.cavedroid.game.GameItemsHolder
7 import ru.deadsoftware.cavedroid.misc.Assets
8 import ru.deadsoftware.cavedroid.misc.utils.drawSprite
9 import ru.deadsoftware.cavedroid.misc.utils.drawString
10 import ru.deadsoftware.cavedroid.misc.utils.px
11 import java.io.Serializable
13 class InventoryItem @JvmOverloads constructor(
14 val itemKey: String,
15 _amount: Int = 1,
16 ) : Serializable {
18 var amount = _amount
19 set(value) {
20 field = if (value < 0) {
21 0
22 } else {
23 value
24 }
25 }
27 @Transient
28 lateinit var item: Item
29 private set
31 @JvmOverloads
32 constructor(_item: Item, amount: Int = 1) : this(_item.params.key, amount) {
33 item = _item
34 }
36 fun init(gameItemsHolder: GameItemsHolder) {
37 if (this::item.isInitialized) {
38 return
39 }
40 item = gameItemsHolder.getItem(itemKey)
41 }
43 @JvmOverloads
44 fun add(count: Int = 1) {
45 if (count > 0 && Int.MAX_VALUE - count < amount) {
46 throw IllegalArgumentException("$amount + $count exceeds Int.MAX_VALUE")
47 }
49 amount += count
50 }
52 @JvmOverloads
53 fun subtract(count: Int = 1) {
54 if (count < 0) {
55 throw IllegalArgumentException("Can't subtract negative amount")
56 }
58 add(-count)
59 }
61 @JvmOverloads
62 fun canBeAdded(count: Int = 1): Boolean {
63 return amount + count <= item.params.maxStack
64 }
66 private fun drawAmountText(spriteBatch: SpriteBatch, text: String, x: Float, y: Float) {
67 spriteBatch.drawString(text, x + 1, y + 1, Color.BLACK)
68 spriteBatch.drawString(text, x, y, Color.WHITE)
69 }
71 fun drawSelected(spriteBatch: SpriteBatch, x: Float, y: Float) {
72 if (item.isNone()) {
73 return
74 }
76 val sprite = item.sprite
77 val amountString = amount.toString()
78 spriteBatch.drawSprite(sprite, x - 10f, y - 10f, rotation = 0f, width = 20f, height = 20f)
79 drawAmountText(
80 spriteBatch = spriteBatch,
81 text = amountString,
82 x = x + 10f - Assets.getStringWidth(amountString) + 1f,
83 y = y + 10f - Assets.getStringHeight(amountString) + 1f
84 )
85 }
87 fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, x: Float, y: Float) {
88 if (item.isNone()) {
89 return
90 }
92 val sprite = item.sprite
93 spriteBatch.drawSprite(sprite, x, y)
95 if (amount < 2) {
96 return
97 }
99 if (item.isTool()) {
100 spriteBatch.end()
101 shapeRenderer.begin(ShapeRenderer.ShapeType.Filled)
102 shapeRenderer.color = Color.GREEN
103 shapeRenderer.rect(
104 /* x = */ x,
105 /* y = */ y + 1.px - 2,
106 /* width = */ 1.px * (amount.toFloat() / item.params.maxStack.toFloat()),
107 /* height = */ 2f
109 shapeRenderer.end()
110 spriteBatch.begin()
111 } else {
112 val amountString = amount.toString()
113 drawAmountText(
114 spriteBatch = spriteBatch,
115 text = amountString,
116 x = x + 1.px - Assets.getStringWidth(amountString),
117 y = y + 1.px - Assets.getStringHeight(amountString)