From: fredboy Date: Sun, 21 Apr 2024 13:11:19 +0000 (+0700) Subject: Fix drawing block damage X-Git-Tag: alpha0.6.0~7 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=7bb5658145e70efdfbd66ab264bf958057f26124;p=cavedroid.git Fix drawing block damage --- diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt index ae02aa4..515ad79 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt @@ -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) diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/BlocksRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/BlocksRenderer.kt index 3409fdf..65d9e0a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/BlocksRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/BlocksRenderer.kt @@ -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) - } } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt index 3380490..3073695 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt @@ -23,6 +23,7 @@ class ForegroundBlocksRenderer @Inject constructor( forEachBlockInArea(viewport) { x, y -> drawForeMap(spriteBatch, viewport, x, y) } + drawBlockDamage(spriteBatch, viewport) } companion object {