DEADSOFTWARE

CaveGame in kotlin
[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
12 import kotlin.contracts.ExperimentalContracts
13 import kotlin.contracts.contract
15 class InventoryItem @JvmOverloads constructor(
16 val itemKey: String,
17 _amount: Int = 1,
18 ) : Serializable {
20 var amount = _amount
21 set(value) {
22 field = if (value < 0) {
23 0
24 } else {
25 value
26 }
27 }
29 @Transient
30 private var _item: Item? = null
32 var item: Item
33 get() {
34 requireNotNull(_item) { "_item is null" }
35 return _item.takeIf { amount > 0 } ?: throw IllegalArgumentException("Accessing item with zero amount")
36 }
37 private set (value) {
38 _item = value
39 }
41 @JvmOverloads
42 constructor(item: Item, amount: Int = 1) : this(item.params.key, amount) {
43 _item = item
44 }
46 fun init(gameItemsHolder: GameItemsHolder) {
47 if (_item != null) {
48 return
49 }
50 _item = gameItemsHolder.getItem(itemKey)
51 }
53 @JvmOverloads
54 fun add(count: Int = 1) {
55 if (count > 0 && Int.MAX_VALUE - count < amount) {
56 throw IllegalArgumentException("$amount + $count exceeds Int.MAX_VALUE")
57 }
59 amount += count
60 }
62 @JvmOverloads
63 fun subtract(count: Int = 1) {
64 if (count < 0) {
65 throw IllegalArgumentException("Can't subtract negative amount")
66 }
68 add(-count)
69 }
71 @JvmOverloads
72 fun canBeAdded(count: Int = 1): Boolean {
73 return amount + count <= item.params.maxStack
74 }
76 private fun drawAmountText(spriteBatch: SpriteBatch, text: String, x: Float, y: Float) {
77 spriteBatch.drawString(text, x + 1, y + 1, Color.BLACK)
78 spriteBatch.drawString(text, x, y, Color.WHITE)
79 }
81 fun drawSelected(spriteBatch: SpriteBatch, x: Float, y: Float) {
82 if (item.isNone()) {
83 return
84 }
86 val sprite = item.sprite
87 val amountString = amount.toString()
88 spriteBatch.drawSprite(sprite, x - 10f, y - 10f, rotation = 0f, width = 20f, height = 20f)
89 drawAmountText(
90 spriteBatch = spriteBatch,
91 text = amountString,
92 x = x + 10f - Assets.getStringWidth(amountString) + 1f,
93 y = y + 10f - Assets.getStringHeight(amountString) + 1f
94 )
95 }
97 fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, x: Float, y: Float) {
98 if (item.isNone()) {
99 return
102 val sprite = item.sprite
103 val placeableMarginTop = (item as? Item.Placeable)?.block?.params?.spriteMargins?.top ?: 0
104 val placeableMarginLeft = (item as? Item.Placeable)?.block?.params?.spriteMargins?.left ?: 0
105 spriteBatch.drawSprite(sprite, x + placeableMarginLeft, y + placeableMarginTop)
107 if (amount < 2) {
108 return
111 if (item.isTool()) {
112 spriteBatch.end()
113 shapeRenderer.begin(ShapeRenderer.ShapeType.Filled)
114 shapeRenderer.color = Color.GREEN
115 shapeRenderer.rect(
116 /* x = */ x,
117 /* y = */ y + 1.px - 2,
118 /* width = */ 1.px * (amount.toFloat() / item.params.maxStack.toFloat()),
119 /* height = */ 2f
121 shapeRenderer.end()
122 spriteBatch.begin()
123 } else {
124 val amountString = amount.toString()
125 drawAmountText(
126 spriteBatch = spriteBatch,
127 text = amountString,
128 x = x + 1.px - Assets.getStringWidth(amountString),
129 y = y + 1.px - Assets.getStringHeight(amountString)
134 companion object {
136 @OptIn(ExperimentalContracts::class)
137 fun InventoryItem?.isNoneOrNull(): Boolean {
138 contract { returns(false) implies(this@isNoneOrNull != null) }
139 return this?.item == null || this.item.isNone()