DEADSOFTWARE

Drop items on Q and add empty hearts to health bar
[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.Player
9 import ru.deadsoftware.cavedroid.game.mobs.player.Player.ControlMode
10 import ru.deadsoftware.cavedroid.game.world.GameWorld
11 import ru.deadsoftware.cavedroid.misc.Assets
12 import ru.deadsoftware.cavedroid.misc.utils.px
13 import javax.inject.Inject
15 @GameScope
16 class HudRenderer @Inject constructor(
17 private val gameWorld: GameWorld,
18 private val mobsController: MobsController,
19 ) : IGameRenderer {
21 override val renderLayer = RENDER_LAYER
23 private val cursorTexture get() = requireNotNull(Assets.textureRegions[CURSOR_KEY])
24 private val hotbarTexture get() = requireNotNull(Assets.textureRegions[HOTBAR_KEY])
25 private val hotbarSelectorTexture get() = requireNotNull(Assets.textureRegions[HOTBAR_SELECTOR_KEY])
26 private val wholeHeartTexture get() = requireNotNull(Assets.textureRegions[WHOLE_HEART_KEY])
27 private val emptyHeartTexture get() = requireNotNull(Assets.textureRegions[EMPTY_HEART_KEY])
28 private val halfHeartTexture get() = requireNotNull(Assets.textureRegions[HALF_HEART_KEY])
30 private fun drawCursor(spriteBatch: SpriteBatch, viewport: Rectangle) {
31 val cursorX = mobsController.player.cursorX
32 val cursorY = mobsController.player.cursorY
34 if (gameWorld.hasForeAt(cursorX, cursorY) ||
35 gameWorld.hasBackAt(cursorX, cursorY) ||
36 mobsController.player.controlMode == ControlMode.CURSOR
37 ) {
38 spriteBatch.draw(cursorTexture, cursorX.px - viewport.x, cursorY.px - viewport.y)
39 }
40 }
42 private fun drawHealth(spriteBatch: SpriteBatch, x: Float, y: Float) {
43 val player = mobsController.player
45 if (player.gameMode == 1) {
46 return
47 }
49 val wholeHeart = wholeHeartTexture
50 val halfHeart = halfHeartTexture
51 val emptyHeart = emptyHeartTexture
53 val totalHearts = Player.MAX_HEALTH / 2
54 val wholeHearts = player.health / 2
56 for (i in 0..< totalHearts) {
57 if (i < wholeHearts) {
58 spriteBatch.draw(wholeHeart, x + i * wholeHeart.regionWidth, y)
59 } else if (i == wholeHearts && player.health % 2 == 1) {
60 spriteBatch.draw(halfHeart, x + i * wholeHeart.regionWidth, y)
61 } else {
62 spriteBatch.draw(emptyHeart, x + i * wholeHeart.regionWidth, y)
63 }
64 }
69 }
71 private fun drawHotbarItems(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, hotbarX: Float) {
72 mobsController.player.inventory.items.asSequence().take(HotbarConfig.hotbarCells)
73 .forEachIndexed { index, item ->
74 if (item.item.isNone()) {
75 return@forEachIndexed
76 }
78 item.draw(
79 spriteBatch = spriteBatch,
80 shapeRenderer = shapeRenderer,
81 x = hotbarX + HotbarConfig.horizontalMargin +
82 index * (HotbarConfig.itemSeparatorWidth + HotbarConfig.itemSlotSpace),
83 y = HotbarConfig.verticalMargin,
84 )
85 }
86 }
88 private fun drawHotbarSelector(spriteBatch: SpriteBatch, hotbarX: Float) {
89 spriteBatch.draw(
90 /* region = */ hotbarSelectorTexture,
91 /* x = */ hotbarX - HotbarSelectorConfig.horizontalPadding
92 + mobsController.player.inventory.activeSlot * (HotbarConfig.itemSeparatorWidth + HotbarConfig.itemSlotSpace),
93 /* y = */ -HotbarSelectorConfig.verticalPadding
94 )
95 }
97 private fun drawHotbar(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle) {
98 val hotbar = hotbarTexture
99 val hotbarX = viewport.width / 2 - hotbar.regionWidth / 2
101 spriteBatch.draw(hotbar, hotbarX, 0f)
102 drawHealth(spriteBatch, hotbarX, hotbarTexture.regionHeight.toFloat())
103 drawHotbarSelector(spriteBatch, hotbarX)
104 drawHotbarItems(spriteBatch, shapeRenderer, hotbarX)
107 override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) {
108 drawCursor(spriteBatch, viewport)
109 drawHotbar(spriteBatch, shapeRenderer, viewport)
112 companion object {
113 private const val RENDER_LAYER = 100500
115 private const val CURSOR_KEY = "cursor"
116 private const val HOTBAR_KEY = "hotbar"
117 private const val HOTBAR_SELECTOR_KEY = "hotbar_selector"
118 private const val WHOLE_HEART_KEY = "heart_whole"
119 private const val HALF_HEART_KEY = "heart_half"
120 private const val EMPTY_HEART_KEY = "heart_empty"
122 private data object HotbarConfig {
123 const val horizontalMargin = 3f
124 const val verticalMargin = 3f
125 const val itemSeparatorWidth = 4f
126 const val itemSlotSpace = 16f
127 const val hotbarCells = 9
130 private data object HotbarSelectorConfig {
131 const val horizontalPadding = 1f
132 const val verticalPadding = 1f