DEADSOFTWARE

Update README
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / render / HudRenderer.kt
1 package ru.deadsoftware.cavedroid.game.render
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.game.GameScope
7 import ru.deadsoftware.cavedroid.game.mobs.MobsController
8 import ru.deadsoftware.cavedroid.game.mobs.Player.ControlMode
9 import ru.deadsoftware.cavedroid.game.world.GameWorld
10 import ru.deadsoftware.cavedroid.misc.Assets
11 import ru.deadsoftware.cavedroid.misc.utils.px
12 import javax.inject.Inject
14 @GameScope
15 class HudRenderer @Inject constructor(
16 private val gameWorld: GameWorld,
17 private val mobsController: MobsController,
18 ) : IGameRenderer {
20 override val renderLayer = RENDER_LAYER
22 private val cursorTexture get() = requireNotNull(Assets.textureRegions[CURSOR_KEY])
23 private val hotbarTexture get() = requireNotNull(Assets.textureRegions[HOTBAR_KEY])
24 private val hotbarSelectorTexture get() = requireNotNull(Assets.textureRegions[HOTBAR_SELECTOR_KEY])
25 private val wholeHeartTexture get() = requireNotNull(Assets.textureRegions[WHOLE_HEART_KEY])
26 private val halfHeartTexture get() = requireNotNull(Assets.textureRegions[HALF_HEART_KEY])
28 private fun drawCursor(spriteBatch: SpriteBatch, viewport: Rectangle) {
29 val cursorX = mobsController.player.cursorX
30 val cursorY = mobsController.player.cursorY
32 if (gameWorld.hasForeAt(cursorX, cursorY) ||
33 gameWorld.hasBackAt(cursorX, cursorY) ||
34 mobsController.player.controlMode == ControlMode.CURSOR
35 ) {
36 spriteBatch.draw(cursorTexture, cursorX.px - viewport.x, cursorY.px - viewport.y)
37 }
38 }
40 private fun drawHealth(spriteBatch: SpriteBatch, x: Float, y: Float) {
41 val player = mobsController.player
43 if (player.gameMode == 1) {
44 return
45 }
47 val wholeHeart = wholeHeartTexture
48 val wholeHearts = player.health / 2
50 for (i in 0..<wholeHearts) {
51 spriteBatch.draw(wholeHeart, x + i * wholeHeart.regionWidth, y)
52 }
54 if (player.health % 2 == 1) {
55 spriteBatch.draw(halfHeartTexture, x + wholeHearts * wholeHeart.regionWidth, y)
56 }
57 }
59 private fun drawHotbarItems(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, hotbarX: Float) {
60 mobsController.player.inventory.asSequence().take(HotbarConfig.hotbarCells)
61 .forEachIndexed { index, item ->
62 if (item.item.isNone()) {
63 return@forEachIndexed
64 }
66 item.draw(
67 spriteBatch = spriteBatch,
68 shapeRenderer = shapeRenderer,
69 x = hotbarX + HotbarConfig.horizontalMargin +
70 index * (HotbarConfig.itemSeparatorWidth + HotbarConfig.itemSlotSpace),
71 y = HotbarConfig.verticalMargin,
72 )
73 }
74 }
76 private fun drawHotbarSelector(spriteBatch: SpriteBatch, hotbarX: Float) {
77 spriteBatch.draw(
78 /* region = */ hotbarSelectorTexture,
79 /* x = */ hotbarX - HotbarSelectorConfig.horizontalPadding
80 + mobsController.player.slot * (HotbarConfig.itemSeparatorWidth + HotbarConfig.itemSlotSpace),
81 /* y = */ -HotbarSelectorConfig.verticalPadding
82 )
83 }
85 private fun drawHotbar(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle) {
86 val hotbar = hotbarTexture
87 val hotbarX = viewport.width / 2 - hotbar.regionWidth / 2
89 spriteBatch.draw(hotbar, hotbarX, 0f)
90 drawHealth(spriteBatch, hotbarX, hotbarTexture.regionHeight.toFloat())
91 drawHotbarSelector(spriteBatch, hotbarX)
92 drawHotbarItems(spriteBatch, shapeRenderer, hotbarX)
93 }
95 override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) {
96 drawCursor(spriteBatch, viewport)
97 drawHotbar(spriteBatch, shapeRenderer, viewport)
98 }
100 companion object {
101 private const val RENDER_LAYER = 100500
103 private const val CURSOR_KEY = "cursor"
104 private const val HOTBAR_KEY = "hotbar"
105 private const val HOTBAR_SELECTOR_KEY = "hotbar_selector"
106 private const val WHOLE_HEART_KEY = "heart_whole"
107 private const val HALF_HEART_KEY = "heart_half"
109 private data object HotbarConfig {
110 const val horizontalMargin = 3f
111 const val verticalMargin = 3f
112 const val itemSeparatorWidth = 4f
113 const val itemSlotSpace = 16f
114 const val hotbarCells = 9
117 private data object HotbarSelectorConfig {
118 const val horizontalPadding = 1f
119 const val verticalPadding = 1f