DEADSOFTWARE

Refactor rendering
[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(spriteBatch: SpriteBatch, inventoryX: Float, inventoryY: Float) {
55 mobsController.player.inventory.asSequence()
56 .map(InventoryItem::item)
57 .forEachIndexed { index, item ->
58 if (item.isNone()) {
59 return@forEachIndexed
60 }
62 val itemX = inventoryX + index * CreativeWindowConfig.itemsGridColWidth
64 spriteBatch.draw(item.sprite, itemX, inventoryY)
65 }
66 }
68 private fun drawCreative(spriteBatch: SpriteBatch, viewport: Rectangle) {
69 val creativeWindow = creativeWindowTexture
71 val windowX = viewport.width / 2 - creativeWindow.regionWidth / 2
72 val windowY = viewport.height / 2 - creativeWindow.regionHeight / 2
73 val oneScrollAmount = CreativeWindowConfig.scrollIndicatorFullHeight / gameItemsHolder.getCreativeScrollAmount()
75 spriteBatch.draw(creativeWindow, windowX, windowY)
76 spriteBatch.draw(
77 /* region = */ scrollIndicatorTexture,
78 /* x = */ windowX + CreativeWindowConfig.scrollIndicatorMarginLeft,
79 /* y = */ windowY + CreativeWindowConfig.scrollIndicatorMarginTop
80 + (gameInput.creativeScroll * oneScrollAmount)
81 )
83 drawItemsGrid(
84 spriteBatch = spriteBatch,
85 gridX = windowX + CreativeWindowConfig.itemsGridMarginLeft,
86 gridY = windowY + CreativeWindowConfig.itemsGridMarginTop
87 )
89 drawPlayerInventory(
90 spriteBatch = spriteBatch,
91 inventoryX = windowX + CreativeWindowConfig.itemsGridMarginLeft,
92 inventoryY = windowY + creativeWindow.regionHeight - CreativeWindowConfig.playerInventoryOffsetFromBottom
93 )
94 }
96 override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) {
97 drawCreative(spriteBatch, viewport)
98 }
100 companion object {
101 private const val CREATIVE_WINDOW_KEY = "creative"
102 private const val SCROLL_INDICATOR_KEY = "handle"
104 private data object CreativeWindowConfig {
105 const val scrollIndicatorMarginLeft = 156f
106 const val scrollIndicatorMarginTop = 18f
107 const val scrollIndicatorFullHeight = 72f
109 const val itemsGridMarginLeft = 8f
110 const val itemsGridMarginTop = 18f
112 const val itemsGridRowHeight = 18f
113 const val itemsGridColWidth = 18f
115 const val itemsInRow = 8
116 const val itemsInCol = 5
118 const val playerInventoryOffsetFromBottom = 24f
120 val itemsOnPage get() = itemsInCol * itemsInRow