DEADSOFTWARE

Add survival inventory
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / render / windows / CreativeWindowRenderer.kt
1 package ru.deadsoftware.cavedroid.game.render.windows
3 import com.badlogic.gdx.graphics.g2d.SpriteBatch
4 import com.badlogic.gdx.graphics.glutils.ShapeRenderer
5 import com.badlogic.gdx.math.Rectangle
6 import ru.deadsoftware.cavedroid.MainConfig
7 import ru.deadsoftware.cavedroid.game.GameInput
8 import ru.deadsoftware.cavedroid.game.GameItemsHolder
9 import ru.deadsoftware.cavedroid.game.GameScope
10 import ru.deadsoftware.cavedroid.game.mobs.MobsController
11 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
12 import ru.deadsoftware.cavedroid.game.render.IGameRenderer
13 import ru.deadsoftware.cavedroid.game.render.WindowsRenderer
14 import ru.deadsoftware.cavedroid.misc.Assets
15 import javax.inject.Inject
16 import kotlin.math.min
18 @GameScope
19 class CreativeWindowRenderer @Inject constructor(
20 private val mainConfig: MainConfig,
21 private val gameInput: GameInput,
22 private val gameItemsHolder: GameItemsHolder,
23 private val mobsController: MobsController,
24 ) : AbstractWindowRenderer(), IGameRenderer {
26 override val renderLayer get() = WindowsRenderer.RENDER_LAYER
28 private val creativeWindowTexture get() = requireNotNull(Assets.textureRegions[CREATIVE_WINDOW_KEY])
29 private val scrollIndicatorTexture get() = requireNotNull(Assets.textureRegions[SCROLL_INDICATOR_KEY])
32 override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) {
33 val creativeWindow = creativeWindowTexture
35 val windowX = viewport.width / 2 - creativeWindow.regionWidth / 2
36 val windowY = viewport.height / 2 - creativeWindow.regionHeight / 2
37 val oneScrollAmount = CreativeWindowConfig.scrollIndicatorFullHeight / gameItemsHolder.getCreativeScrollAmount()
39 spriteBatch.draw(creativeWindow, windowX, windowY)
40 spriteBatch.draw(
41 /* region = */ scrollIndicatorTexture,
42 /* x = */ windowX + CreativeWindowConfig.scrollIndicatorMarginLeft,
43 /* y = */ windowY + CreativeWindowConfig.scrollIndicatorMarginTop
44 + (gameInput.creativeScroll * oneScrollAmount)
45 )
47 val allItems = gameItemsHolder.getAllItems()
48 val startIndex = gameInput.creativeScroll * CreativeWindowConfig.itemsInRow
49 val endIndex = min(startIndex + CreativeWindowConfig.itemsOnPage, allItems.size)
50 val items = sequence {
51 for (i in startIndex..<endIndex) {
52 yield(allItems.elementAt(i))
53 }
54 }
56 drawItemsGrid(
57 spriteBatch = spriteBatch,
58 shapeRenderer = shapeRenderer,
59 gridX = windowX + CreativeWindowConfig.itemsGridMarginLeft,
60 gridY = windowY + CreativeWindowConfig.itemsGridMarginTop,
61 items = items.asIterable(),
62 itemsInRow = CreativeWindowConfig.itemsInRow,
63 cellWidth = CreativeWindowConfig.itemsGridColWidth,
64 cellHeight = CreativeWindowConfig.itemsGridRowHeight,
65 )
67 drawItemsGrid(
68 spriteBatch = spriteBatch,
69 shapeRenderer = shapeRenderer,
70 gridX = windowX + CreativeWindowConfig.itemsGridMarginLeft,
71 gridY = windowY + creativeWindow.regionHeight - CreativeWindowConfig.playerInventoryOffsetFromBottom,
72 items = mobsController.player.inventory.asSequence().take(CreativeWindowConfig.invItems).asIterable(),
73 itemsInRow = CreativeWindowConfig.invItems,
74 cellWidth = CreativeWindowConfig.itemsGridColWidth,
75 cellHeight = CreativeWindowConfig.itemsGridRowHeight,
76 )
77 }
79 companion object {
80 private const val CREATIVE_WINDOW_KEY = "creative"
81 private const val SCROLL_INDICATOR_KEY = "handle"
83 private data object CreativeWindowConfig {
84 const val scrollIndicatorMarginLeft = 156f
85 const val scrollIndicatorMarginTop = 18f
86 const val scrollIndicatorFullHeight = 72f
88 const val itemsGridMarginLeft = 8f
89 const val itemsGridMarginTop = 18f
91 const val itemsGridRowHeight = 18f
92 const val itemsGridColWidth = 18f
94 const val itemsInRow = 8
95 const val itemsInCol = 5
97 const val invItems = 9
99 const val playerInventoryOffsetFromBottom = 24f
101 val itemsOnPage get() = itemsInCol * itemsInRow