DEADSOFTWARE

Add environment damage
authorfredboy <fredboy@protonmail.com>
Mon, 22 Apr 2024 16:10:56 +0000 (23:10 +0700)
committerfredboy <fredboy@protonmail.com>
Mon, 22 Apr 2024 16:10:56 +0000 (23:10 +0700)
android/assets/json/game_items.json
core/src/ru/deadsoftware/cavedroid/game/GameProc.java
core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java
core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java
core/src/ru/deadsoftware/cavedroid/game/model/block/CommonBlockParams.kt
core/src/ru/deadsoftware/cavedroid/game/model/dto/BlockDto.kt
core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt
core/src/ru/deadsoftware/cavedroid/game/render/DebugRenderer.kt
core/src/ru/deadsoftware/cavedroid/game/world/GameWorldMobDamageControllerTask.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/misc/utils/RenderingUtils.kt

index 4f08f56183628fc9674836bd784d56dfea209d46..be444541df17b151ebc9e24604b62d471076caf8 100644 (file)
@@ -71,7 +71,8 @@
       "texture": "lava_still",
       "animated": true,
       "frames": 16,
-      "state": 0
+      "state": 0,
+      "damage": 4
     },
     "sand": {
       "hp": 45,
       "transparent": true,
       "block_required": true,
       "drop": "cactus",
-      "texture": "cactus"
+      "texture": "cactus",
+      "damage": 1
     },
     "water_16": {
       "collision": false,
       "texture": "lava_flow",
       "animated": true,
       "frames": 16,
-      "state": 1
+      "state": 1,
+      "damage": 4
     },
     "lava_12": {
       "top": 4,
       "texture": "lava_flow",
       "animated": true,
       "frames": 16,
-      "state": 2
+      "state": 2,
+      "damage": 4
     },
     "lava_8": {
       "top": 8,
       "texture": "lava_flow",
       "animated": true,
       "frames": 16,
-      "state": 3
+      "state": 3,
+      "damage": 4
     },
     "lava_4": {
       "top": 12,
       "texture": "lava_flow",
       "animated": true,
       "frames": 16,
-      "state": 4
+      "state": 4,
+      "damage": 4
     },
     "obsidian": {
       "hp": 1500,
index 184e376cba5c448991c67c303e73f132c613cb3a..75605bf058dd6b9ee2d4fbc6a88a627202aecf7a 100644 (file)
@@ -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();
     }
 }
index 394346f2b9252f31d4982d7b2c4771d9d42bcf1c..415ef30376503a491a3eefd9eb8c127d53089b77 100644 (file)
@@ -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;
index 6e0dfe394700d5f1e63eeb9b5e33c0f5b2c2a930..b1896e4fdb8f858a3f32fd3c91bd7d36e9e4ba1c 100644 (file)
@@ -180,6 +180,11 @@ public class Player extends Mob {
         if (gameMode == 1) {
             return;
         }
+
+        if (damage > 0) {
+            getVelocity().y += JUMP_VELOCITY / 3f;
+        }
+
         super.damage(damage);
     }
 
index 584e7376298d4fa103d36b88ed8d9c4e2ee8e6cf..202b191bf21d9859fb56d44642d8d5dc043e4655 100644 (file)
@@ -17,5 +17,6 @@ data class CommonBlockParams(
     val texture: Texture?,
     val spriteMargins: BlockMargins,
     val toolLevel: Int,
-    val toolType: Class<out Item.Tool>?
+    val toolType: Class<out Item.Tool>?,
+    val damage: Int,
 )
index 1a7dc4b3bf5a813354d65678b69a11b684bbe09d..b8c63268825acc07430df7e9bfaee7aa5c7b10fe 100644 (file)
@@ -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,
 )
index 8373e02febd5189095af8919f9ae594b7da9cb70..e97659a953a83e8b69dbb0bc90e0df9c188f504f 100644 (file)
@@ -54,6 +54,7 @@ class BlockMapper @Inject constructor(
             ),
             toolLevel = dto.toolLevel,
             toolType = mapToolType(dto),
+            damage = dto.damage,
         )
     }
 
index 03f62534522264f008936a451faaa9d2139ea989..8145324ee9b1ac88d8f10316645196bd845fe1d4 100644 (file)
@@ -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 (file)
index 0000000..8dc39b9
--- /dev/null
@@ -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
index 5849f03c79f02356a2e14a3f3c58df7102421f0a..d197229578bfb8a92e989eb228b05a2bf05f0c69 100644 (file)
@@ -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) {