From 7cb80fd35da9bb41a3f2b96dd898d4bbb1e9b718 Mon Sep 17 00:00:00 2001 From: fredboy Date: Tue, 16 Apr 2024 16:50:59 +0700 Subject: [PATCH] Add biomes and fix mobs physics --- .../cavedroid/game/GamePhysics.java | 21 +++-- .../game/world/GameWorldGenerator.kt | 88 ++++++++++++++++--- 2 files changed, 87 insertions(+), 22 deletions(-) diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 9149705..3825394 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -167,9 +167,6 @@ public class GamePhysics { if (d == -1) { mob.setCanJump(true); mob.setFlyMode(false); - - int dmg = ((int)Math.max(0f, (((mob.getVelocity().y * mob.getVelocity().y) / (2 * gravity.y)) - 48f) / 16f)); - if (dmg > 0) System.out.println("Damage: " + dmg); } mob.y = MathUtils.round(mob.getY()); @@ -185,6 +182,8 @@ public class GamePhysics { //todo fall damage // h = (v^2) / 2g // dmg = max(0, (h - 48) / 32) - half of blocks fallen starting from 3 blocks height + // int dmg = ((int)Math.max(0f, (((mob.getVelocity().y * mob.getVelocity().y) / (2 * gravity.y)) - 48f) / 16f)); + // if (dmg > 0) System.out.println("Damage: " + dmg); } else { mob.y += 1; @@ -241,17 +240,17 @@ public class GamePhysics { private void mobPhy(Mob mob, float delta) { if (mob.getType() == Mob.Type.MOB && GameItems.isFluid(getBlock(mob))) { - if (mob.getVelocity().y > 540) { - mob.getVelocity().add(0, -5.4f); + if (mob.getVelocity().y > 32f) { + mob.getVelocity().y -= mob.getVelocity().y * 32f * delta; } - mob.getVelocity().add(0, -30f); + mob.getVelocity().y += PL_JUMP_VELOCITY * delta; - if (mob.getVelocity().y < -180) { - mob.getVelocity().y = -180; + if (mob.getVelocity().y < -PL_SPEED) { + mob.getVelocity().y = -PL_SPEED; } - } else if (!mob.isFlyMode() && mob.getVelocity().y < 1080) { - mob.getVelocity().add(gravity); + } else if (!mob.isFlyMode() && mob.getVelocity().y < PL_TERMINAL_VELOCITY) { + mob.getVelocity().y += gravity.y * delta; } mob.y += mob.getVelocity().y * delta; @@ -265,7 +264,7 @@ public class GamePhysics { mobXColl(mob); if (mob.canJump() && mob.getVelocity().x != 0 && checkJump(mob)) { - mob.getVelocity().add(0, -480); + mob.getVelocity().y += PL_JUMP_VELOCITY; mob.setCanJump(false); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldGenerator.kt b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldGenerator.kt index dd64c6a..bc5ffa4 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldGenerator.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldGenerator.kt @@ -7,6 +7,13 @@ import kotlin.random.Random object GameWorldGenerator { + private const val BIOME_MIN_SIZE = 64 + + private enum class Biome { + PLAINS, + DESERT + } + private fun generateHeights(width: Int, min: Int, max: Int, random: Random) = IntArray(width).apply { set(0, (min + max) / 2) for (x in 1 until width) { @@ -22,6 +29,68 @@ object GameWorldGenerator { } } + private fun generateBiomes(width: Int, random: Random) = buildMap { + val xSequence = sequence { + var lastX = 0 + var count = 0 + + while (lastX < width - BIOME_MIN_SIZE - 1) { + yield(lastX) + + lastX = random.nextInt(lastX + BIOME_MIN_SIZE, width) + count++ + } + } + + return xSequence.associateWith { Biome.values()[random.nextInt(Biome.values().size)] } + } + + private fun plainsBiome( + foreMap: Array, + backMap: Array, + width: Int, + height: Int, + x: Int, + xHeight: Int, + random: Random, + ) { + foreMap[x][xHeight] = GameItems.getBlockId("grass") + foreMap[x][height - 1] = GameItems.getBlockId("bedrock") + backMap[x][xHeight] = GameItems.getBlockId("grass") + backMap[x][height - 1] = GameItems.getBlockId("bedrock") + + for (y in xHeight + 1 until height - 1) { + foreMap[x][y] = when { + y < xHeight + random.nextInt(5, 8) -> GameItems.getBlockId("dirt") + else -> GameItems.getBlockId("stone") + } + backMap[x][y] = foreMap[x][y] + } + } + + private fun desertBiome( + foreMap: Array, + backMap: Array, + width: Int, + height: Int, + x: Int, + xHeight: Int, + random: Random, + ) { + foreMap[x][xHeight] = GameItems.getBlockId("sand") + foreMap[x][height - 1] = GameItems.getBlockId("bedrock") + backMap[x][xHeight] = GameItems.getBlockId("sand") + backMap[x][height - 1] = GameItems.getBlockId("bedrock") + + for (y in xHeight + 1 until height - 1) { + foreMap[x][y] = when { + y < xHeight + random.nextInt(5, 8) -> GameItems.getBlockId("sand") + else -> GameItems.getBlockId("stone") + } + backMap[x][y] = foreMap[x][y] + } + } + /** * Generates world of given width and height with given seed * @param width world width @@ -34,22 +103,19 @@ object GameWorldGenerator { val foreMap = Array(width) { IntArray(height) } val backMap = Array(width) { IntArray(width) } val heightsMap = generateHeights(width, height / 2, height * 3 / 4, random) + val biomesMap = generateBiomes(width, random) + + var biome = Biome.PLAINS for (x in 0 until width) { val xHeight = heightsMap[x] + biome = biomesMap[x] ?: biome - foreMap[x][xHeight] = GameItems.getBlockId("grass") - foreMap[x][height - 1] = GameItems.getBlockId("bedrock") - backMap[x][xHeight] = GameItems.getBlockId("grass") - backMap[x][height - 1] = GameItems.getBlockId("bedrock") - - for (y in xHeight + 1 until height - 1) { - foreMap[x][y] = when { - y < xHeight + random.nextInt(5, 8) -> GameItems.getBlockId("dirt") - else -> GameItems.getBlockId("stone") - } - backMap[x][y] = foreMap[x][y] + when (biome) { + Biome.PLAINS -> plainsBiome(foreMap, backMap, width, height, x, xHeight, random) + Biome.DESERT -> desertBiome(foreMap, backMap, width, height, x, xHeight, random) } + } return Pair(foreMap, backMap) } -- 2.29.2