1 package ru.fredboy.cavedroid.ux.rendering.windows
3 import com.badlogic.gdx.Gdx
4 import com.badlogic.gdx.graphics.g2d.SpriteBatch
5 import com.badlogic.gdx.graphics.glutils.ShapeRenderer
6 import com.badlogic.gdx.math.MathUtils
7 import com.badlogic.gdx.math.Rectangle
8 import ru.fredboy.cavedroid.ux.rendering.IGameRenderer
9 import ru.fredboy.cavedroid.ux.rendering.WindowsRenderer
10 import ru.fredboy.cavedroid.common.di.GameScope
11 import ru.fredboy.cavedroid.domain.assets.usecase.GetFontUseCase
12 import ru.fredboy.cavedroid.domain.assets.usecase.GetStringHeightUseCase
13 import ru.fredboy.cavedroid.domain.assets.usecase.GetStringWidthUseCase
14 import ru.fredboy.cavedroid.domain.assets.usecase.GetTextureRegionByNameUseCase
15 import ru.fredboy.cavedroid.domain.configuration.repository.GameConfigurationRepository
16 import ru.fredboy.cavedroid.entity.mob.model.Direction
17 import ru.fredboy.cavedroid.game.controller.mob.MobController
18 import ru.fredboy.cavedroid.game.window.GameWindowsConfigs
19 import ru.fredboy.cavedroid.game.window.GameWindowsManager
20 import ru.fredboy.cavedroid.game.window.inventory.SurvivalInventoryWindow
21 import javax.inject.Inject
22 import kotlin.math.atan
25 class SurvivalWindowRenderer @Inject constructor(
26 private val gameConfigurationRepository: GameConfigurationRepository,
27 private val mobController: MobController,
28 private val gameWindowsManager: GameWindowsManager,
29 private val textureRegions: GetTextureRegionByNameUseCase,
30 private val getStringWidth: GetStringWidthUseCase,
31 private val getStringHeight: GetStringHeightUseCase,
32 private val getFont: GetFontUseCase,
33 ) : AbstractWindowRenderer(), IGameRenderer {
35 override val renderLayer get() = WindowsRenderer.Companion.RENDER_LAYER
37 private val survivalWindowTexture get() = requireNotNull(textureRegions[SURVIVAL_WINDOW_KEY])
39 private fun setPortraitHeadRotation(portraitX: Float, portraitY: Float) {
40 if (gameConfigurationRepository.isTouch()) {
44 val mouseX = Gdx.input.x * (gameConfigurationRepository.getWidth() / Gdx.graphics.width)
45 val mouseY = Gdx.input.y * (gameConfigurationRepository.getHeight() / Gdx.graphics.height)
47 val h = mouseX.toDouble() - portraitX.toDouble()
48 val v = mouseY.toDouble() - portraitY.toDouble()
50 mobController.player.direction = if (mouseX < portraitX + mobController.player.width / 2) {
56 mobController.player.headRotation = atan(v / h).toFloat() * MathUtils.radDeg
59 private fun drawPlayerPortrait(spriteBatch: SpriteBatch, windowX: Float, windowY: Float, delta: Float) {
60 val portraitX = windowX + GameWindowsConfigs.Survival.portraitMarginLeft +
61 (GameWindowsConfigs.Survival.portraitWidth / 2 - mobController.player.width / 2)
62 val portraitY = windowY + GameWindowsConfigs.Survival.portraitMarginTop +
63 (GameWindowsConfigs.Survival.portraitHeight / 2 - mobController.player.height / 2)
65 setPortraitHeadRotation(portraitX, portraitY)
66 mobController.player.draw(spriteBatch, portraitX, portraitY, delta)
69 override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) {
70 val windowTexture = survivalWindowTexture
71 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
73 val windowX = viewport.width / 2 - windowTexture.regionWidth / 2
74 val windowY = viewport.height / 2 - windowTexture.regionHeight / 2
76 spriteBatch.draw(windowTexture, windowX, windowY)
78 drawPlayerPortrait(spriteBatch, windowX, windowY, delta)
81 spriteBatch = spriteBatch,
82 shapeRenderer = shapeRenderer,
84 gridX = windowX + GameWindowsConfigs.Survival.itemsGridMarginLeft,
85 gridY = windowY + GameWindowsConfigs.Survival.itemsGridMarginTop,
86 items = mobController.player.inventory.items.asSequence()
87 .drop(GameWindowsConfigs.Survival.hotbarCells)
88 .take(GameWindowsConfigs.Survival.itemsInCol * GameWindowsConfigs.Survival.itemsInRow)
90 itemsInRow = GameWindowsConfigs.Survival.itemsInRow,
91 cellWidth = GameWindowsConfigs.Survival.itemsGridColWidth,
92 cellHeight = GameWindowsConfigs.Survival.itemsGridRowHeight,
93 getStringWidth = getStringWidth,
94 getStringHeight = getStringHeight
98 spriteBatch = spriteBatch,
99 shapeRenderer = shapeRenderer,
101 gridX = windowX + GameWindowsConfigs.Survival.itemsGridMarginLeft,
102 gridY = windowY + windowTexture.regionHeight - GameWindowsConfigs.Survival.hotbarOffsetFromBottom,
103 items = mobController.player.inventory.items.asSequence()
104 .take(GameWindowsConfigs.Survival.hotbarCells)
106 itemsInRow = GameWindowsConfigs.Survival.hotbarCells,
107 cellWidth = GameWindowsConfigs.Survival.itemsGridColWidth,
108 cellHeight = GameWindowsConfigs.Survival.itemsGridRowHeight,
109 getStringWidth = getStringWidth,
110 getStringHeight = getStringHeight
114 spriteBatch = spriteBatch,
115 shapeRenderer = shapeRenderer,
117 gridX = windowX + GameWindowsConfigs.Survival.craftOffsetX,
118 gridY = windowY + GameWindowsConfigs.Survival.craftOffsetY,
119 items = window.craftingItems.asSequence().mapIndexedNotNull { index, it ->
120 if (index % 3 > 1 || index / 3 > 1) {
126 itemsInRow = GameWindowsConfigs.Survival.craftGridSize,
127 cellWidth = GameWindowsConfigs.Survival.itemsGridColWidth,
128 cellHeight = GameWindowsConfigs.Survival.itemsGridRowHeight,
129 getStringWidth = getStringWidth,
130 getStringHeight = getStringHeight
133 window.craftResult.draw(
134 spriteBatch = spriteBatch,
135 shapeRenderer = shapeRenderer,
137 x = windowX + GameWindowsConfigs.Survival.craftResultOffsetX,
138 y = windowY + GameWindowsConfigs.Survival.craftResultOffsetY,
139 getStringWidth = getStringWidth::invoke,
140 getStringHeight = getStringHeight::invoke,
143 window.selectedItem?.drawSelected(
144 spriteBatch = spriteBatch,
146 x = Gdx.input.x * (viewport.width / Gdx.graphics.width),
147 y = Gdx.input.y * (viewport.height / Gdx.graphics.height),
148 getStringWidth = getStringWidth::invoke,
149 getStringHeight = getStringHeight::invoke,
154 private const val SURVIVAL_WINDOW_KEY = "survival"