From 894d9f1babeff18c9cbb5f83206ff2c549de0bdb Mon Sep 17 00:00:00 2001 From: fredboy Date: Sat, 20 Apr 2024 04:33:47 +0700 Subject: [PATCH] Add update blocks logic actions --- .../cavedroid/game/GameComponent.java | 5 +- .../cavedroid/game/GameInput.java | 6 +- .../deadsoftware/cavedroid/game/GameProc.java | 29 +++++--- .../cavedroid/game/GameRenderer.java | 3 +- ...tionUtils.kt => CommonBlockActionUtils.kt} | 6 ++ .../game/actions/UpdateBlockActionsModule.kt | 44 ++++++++++++ ...tionsModule.kt => UseItemActionsModule.kt} | 2 +- .../actions/updateblock/IUpdateBlockAction.kt | 7 ++ .../actions/updateblock/UpdateGrassAction.kt | 23 ++++++ .../actions/updateblock/UpdateGravelAction.kt | 28 ++++++++ .../updateblock/UpdateRequiresBlockAction.kt | 21 ++++++ .../actions/updateblock/UpdateSandAction.kt | 27 +++++++ .../cavedroid/game/world/GameWorld.java | 71 +------------------ .../GameWorldBlocksLogicControllerTask.kt | 59 +++++++++++++++ ...> GameWorldFluidsLogicControllerTask.java} | 12 ++-- 15 files changed, 253 insertions(+), 90 deletions(-) rename core/src/ru/deadsoftware/cavedroid/game/actions/{PlaceBlockActionUtils.kt => CommonBlockActionUtils.kt} (71%) create mode 100644 core/src/ru/deadsoftware/cavedroid/game/actions/UpdateBlockActionsModule.kt rename core/src/ru/deadsoftware/cavedroid/game/actions/{GameActionsModule.kt => UseItemActionsModule.kt} (97%) create mode 100644 core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/IUpdateBlockAction.kt create mode 100644 core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGrassAction.kt create mode 100644 core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGravelAction.kt create mode 100644 core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateRequiresBlockAction.kt create mode 100644 core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateSandAction.kt create mode 100644 core/src/ru/deadsoftware/cavedroid/game/world/GameWorldBlocksLogicControllerTask.kt rename core/src/ru/deadsoftware/cavedroid/game/world/{GameFluidsThread.java => GameWorldFluidsLogicControllerTask.java} (92%) diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java b/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java index 8b1b7ee..81f4b88 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java @@ -2,10 +2,11 @@ package ru.deadsoftware.cavedroid.game; import dagger.Component; import ru.deadsoftware.cavedroid.MainComponent; -import ru.deadsoftware.cavedroid.game.actions.GameActionsModule; +import ru.deadsoftware.cavedroid.game.actions.UpdateBlockActionsModule; +import ru.deadsoftware.cavedroid.game.actions.UseItemActionsModule; @GameScope -@Component(dependencies = MainComponent.class, modules = { GameModule.class, GameActionsModule.class }) +@Component(dependencies = MainComponent.class, modules = { GameModule.class, UseItemActionsModule.class, UpdateBlockActionsModule.class }) public interface GameComponent { GameProc getGameProc(); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index e26b06b..17d6ad1 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -8,7 +8,7 @@ import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.utils.TimeUtils; import com.google.common.collect.Range; import ru.deadsoftware.cavedroid.MainConfig; -import ru.deadsoftware.cavedroid.game.actions.PlaceBlockActionUtilsKt; +import ru.deadsoftware.cavedroid.game.actions.CommonBlockActionUtilsKt; import ru.deadsoftware.cavedroid.game.actions.useitem.IUseItemAction; import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.mobs.MobsController; @@ -212,9 +212,9 @@ public class GameInput { @CheckForNull final String actionKey = item.getActionKey(); if (item.isBlock()) { if (!bg) { - PlaceBlockActionUtilsKt.placeToForegroundAction(mUseItemActionMap, item, x, y); + CommonBlockActionUtilsKt.placeToForegroundAction(mUseItemActionMap, item, x, y); } else { - PlaceBlockActionUtilsKt.placeToBackgroundAction(mUseItemActionMap, item, x, y); + CommonBlockActionUtilsKt.placeToBackgroundAction(mUseItemActionMap, item, x, y); } } else if (actionKey != null) { final IUseItemAction useItemAction = mUseItemActionMap.get(actionKey); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index f063f7a..139a31b 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -1,8 +1,10 @@ package ru.deadsoftware.cavedroid.game; import com.badlogic.gdx.utils.Disposable; +import com.badlogic.gdx.utils.Timer; import ru.deadsoftware.cavedroid.game.mobs.MobsController; -import ru.deadsoftware.cavedroid.game.world.GameWorld; +import ru.deadsoftware.cavedroid.game.world.GameWorldBlocksLogicControllerTask; +import ru.deadsoftware.cavedroid.game.world.GameWorldFluidsLogicControllerTask; import javax.inject.Inject; @@ -11,24 +13,34 @@ public class GameProc implements Disposable { public static final int MAX_CREATIVE_SCROLL = GameItems.getItemsSize() / 8; - private final GameWorld mGameWorld; private final GamePhysics mGamePhysics; private final GameInput mGameInput; private final GameRenderer mGameRenderer; private final MobsController mMobsController; + private final GameWorldFluidsLogicControllerTask mGameWorldFluidsLogicControllerTask; + private final GameWorldBlocksLogicControllerTask mGameWorldBlocksLogicControllerTask; + + private final Timer mWorldLogicTimer = new Timer(); @Inject - public GameProc(GameWorld gameWorld, - GamePhysics gamePhysics, + public GameProc(GamePhysics gamePhysics, GameInput gameInput, GameRenderer gameRenderer, - MobsController mobsController + MobsController mobsController, + GameWorldFluidsLogicControllerTask gameWorldFluidsLogicControllerTask, + GameWorldBlocksLogicControllerTask gameWorldBlocksLogicControllerTask ) { - mGameWorld = gameWorld; mGamePhysics = gamePhysics; mGameInput = gameInput; mGameRenderer = gameRenderer; mMobsController = mobsController; + mGameWorldFluidsLogicControllerTask = gameWorldFluidsLogicControllerTask; + mGameWorldBlocksLogicControllerTask = gameWorldBlocksLogicControllerTask; + + mWorldLogicTimer.scheduleTask(gameWorldFluidsLogicControllerTask, 0, + GameWorldFluidsLogicControllerTask.FLUID_UPDATE_INTERVAL_SEC); + mWorldLogicTimer.scheduleTask(gameWorldBlocksLogicControllerTask, 0, + GameWorldBlocksLogicControllerTask.WORLD_BLOCKS_LOGIC_UPDATE_INTERVAL_SEC); } public void setPlayerGameMode(int gameMode) { @@ -38,12 +50,13 @@ public class GameProc implements Disposable { public void update(float delta) { mGamePhysics.update(delta); mGameInput.update(); - mGameWorld.update(); mGameRenderer.render(delta); } @Override public void dispose() { - mGameWorld.dispose(); + mWorldLogicTimer.stop(); + mGameWorldFluidsLogicControllerTask.cancel(); + mGameWorldBlocksLogicControllerTask.cancel(); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index 20401ac..ca9cf8b 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 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 com.badlogic.gdx.scenes.scene2d.utils.ScissorStack; @@ -370,7 +371,7 @@ public class GameRenderer extends Renderer { @Override public void render(float delta) { - int fps = (int) (1 / delta); + int fps = MathUtils.ceil(1 / delta); updateCameraPosition(); mGameInput.moveCursor(this); diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/PlaceBlockActionUtils.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/CommonBlockActionUtils.kt similarity index 71% rename from core/src/ru/deadsoftware/cavedroid/game/actions/PlaceBlockActionUtils.kt rename to core/src/ru/deadsoftware/cavedroid/game/actions/CommonBlockActionUtils.kt index 52b97d0..4309430 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/PlaceBlockActionUtils.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/CommonBlockActionUtils.kt @@ -1,6 +1,8 @@ package ru.deadsoftware.cavedroid.game.actions import com.badlogic.gdx.Gdx +import ru.deadsoftware.cavedroid.game.actions.updateblock.IUpdateBlockAction +import ru.deadsoftware.cavedroid.game.actions.updateblock.UpdateRequiresBlockAction import ru.deadsoftware.cavedroid.game.actions.useitem.IUseItemAction import ru.deadsoftware.cavedroid.game.actions.useitem.PlaceBlockItemToBackgroundAction import ru.deadsoftware.cavedroid.game.actions.useitem.PlaceBlockItemToForegroundAction @@ -17,3 +19,7 @@ fun Map.placeToBackgroundAction(item: Item, x: Int, y: I get(PlaceBlockItemToBackgroundAction.ACTION_KEY)?.perform(item, x, y) ?: Gdx.app.error(TAG, "action place_background_block not found") } + +fun Map.getRequiresBlockAction(): IUpdateBlockAction { + return requireNotNull(get(UpdateRequiresBlockAction.ACTION_KEY)) { "action requires_block not found" } +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/UpdateBlockActionsModule.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/UpdateBlockActionsModule.kt new file mode 100644 index 0000000..be84127 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/UpdateBlockActionsModule.kt @@ -0,0 +1,44 @@ +package ru.deadsoftware.cavedroid.game.actions + +import dagger.Binds +import dagger.Module +import dagger.multibindings.IntoMap +import dagger.multibindings.StringKey +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.actions.updateblock.* + +@Module +class UpdateBlockActionsModule { + + @Binds + @IntoMap + @StringKey(UpdateSandAction.BLOCK_KEY) + @GameScope + fun bindUpdateSandAction(action: UpdateSandAction): IUpdateBlockAction { + return action; + } + + @Binds + @IntoMap + @StringKey(UpdateGravelAction.BLOCK_KEY) + @GameScope + fun bindUpdateGravelAction(action: UpdateGravelAction): IUpdateBlockAction { + return action; + } + + @Binds + @IntoMap + @StringKey(UpdateRequiresBlockAction.ACTION_KEY) + @GameScope + fun bindUpdateRequiresBlockAction(action: UpdateRequiresBlockAction): IUpdateBlockAction { + return action; + } + + @Binds + @IntoMap + @StringKey(UpdateGrassAction.BLOCK_KEY) + @GameScope + fun bindUpdateGrassAction(action: UpdateGrassAction): IUpdateBlockAction { + return action; + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/GameActionsModule.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/UseItemActionsModule.kt similarity index 97% rename from core/src/ru/deadsoftware/cavedroid/game/actions/GameActionsModule.kt rename to core/src/ru/deadsoftware/cavedroid/game/actions/UseItemActionsModule.kt index 672c062..a54e4a7 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/GameActionsModule.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/UseItemActionsModule.kt @@ -8,7 +8,7 @@ import ru.deadsoftware.cavedroid.game.GameScope import ru.deadsoftware.cavedroid.game.actions.useitem.* @Module -class GameActionsModule { +class UseItemActionsModule { @Binds @IntoMap diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/IUpdateBlockAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/IUpdateBlockAction.kt new file mode 100644 index 0000000..9cc4aea --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/IUpdateBlockAction.kt @@ -0,0 +1,7 @@ +package ru.deadsoftware.cavedroid.game.actions.updateblock + +interface IUpdateBlockAction { + + fun update(x: Int, y: Int) + +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGrassAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGrassAction.kt new file mode 100644 index 0000000..a50c541 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGrassAction.kt @@ -0,0 +1,23 @@ +package ru.deadsoftware.cavedroid.game.actions.updateblock + +import ru.deadsoftware.cavedroid.game.GameItems +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.world.GameWorld +import javax.inject.Inject + +@GameScope +class UpdateGrassAction @Inject constructor( + private val gameWorld: GameWorld, +) : IUpdateBlockAction { + + override fun update(x: Int, y: Int) { + val blockOnTop = gameWorld.getForeMapBlock(x, y - 1) + if (blockOnTop.collision || blockOnTop.fluid) { + gameWorld.setForeMap(x, y, GameItems.getBlockId("dirt")) + } + } + + companion object { + const val BLOCK_KEY = "grass" + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGravelAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGravelAction.kt new file mode 100644 index 0000000..53fcd62 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGravelAction.kt @@ -0,0 +1,28 @@ +package ru.deadsoftware.cavedroid.game.actions.updateblock + +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 +import ru.deadsoftware.cavedroid.game.world.GameWorld +import javax.inject.Inject + +@GameScope +class UpdateGravelAction @Inject constructor( + private val gameWorld: GameWorld, + private val mobsController: MobsController, +) : IUpdateBlockAction { + + override fun update(x: Int, y: Int) { + val shouldFall = gameWorld.getForeMapBlock(x, y + 1).collision.not() + + if (shouldFall) { + gameWorld.setForeMap(x, y, 0) + mobsController.addMob(FallingGravel(x * 16f, y * 16f)) + } + } + + companion object { + const val BLOCK_KEY = "gravel" + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateRequiresBlockAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateRequiresBlockAction.kt new file mode 100644 index 0000000..2e27373 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateRequiresBlockAction.kt @@ -0,0 +1,21 @@ +package ru.deadsoftware.cavedroid.game.actions.updateblock + +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.world.GameWorld +import javax.inject.Inject + +@GameScope +class UpdateRequiresBlockAction @Inject constructor( + private val gameWorld: GameWorld, +) : IUpdateBlockAction { + + override fun update(x: Int, y: Int) { + if (!gameWorld.getForeMapBlock(x, y + 1).collision) { + gameWorld.destroyForeMap(x, y) + } + } + + companion object { + const val ACTION_KEY = "requires_block" + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateSandAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateSandAction.kt new file mode 100644 index 0000000..26fb978 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateSandAction.kt @@ -0,0 +1,27 @@ +package ru.deadsoftware.cavedroid.game.actions.updateblock + +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.mobs.FallingSand +import ru.deadsoftware.cavedroid.game.mobs.MobsController +import ru.deadsoftware.cavedroid.game.world.GameWorld +import javax.inject.Inject + +@GameScope +class UpdateSandAction @Inject constructor( + private val gameWorld: GameWorld, + private val mobsController: MobsController, +) : IUpdateBlockAction { + + override fun update(x: Int, y: Int) { + val shouldFall = gameWorld.getForeMapBlock(x, y + 1).collision.not() + + if (shouldFall) { + gameWorld.setForeMap(x, y, 0) + mobsController.addMob(FallingSand(x * 16f, y * 16f)) + } + } + + companion object { + const val BLOCK_KEY = "sand" + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java index 43fc164..d3e56c1 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java @@ -1,12 +1,8 @@ package ru.deadsoftware.cavedroid.game.world; -import com.badlogic.gdx.utils.Disposable; -import com.badlogic.gdx.utils.Timer; 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; import ru.deadsoftware.cavedroid.game.model.world.generator.WorldGeneratorConfig; import ru.deadsoftware.cavedroid.game.objects.Block; @@ -16,25 +12,16 @@ import javax.annotation.CheckForNull; import javax.inject.Inject; @GameScope -public class GameWorld implements Disposable { - - private static final int UPDATE_RANGE = 16; +public class GameWorld { private final DropController mDropController; private final MobsController mMobsController; - private final Timer mGameFluidsTimer; - private final GameFluidsThread mGameFluidsThread; - private final int mWidth; private final int mHeight; private final int[][] mForeMap; private final int[][] mBackMap; - private boolean mShouldUpdate; - private int mUpdateX; - private int mUpdateY; - @Inject public GameWorld(DropController dropController, MobsController mobsController, @@ -59,11 +46,6 @@ public class GameWorld implements Disposable { mWidth = mForeMap.length; mHeight = mForeMap[0].length; } - - mGameFluidsThread = new GameFluidsThread(this, mMobsController); - - mGameFluidsTimer = new Timer(); - mGameFluidsTimer.scheduleTask(mGameFluidsThread, 0, GameFluidsThread.FLUID_UPDATE_INTERVAL_SEC); } public int getWidth() { @@ -181,9 +163,6 @@ public class GameWorld implements Disposable { } else if (GameItems.isSlab(value) && getForeMap(x, y) == value) { placeSlab(x, y, value); } - mUpdateX = x - 8; - mUpdateY = y - 8; - mShouldUpdate = true; } public void placeToBackground(int x, int y, int value) { @@ -208,52 +187,4 @@ public class GameWorld implements Disposable { } placeToBackground(x, y, 0); } - - private void updateBlock(int x, int y) { - if (getForeMap(x, y) == 10) { - if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) { - setForeMap(x, y, 0); - mMobsController.addMob(new FallingSand(x * 16, y * 16)); - updateBlock(x, y - 1); - } - } - - if (getForeMap(x, y) == 11) { - if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) { - setForeMap(x, y, 0); - mMobsController.addMob(new FallingGravel(x * 16, y * 16)); - updateBlock(x, y - 1); - } - } - - if (hasForeAt(x, y) && getForeMapBlock(x, y).requiresBlock()) { - if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) { - destroyForeMap(x, y); - updateBlock(x, y - 1); - } - } - - if (getForeMap(x, y) == 2) { - if (hasForeAt(x, y - 1) && (getForeMapBlock(x, y - 1).hasCollision() || - GameItems.isFluid(getForeMap(x, y - 1)))) { - setForeMap(x, y, 3); - } - } - } - - public void update() { - if (mShouldUpdate) { - for (int y = mUpdateY; y < mUpdateY + UPDATE_RANGE; y++) { - for (int x = mUpdateX; x < mUpdateX + UPDATE_RANGE; x++) { - updateBlock(x, y); - } - } - mShouldUpdate = false; - } - } - - @Override - public void dispose() { - mGameFluidsThread.cancel(); - } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldBlocksLogicControllerTask.kt b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldBlocksLogicControllerTask.kt new file mode 100644 index 0000000..20f992a --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldBlocksLogicControllerTask.kt @@ -0,0 +1,59 @@ +package ru.deadsoftware.cavedroid.game.world + +import com.badlogic.gdx.Gdx +import com.badlogic.gdx.utils.TimeUtils +import com.badlogic.gdx.utils.Timer.Task +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.actions.getRequiresBlockAction +import ru.deadsoftware.cavedroid.game.actions.updateblock.IUpdateBlockAction +import ru.deadsoftware.cavedroid.game.mobs.MobsController +import javax.inject.Inject + +@GameScope +class GameWorldBlocksLogicControllerTask @Inject constructor( + private val gameWorld: GameWorld, + private val updateBlockActions: Map, + private val mobsController: MobsController, +) : Task() { + + private var currentRelativeChunk = 0 + + private fun getChunkStart(): Int { + val playerX = mobsController.player.mapX + val playerChunk = playerX / CHUNK_WIDTH + val currentChunk = playerChunk - CHUNKS / 2 + currentRelativeChunk + + return currentChunk * 16 + } + + private fun updateBlock(x: Int, y: Int) { + val block = gameWorld.getForeMapBlock(x, y) + val blockKey = block.key + val action = updateBlockActions[blockKey] + ?: updateBlockActions.getRequiresBlockAction().takeIf { block.requiresBlock } + + action?.update(x, y) + } + + override fun run() { + val startX = getChunkStart() + + for (y in gameWorld.height downTo 0) { + for (x in startX ..< startX + CHUNK_WIDTH) { + updateBlock(x, y) + } + } + + currentRelativeChunk = (currentRelativeChunk + 1) % CHUNKS + } + + companion object { + private const val TAG = "GameWorldBlocksLogicControllerTask" + + private const val CHUNK_WIDTH = 16 + private const val CHUNKS = 3 + + const val WORLD_BLOCKS_LOGIC_UPDATE_INTERVAL_SEC = .1f + } + +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameFluidsThread.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldFluidsLogicControllerTask.java similarity index 92% rename from core/src/ru/deadsoftware/cavedroid/game/world/GameFluidsThread.java rename to core/src/ru/deadsoftware/cavedroid/game/world/GameWorldFluidsLogicControllerTask.java index 50fcef1..d996cf4 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameFluidsThread.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldFluidsLogicControllerTask.java @@ -1,13 +1,16 @@ package ru.deadsoftware.cavedroid.game.world; import com.badlogic.gdx.utils.Timer; +import ru.deadsoftware.cavedroid.game.GameScope; import ru.deadsoftware.cavedroid.game.mobs.MobsController; +import javax.inject.Inject; import java.util.Arrays; import static ru.deadsoftware.cavedroid.game.GameItems.*; -class GameFluidsThread extends Timer.Task { +@GameScope +public class GameWorldFluidsLogicControllerTask extends Timer.Task { public static final float FLUID_UPDATE_INTERVAL_SEC = 0.1f; private static final int FLUID_STATES = 5; @@ -15,13 +18,12 @@ class GameFluidsThread extends Timer.Task { private static final int[] WATER_IDS = {8, 60, 61, 62, 63}; private static final int[] LAVA_IDS = {9, 64, 65, 66, 67}; - private long mFluidLastUpdateTimestamp = 0; - private final GameWorld mGameWorld; private final MobsController mMobsController; - GameFluidsThread(GameWorld gameWorld, - MobsController mobsController) { + @Inject + GameWorldFluidsLogicControllerTask(GameWorld gameWorld, + MobsController mobsController) { mGameWorld = gameWorld; mMobsController = mobsController; } -- 2.29.2