DEADSOFTWARE

Add items count and tools duration
[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
17 @GameScope
18 class CreativeWindowRenderer @Inject constructor(
19 private val mainConfig: MainConfig,
20 private val gameInput: GameInput,
21 private val gameItemsHolder: GameItemsHolder,
22 private val mobsController: MobsController,
23 ) : IGameRenderer {
25 override val renderLayer get() = WindowsRenderer.RENDER_LAYER
27 private val creativeWindowTexture get() = requireNotNull(Assets.textureRegions[CREATIVE_WINDOW_KEY])
28 private val scrollIndicatorTexture get() = requireNotNull(Assets.textureRegions[SCROLL_INDICATOR_KEY])
30 private fun drawItemsGrid(spriteBatch: SpriteBatch, gridX: Float, gridY: Float) {
31 val allItems = gameItemsHolder.getAllItems()
32 val startIndex = gameInput.creativeScroll * CreativeWindowConfig.itemsInRow
33 val endIndex = startIndex + CreativeWindowConfig.itemsOnPage
35 for (i in startIndex..<endIndex) {
36 if (i !in allItems.indices) {
37 break
38 }
39 val item = allItems.elementAt(i)
41 if (item.isNone()) {
42 continue
43 }
45 val gridIndex = i - startIndex
47 val itemX = gridX + (gridIndex % CreativeWindowConfig.itemsInRow) * CreativeWindowConfig.itemsGridColWidth
48 val itemY = gridY + (gridIndex / CreativeWindowConfig.itemsInRow) * CreativeWindowConfig.itemsGridRowHeight
50 spriteBatch.draw(item.sprite, itemX, itemY)
51 }
52 }
54 private fun drawPlayerInventory(
55 spriteBatch: SpriteBatch,
56 shapeRenderer: ShapeRenderer,
57 inventoryX: Float,
58 inventoryY: Float
59 ) {
60 mobsController.player.inventory.asSequence()
61 .forEachIndexed { index, item ->
62 if (item.item.isNone()) {
63 return@forEachIndexed
64 }
66 val itemX = inventoryX + index * CreativeWindowConfig.itemsGridColWidth
68 item.draw(spriteBatch, shapeRenderer, itemX, inventoryY)
69 }
70 }
72 private fun drawCreative(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle) {
73 val creativeWindow = creativeWindowTexture
75 val windowX = viewport.width / 2 - creativeWindow.regionWidth / 2
76 val windowY = viewport.height / 2 - creativeWindow.regionHeight / 2
77 val oneScrollAmount = CreativeWindowConfig.scrollIndicatorFullHeight / gameItemsHolder.getCreativeScrollAmount()
79 spriteBatch.draw(creativeWindow, windowX, windowY)
80 spriteBatch.draw(
81 /* region = */ scrollIndicatorTexture,
82 /* x = */ windowX + CreativeWindowConfig.scrollIndicatorMarginLeft,
83 /* y = */ windowY + CreativeWindowConfig.scrollIndicatorMarginTop
84 + (gameInput.creativeScroll * oneScrollAmount)
85 )
87 drawItemsGrid(
88 spriteBatch = spriteBatch,
89 gridX = windowX + CreativeWindowConfig.itemsGridMarginLeft,
90 gridY = windowY + CreativeWindowConfig.itemsGridMarginTop
91 )
93 drawPlayerInventory(
94 spriteBatch = spriteBatch,
95 shapeRenderer = shapeRenderer,
96 inventoryX = windowX + CreativeWindowConfig.itemsGridMarginLeft,
97 inventoryY = windowY + creativeWindow.regionHeight - CreativeWindowConfig.playerInventoryOffsetFromBottom
98 )
99 }
101 override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) {
102 drawCreative(spriteBatch, shapeRenderer, viewport)
105 companion object {
106 private const val CREATIVE_WINDOW_KEY = "creative"
107 private const val SCROLL_INDICATOR_KEY = "handle"
109 private data object CreativeWindowConfig {
110 const val scrollIndicatorMarginLeft = 156f
111 const val scrollIndicatorMarginTop = 18f
112 const val scrollIndicatorFullHeight = 72f
114 const val itemsGridMarginLeft = 8f
115 const val itemsGridMarginTop = 18f
117 const val itemsGridRowHeight = 18f
118 const val itemsGridColWidth = 18f
120 const val itemsInRow = 8
121 const val itemsInCol = 5
123 const val playerInventoryOffsetFromBottom = 24f
125 val itemsOnPage get() = itemsInCol * itemsInRow