DEADSOFTWARE

Add chest
[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.game.mobs.player.Inventory
8 import ru.deadsoftware.cavedroid.misc.Assets
9 import ru.deadsoftware.cavedroid.misc.utils.drawSprite
10 import ru.deadsoftware.cavedroid.misc.utils.drawString
11 import ru.deadsoftware.cavedroid.misc.utils.px
12 import java.io.Serializable
14 class InventoryItem @JvmOverloads constructor(
15 val itemKey: String,
16 _amount: Int = 1,
17 ) : Serializable {
19 var amount = _amount
20 set(value) {
21 field = if (value < 0) {
22 0
23 } else {
24 value
25 }
26 }
28 @Transient
29 lateinit var item: Item
30 private set
32 @JvmOverloads
33 constructor(_item: Item, amount: Int = 1) : this(_item.params.key, amount) {
34 item = _item
35 }
37 fun init(gameItemsHolder: GameItemsHolder) {
38 if (this::item.isInitialized) {
39 return
40 }
41 item = gameItemsHolder.getItem(itemKey)
42 }
44 @JvmOverloads
45 fun add(count: Int = 1) {
46 if (count > 0 && Int.MAX_VALUE - count < amount) {
47 throw IllegalArgumentException("$amount + $count exceeds Int.MAX_VALUE")
48 }
50 amount += count
51 }
53 @JvmOverloads
54 fun subtract(count: Int = 1) {
55 if (count < 0) {
56 throw IllegalArgumentException("Can't subtract negative amount")
57 }
59 add(-count)
60 }
62 @JvmOverloads
63 fun canBeAdded(count: Int = 1): Boolean {
64 return amount + count <= item.params.maxStack
65 }
67 private fun drawAmountText(spriteBatch: SpriteBatch, text: String, x: Float, y: Float) {
68 spriteBatch.drawString(text, x + 1, y + 1, Color.BLACK)
69 spriteBatch.drawString(text, x, y, Color.WHITE)
70 }
72 fun drawSelected(spriteBatch: SpriteBatch, x: Float, y: Float) {
73 if (item.isNone()) {
74 return
75 }
77 val sprite = item.sprite
78 val amountString = amount.toString()
79 spriteBatch.drawSprite(sprite, x - 10f, y - 10f, rotation = 0f, width = 20f, height = 20f)
80 drawAmountText(
81 spriteBatch = spriteBatch,
82 text = amountString,
83 x = x + 10f - Assets.getStringWidth(amountString) + 1f,
84 y = y + 10f - Assets.getStringHeight(amountString) + 1f
85 )
86 }
88 fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, x: Float, y: Float) {
89 if (item.isNone()) {
90 return
91 }
93 val sprite = item.sprite
94 val placeableMarginTop = (item as? Item.Placeable)?.block?.params?.spriteMargins?.top ?: 0
95 val placeableMarginLeft = (item as? Item.Placeable)?.block?.params?.spriteMargins?.left ?: 0
96 spriteBatch.drawSprite(sprite, x + placeableMarginLeft, y + placeableMarginTop)
98 if (amount < 2) {
99 return
102 if (item.isTool()) {
103 spriteBatch.end()
104 shapeRenderer.begin(ShapeRenderer.ShapeType.Filled)
105 shapeRenderer.color = Color.GREEN
106 shapeRenderer.rect(
107 /* x = */ x,
108 /* y = */ y + 1.px - 2,
109 /* width = */ 1.px * (amount.toFloat() / item.params.maxStack.toFloat()),
110 /* height = */ 2f
112 shapeRenderer.end()
113 spriteBatch.begin()
114 } else {
115 val amountString = amount.toString()
116 drawAmountText(
117 spriteBatch = spriteBatch,
118 text = amountString,
119 x = x + 1.px - Assets.getStringWidth(amountString),
120 y = y + 1.px - Assets.getStringHeight(amountString)
125 companion object {
126 fun InventoryItem?.isNoneOrNull() = this?.item == null || this.item.isNone()