From: fredboy Date: Mon, 15 Apr 2024 10:42:08 +0000 (+0700) Subject: Move game world to new package X-Git-Tag: alpha0.4.1~5 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=2aa65a4cdb47df8014f28342d460fc6639bed885;p=cavedroid.git Move game world to new package --- diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 2376bb1..3da4e2e 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -11,6 +11,7 @@ import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.mobs.Pig; import ru.deadsoftware.cavedroid.game.mobs.Player; import ru.deadsoftware.cavedroid.game.objects.DropController; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.ControlMode; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameModule.java b/core/src/ru/deadsoftware/cavedroid/game/GameModule.java index d7ccc2d..a72dbcd 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameModule.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameModule.java @@ -5,6 +5,7 @@ import dagger.Provides; import ru.deadsoftware.cavedroid.MainConfig; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.objects.DropController; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import javax.annotation.CheckForNull; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index cb5b64c..8d5d24c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -10,6 +10,7 @@ import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.mobs.Player; import ru.deadsoftware.cavedroid.game.objects.Drop; import ru.deadsoftware.cavedroid.game.objects.DropController; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import javax.inject.Inject; import java.util.Iterator; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index d8925f9..8c4fefb 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -1,6 +1,7 @@ package ru.deadsoftware.cavedroid.game; import com.badlogic.gdx.utils.Disposable; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import javax.inject.Inject; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index c35b3b4..44ec9ba 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -12,6 +12,7 @@ import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.mobs.Player; import ru.deadsoftware.cavedroid.game.objects.Drop; import ru.deadsoftware.cavedroid.game.objects.DropController; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import ru.deadsoftware.cavedroid.misc.ControlMode; import ru.deadsoftware.cavedroid.misc.Renderer; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java b/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java index 1ff2bad..0e14983 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java @@ -5,6 +5,7 @@ import com.badlogic.gdx.files.FileHandle; import ru.deadsoftware.cavedroid.MainConfig; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.objects.DropController; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import javax.annotation.CheckForNull; import java.io.*; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameWorldGenerator.kt b/core/src/ru/deadsoftware/cavedroid/game/GameWorldGenerator.kt deleted file mode 100644 index c157a4e..0000000 --- a/core/src/ru/deadsoftware/cavedroid/game/GameWorldGenerator.kt +++ /dev/null @@ -1,52 +0,0 @@ -package ru.deadsoftware.cavedroid.game - -import com.badlogic.gdx.utils.TimeUtils -import kotlin.math.abs -import kotlin.random.Random - -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) { - val previous = get(x - 1) - var d = random.nextInt(-5, 6).let { if (it !in -4..4) it / abs(it) else 0 } - - if (previous + d !in min..max) { d = -d } - if (lastIndex - x < abs(get(0) - previous) * 3) { - d = get(0).compareTo(previous).let { if (it != 0) it / abs(it) else 0 } - } - - set(x, get(x - 1) + d) - } -} - -/** - * Generates world of given width and height with given seed - * @param width world width - * @param height world height - * @param seed seed for random number generator - * @return pair of foreground and background layers - */ -fun generate(width: Int, height: Int, seed: Long = TimeUtils.millis()): Pair, Array> { - val random = Random(seed) - val foreMap = Array(width) { IntArray(height) } - val backMap = Array(width) { IntArray(width) } - val heightsMap = generateHeights(width, height / 2, height * 3 / 4, random) - - for (x in 0 until width) { - val xHeight = heightsMap[x] - - 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] - } - } - return Pair(foreMap, backMap) -} diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java index f4c62f9..495b628 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java @@ -3,7 +3,7 @@ package ru.deadsoftware.cavedroid.game.mobs; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavedroid.game.GameItems; -import ru.deadsoftware.cavedroid.game.GameWorld; +import ru.deadsoftware.cavedroid.game.world.GameWorld; /** * Falling gravel is actually a mob, that spawns in place of gravel when there is no block under it, diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java index d124243..07cd8cd 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java @@ -3,7 +3,7 @@ package ru.deadsoftware.cavedroid.game.mobs; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavedroid.game.GameItems; -import ru.deadsoftware.cavedroid.game.GameWorld; +import ru.deadsoftware.cavedroid.game.world.GameWorld; /** diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 9f14a69..4ad04e8 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -4,7 +4,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; -import ru.deadsoftware.cavedroid.game.GameWorld; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import java.io.Serializable; diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java index 5742a7c..15185ef 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java @@ -3,7 +3,7 @@ package ru.deadsoftware.cavedroid.game.mobs; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; -import ru.deadsoftware.cavedroid.game.GameWorld; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import ru.deadsoftware.cavedroid.misc.Assets; import static ru.deadsoftware.cavedroid.misc.Assets.pigSprite; diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 5cdb5d8..272992a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -2,8 +2,7 @@ package ru.deadsoftware.cavedroid.game.mobs; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector2; -import ru.deadsoftware.cavedroid.game.GamePhysics; -import ru.deadsoftware.cavedroid.game.GameWorld; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import ru.deadsoftware.cavedroid.misc.Assets; public class Player extends Mob { diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java index e9f2e61..995c79e 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java @@ -4,7 +4,7 @@ import com.badlogic.gdx.math.Intersector; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; -import ru.deadsoftware.cavedroid.game.GameWorld; +import ru.deadsoftware.cavedroid.game.world.GameWorld; import ru.deadsoftware.cavedroid.game.mobs.Player; import java.io.Serializable; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameFluidsThread.java similarity index 99% rename from core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java rename to core/src/ru/deadsoftware/cavedroid/game/world/GameFluidsThread.java index c3cdd47..570b2c1 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameFluidsThread.java @@ -1,4 +1,4 @@ -package ru.deadsoftware.cavedroid.game; +package ru.deadsoftware.cavedroid.game.world; import com.badlogic.gdx.utils.TimeUtils; import ru.deadsoftware.cavedroid.game.mobs.MobsController; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java similarity index 95% rename from core/src/ru/deadsoftware/cavedroid/game/GameWorld.java rename to core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java index 3bf4981..18afcd4 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java @@ -1,8 +1,10 @@ -package ru.deadsoftware.cavedroid.game; +package ru.deadsoftware.cavedroid.game.world; import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.utils.TimeUtils; import kotlin.Pair; +import ru.deadsoftware.cavedroid.game.GameItems; +import ru.deadsoftware.cavedroid.game.GameScope; import ru.deadsoftware.cavedroid.game.mobs.FallingGravel; import ru.deadsoftware.cavedroid.game.mobs.FallingSand; import ru.deadsoftware.cavedroid.game.mobs.MobsController; @@ -45,7 +47,7 @@ public class GameWorld implements Disposable { if (isNewGame) { mWidth = DEFAULT_WIDTH; mHeight = DEFAULT_HEIGHT; - Pair maps = GameWorldGeneratorKt.generate(mWidth, mHeight, TimeUtils.millis()); + Pair maps = GameWorldGenerator.INSTANCE.generate(mWidth, mHeight, TimeUtils.millis()); mForeMap = maps.getFirst(); mBackMap = maps.getSecond(); mMobsController.getPlayer().respawn(this); @@ -75,11 +77,11 @@ public class GameWorld implements Disposable { return mHeight * 16f; } - int[][] getFullForeMap() { + public int[][] getFullForeMap() { return mForeMap; } - int[][] getFullBackMap() { + public int[][] getFullBackMap() { return mBackMap; } diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldGenerator.kt b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldGenerator.kt new file mode 100644 index 0000000..dd64c6a --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldGenerator.kt @@ -0,0 +1,57 @@ +package ru.deadsoftware.cavedroid.game.world + +import com.badlogic.gdx.utils.TimeUtils +import ru.deadsoftware.cavedroid.game.GameItems +import kotlin.math.abs +import kotlin.random.Random + +object GameWorldGenerator { + + 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) { + val previous = get(x - 1) + var d = random.nextInt(-5, 6).let { if (it !in -4..4) it / abs(it) else 0 } + + if (previous + d !in min..max) { d = -d } + if (lastIndex - x < abs(get(0) - previous) * 3) { + d = get(0).compareTo(previous).let { if (it != 0) it / abs(it) else 0 } + } + + set(x, get(x - 1) + d) + } + } + + /** + * Generates world of given width and height with given seed + * @param width world width + * @param height world height + * @param seed seed for random number generator + * @return pair of foreground and background layers + */ + fun generate(width: Int, height: Int, seed: Long = TimeUtils.millis()): Pair, Array> { + val random = Random(seed) + val foreMap = Array(width) { IntArray(height) } + val backMap = Array(width) { IntArray(width) } + val heightsMap = generateHeights(width, height / 2, height * 3 / 4, random) + + for (x in 0 until width) { + val xHeight = heightsMap[x] + + 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] + } + } + return Pair(foreMap, backMap) + } + +}