From: fredboy Date: Mon, 22 Apr 2024 16:10:56 +0000 (+0700) Subject: Add environment damage X-Git-Tag: alpha0.6.2~9 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=3a63261d832e0951af6c7095598eb48658c4a9f5;p=cavedroid.git Add environment damage --- diff --git a/android/assets/json/game_items.json b/android/assets/json/game_items.json index 4f08f56..be44454 100644 --- a/android/assets/json/game_items.json +++ b/android/assets/json/game_items.json @@ -71,7 +71,8 @@ "texture": "lava_still", "animated": true, "frames": 16, - "state": 0 + "state": 0, + "damage": 4 }, "sand": { "hp": 45, @@ -550,7 +551,8 @@ "transparent": true, "block_required": true, "drop": "cactus", - "texture": "cactus" + "texture": "cactus", + "damage": 1 }, "water_16": { "collision": false, @@ -605,7 +607,8 @@ "texture": "lava_flow", "animated": true, "frames": 16, - "state": 1 + "state": 1, + "damage": 4 }, "lava_12": { "top": 4, @@ -617,7 +620,8 @@ "texture": "lava_flow", "animated": true, "frames": 16, - "state": 2 + "state": 2, + "damage": 4 }, "lava_8": { "top": 8, @@ -629,7 +633,8 @@ "texture": "lava_flow", "animated": true, "frames": 16, - "state": 3 + "state": 3, + "damage": 4 }, "lava_4": { "top": 12, @@ -641,7 +646,8 @@ "texture": "lava_flow", "animated": true, "frames": 16, - "state": 4 + "state": 4, + "damage": 4 }, "obsidian": { "hp": 1500, diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index 184e376..75605bf 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -5,6 +5,7 @@ import com.badlogic.gdx.utils.Timer; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.world.GameWorldBlocksLogicControllerTask; import ru.deadsoftware.cavedroid.game.world.GameWorldFluidsLogicControllerTask; +import ru.deadsoftware.cavedroid.game.world.GameWorldMobDamageControllerTask; import javax.inject.Inject; @@ -17,6 +18,7 @@ public class GameProc implements Disposable { private final MobsController mMobsController; private final GameWorldFluidsLogicControllerTask mGameWorldFluidsLogicControllerTask; private final GameWorldBlocksLogicControllerTask mGameWorldBlocksLogicControllerTask; + private final GameWorldMobDamageControllerTask mGameWorldMobDamageControllerTask; private final Timer mWorldLogicTimer = new Timer(); @@ -26,7 +28,8 @@ public class GameProc implements Disposable { GameRenderer gameRenderer, MobsController mobsController, GameWorldFluidsLogicControllerTask gameWorldFluidsLogicControllerTask, - GameWorldBlocksLogicControllerTask gameWorldBlocksLogicControllerTask + GameWorldBlocksLogicControllerTask gameWorldBlocksLogicControllerTask, + GameWorldMobDamageControllerTask gameWorldMobDamageControllerTask ) { mGamePhysics = gamePhysics; mGameInput = gameInput; @@ -34,6 +37,7 @@ public class GameProc implements Disposable { mMobsController = mobsController; mGameWorldFluidsLogicControllerTask = gameWorldFluidsLogicControllerTask; mGameWorldBlocksLogicControllerTask = gameWorldBlocksLogicControllerTask; + mGameWorldMobDamageControllerTask = gameWorldMobDamageControllerTask; @@ -41,6 +45,8 @@ public class GameProc implements Disposable { GameWorldFluidsLogicControllerTask.FLUID_UPDATE_INTERVAL_SEC); mWorldLogicTimer.scheduleTask(gameWorldBlocksLogicControllerTask, 0, GameWorldBlocksLogicControllerTask.WORLD_BLOCKS_LOGIC_UPDATE_INTERVAL_SEC); + mWorldLogicTimer.scheduleTask(gameWorldMobDamageControllerTask, 0, + GameWorldMobDamageControllerTask.ENVIRONMENTAL_MOB_DAMAGE_INTERVAL_SEC); } public void setPlayerGameMode(int gameMode) { @@ -58,5 +64,6 @@ public class GameProc implements Disposable { mWorldLogicTimer.stop(); mGameWorldFluidsLogicControllerTask.cancel(); mGameWorldBlocksLogicControllerTask.cancel(); + mGameWorldMobDamageControllerTask.cancel(); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 394346f..415ef30 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -242,6 +242,10 @@ public abstract class Mob extends Rectangle implements Serializable { } public void damage(int damage) { + if (damage == 0) { + return; + } + if (damage < 0) { Gdx.app.error(this.getClass().getSimpleName(), "Damage cant be negative!"); return; diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 6e0dfe3..b1896e4 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -180,6 +180,11 @@ public class Player extends Mob { if (gameMode == 1) { return; } + + if (damage > 0) { + getVelocity().y += JUMP_VELOCITY / 3f; + } + super.damage(damage); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/block/CommonBlockParams.kt b/core/src/ru/deadsoftware/cavedroid/game/model/block/CommonBlockParams.kt index 584e737..202b191 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/block/CommonBlockParams.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/block/CommonBlockParams.kt @@ -17,5 +17,6 @@ data class CommonBlockParams( val texture: Texture?, val spriteMargins: BlockMargins, val toolLevel: Int, - val toolType: Class? + val toolType: Class?, + val damage: Int, ) diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/dto/BlockDto.kt b/core/src/ru/deadsoftware/cavedroid/game/model/dto/BlockDto.kt index 1a7dc4b..b8c6326 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/dto/BlockDto.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/dto/BlockDto.kt @@ -30,4 +30,5 @@ data class BlockDto( @SerialName("other_part") val otherPart: String? = null, @SerialName("tool_level") val toolLevel: Int = 0, @SerialName("tool_type") val toolType: String? = null, + @SerialName("damage") val damage: Int = 0, ) diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt index 8373e02..e97659a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt @@ -54,6 +54,7 @@ class BlockMapper @Inject constructor( ), toolLevel = dto.toolLevel, toolType = mapToolType(dto), + damage = dto.damage, ) } diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt index 03f6253..8145324 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt @@ -89,6 +89,15 @@ class DebugRenderer @Inject constructor( override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) { if (mainConfig.isShowInfo) { drawDebugInfo(spriteBatch) + + spriteBatch.end(); + shapeRenderer.begin(ShapeRenderer.ShapeType.Line); + forEachBlockInArea(mobsController.player) { x: Int, y: Int -> + shapeRenderer.color = Color.CYAN + shapeRenderer.rect(x.px - viewport.x, y.px - viewport.y, 16f, 16f) + } + shapeRenderer.end() + spriteBatch.begin() } if (mainConfig.isShowMap) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldMobDamageControllerTask.kt b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldMobDamageControllerTask.kt new file mode 100644 index 0000000..8dc39b9 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldMobDamageControllerTask.kt @@ -0,0 +1,38 @@ +package ru.deadsoftware.cavedroid.game.world + +import com.badlogic.gdx.utils.Timer +import ru.deadsoftware.cavedroid.game.GameItemsHolder +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.mobs.MobsController +import ru.deadsoftware.cavedroid.misc.utils.forEachBlockInArea +import javax.inject.Inject +import kotlin.math.max + +@GameScope +class GameWorldMobDamageControllerTask @Inject constructor( + private val mobsController: MobsController, + private val gameWorld: GameWorld, + private val gameItemsHolder: GameItemsHolder, +) : Timer.Task() { + + override fun run() { + sequence { + yield(mobsController.player) + yieldAll(mobsController.mobs) + }.forEach { mob -> + forEachBlockInArea(mob) { x, y -> + val foregroundBlock = gameWorld.getForeMap(x, y) + val backgroundBlock = gameWorld.getBackMap(x, y) + + mob.damage(max(foregroundBlock.params.damage, backgroundBlock.params.damage)) + } + } + + + } + + companion object { + const val ENVIRONMENTAL_MOB_DAMAGE_INTERVAL_SEC = 0.5f + } + +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt index 5849f03..d197229 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt +++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt @@ -31,12 +31,12 @@ fun Rectangle.cycledInsideWorld( fun forEachBlockInArea( area: Rectangle, - func: (x: Int, y: Int) -> Unit + func: (x: Int, y: Int) -> Unit, ) { val startMapX = area.x.bl - val endMapX = startMapX + area.width.bl + 1 + val endMapX = (area.x + area.width - 1f).bl val startMapY = area.y.bl - val endMapY = startMapY + area.height.bl + 1 + val endMapY = (area.y + area.height - 1f).bl for (x in startMapX..endMapX) { for (y in startMapY..endMapY) {