DEADSOFTWARE

Fix drawing block damage
authorfredboy <fredboy@protonmail.com>
Sun, 21 Apr 2024 13:11:19 +0000 (20:11 +0700)
committerfredboy <fredboy@protonmail.com>
Sun, 21 Apr 2024 13:11:19 +0000 (20:11 +0700)
core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt
core/src/ru/deadsoftware/cavedroid/game/render/BlocksRenderer.kt
core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt

index ae02aa407cb432b31a191439c62c2be1025906c0..515ad796f62c8cfa866804f614d0b6d74f834aa9 100644 (file)
@@ -26,6 +26,8 @@ class BackgroundBlocksRenderer @Inject constructor(
             drawBackMap(spriteBatch, viewport, x, y)
         }
 
+        drawBlockDamage(spriteBatch, viewport)
+
         spriteBatch.end()
         Gdx.gl.glEnable(GL20.GL_BLEND)
         Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA)
index 3409fdf363f72ef44043c0ac629ef3e5b919d336..65d9e0ade81636d8855f1c509b51ae13a889f08f 100644 (file)
@@ -3,6 +3,7 @@ package ru.deadsoftware.cavedroid.game.render
 import com.badlogic.gdx.graphics.g2d.SpriteBatch
 import com.badlogic.gdx.graphics.g2d.TextureRegion
 import com.badlogic.gdx.graphics.glutils.ShapeRenderer
+import com.badlogic.gdx.math.MathUtils
 import com.badlogic.gdx.math.Rectangle
 import ru.deadsoftware.cavedroid.game.GameInput
 import ru.deadsoftware.cavedroid.game.model.block.Block
@@ -28,16 +29,25 @@ abstract class BlocksRenderer(
         return Assets.textureRegions[textureKey]
     }
 
-    private fun drawBlockDamage(spriteBatch: SpriteBatch, block: Block, x: Float, y: Float) {
+    protected fun drawBlockDamage(spriteBatch: SpriteBatch, viewport: Rectangle) {
         val blockDamage = gameInput.blockDamage
         if (blockDamage <= 0) {
             return
         }
 
-        val index = MAX_BLOCK_DAMAGE_INDEX * (blockDamage / block.params.hitPoints)
+        val block = if (background) {
+            gameWorld.getBackMap(gameInput.curX, gameInput.curY)
+        } else {
+            gameWorld.getForeMap(gameInput.curX, gameInput.curY)
+        }
+
+        val index = (MAX_BLOCK_DAMAGE_INDEX.toFloat() * (blockDamage.toFloat() / block.params.hitPoints.toFloat()))
+            .let(MathUtils::floor)
         val texture = blockDamageTexture(index) ?: return
 
-        spriteBatch.draw(texture, x, y)
+        if (gameWorld.hasForeAt(gameInput.curX, gameInput.curY) != background) {
+            spriteBatch.draw(texture, gameInput.curX.px - viewport.x, gameInput.curY.px - viewport.y)
+        }
     }
 
     protected fun shadeBackMap(
@@ -72,10 +82,6 @@ abstract class BlocksRenderer(
             val drawX = x.px - viewport.x
             val drawY = y.px - viewport.y
             backgroundBlock.draw(spriteBatch, drawX, drawY)
-
-            if (foregroundBlock.isNone()) {
-                drawBlockDamage(spriteBatch, backgroundBlock, drawX, drawY)
-            }
         }
     }
 
@@ -86,10 +92,6 @@ abstract class BlocksRenderer(
             val drawX = x.px - viewport.x
             val drawY = y.px - viewport.y
             foregroundBlock.draw(spriteBatch, drawX, drawY)
-
-            if (!foregroundBlock.isNone()) {
-                drawBlockDamage(spriteBatch, foregroundBlock, drawX, drawY)
-            }
         }
     }
 
index 3380490a40e3ec6108ca3ea4851147e0885df40b..30736958f36f3f31ec1e8941a11fc4f11e85b25a 100644 (file)
@@ -23,6 +23,7 @@ class ForegroundBlocksRenderer @Inject constructor(
         forEachBlockInArea(viewport) { x, y ->
             drawForeMap(spriteBatch, viewport, x, y)
         }
+        drawBlockDamage(spriteBatch, viewport)
     }
 
     companion object {