DEADSOFTWARE

9d5295c2f7f822d6065e0f4492a6087c96a263b8
[cavedroid.git] /
1 package ru.fredboy.cavedroid.domain.items.model.inventory
2
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
13
14 class InventoryItem(
15 val item: Item,
16 _amount: Int = 1,
17 ) {
18
19 var amount = _amount
20 set(value) {
21 field = if (value < 0) {
22 0
23 } else {
24 value
25 }
26 }
27
28 fun add(count: Int = 1) {
29 if (count > 0 && Int.MAX_VALUE - count < amount) {
30 throw IllegalArgumentException("$amount + $count exceeds Int.MAX_VALUE")
31 }
32
33 amount += count
34 }
35
36 fun subtract(count: Int = 1) {
37 if (count < 0) {
38 throw IllegalArgumentException("Can't subtract negative amount")
39 }
40
41 add(-count)
42 }
43
44 fun canBeAdded(count: Int = 1): Boolean {
45 return amount + count <= item.params.maxStack
46 }
47
48 private fun drawAmountText(
49 spriteBatch: SpriteBatch,
50 font: BitmapFont,
51 text: String,
52 x: Float,
53 y: Float
54 ) {
55 spriteBatch.drawString(font, text, x + 1, y + 1, Color.BLACK)
56 spriteBatch.drawString(font, text, x, y, Color.WHITE)
57 }
58
59 fun drawSelected(
60 spriteBatch: SpriteBatch,
61 font: BitmapFont,
62 x: Float,
63 y: Float,
64 getStringWidth: (String) -> Float,
65 getStringHeight: (String) -> Float,
66 ) {
67 if (item.isNone()) {
68 return
69 }
70
71 val sprite = item.sprite
72 val amountString = amount.toString()
73 spriteBatch.drawSprite(sprite, x - 10f, y - 10f, rotation = 0f, width = 20f, height = 20f)
74 drawAmountText(
75 spriteBatch = spriteBatch,
76 font = font,
77 text = amountString,
78 x = x + 10f - getStringWidth(amountString) + 1f,
79 y = y + 10f - getStringHeight(amountString) + 1f
80 )
81 }
82
83 fun draw(
84 spriteBatch: SpriteBatch,
85 shapeRenderer: ShapeRenderer,
86 font: BitmapFont,
87 x: Float,
88 y: Float,
89 getStringWidth: (String) -> Float,
90 getStringHeight: (String) -> Float,
91 ) {
92 if (item.isNone()) {
93 return
94 }
95
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)
100
101 if (amount < 2) {
102 return
103 }
104
105 if (item.isTool()) {
106 spriteBatch.end()
107 shapeRenderer.begin(ShapeRenderer.ShapeType.Filled)
108 shapeRenderer.color = Color.GREEN
109 shapeRenderer.rect(
110 /* x = */ x,
111 /* y = */ y + 1.px - 2,
112 /* width = */ 1.px * (amount.toFloat() / item.params.maxStack.toFloat()),
113 /* height = */ 2f
114 )
115 shapeRenderer.end()
116 spriteBatch.begin()
117 } else {
118 val amountString = amount.toString()
119 drawAmountText(
120 spriteBatch = spriteBatch,
121 font = font,
122 text = amountString,
123 x = x + 1.px - getStringWidth(amountString),
124 y = y + 1.px - getStringHeight(amountString)
125 )
126 }
127 }
128
129 companion object {
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()
134 }
135 }
136
137 }