From 2e5cf197472693ffd66a7f1bb0bea2da98cf63e1 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Fri, 27 Sep 2019 01:11:36 +0700 Subject: [PATCH] Better fluid updater --- .../deadsoftware/cavedroid/game/GameProc.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index c2fca90..53b2b90 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -17,10 +17,11 @@ import ru.deadsoftware.cavedroid.misc.Assets; import java.io.Serializable; import java.util.ArrayList; -import java.util.Random; public class GameProc implements Serializable, Disposable { + private static final int FLUID_UPDATE_INTERVAL_MS = 100; + static boolean DO_UPD = false; static int UPD_X = -1, UPD_Y = -1; @@ -34,6 +35,7 @@ public class GameProc implements Serializable, Disposable { public ArrayList mobs; public ArrayList drops; + private long fluidLastUpdateTimestamp = 0; public boolean isTouchDown, isKeyDown; public int ctrlMode, touchDownX, touchDownY, touchDownBtn, keyDownCode; @@ -63,17 +65,24 @@ public class GameProc implements Serializable, Disposable { } maxCreativeScroll = GameItems.getItemsSize() / 8; - fluidThread = new Thread(() -> { - while (!fluidThread.isInterrupted()) { - fluidUpdater(); - } - }); + createFluidThread(); fluidThread.start(); GameSaver.save(this); } + private void createFluidThread() { + fluidThread = new Thread(() -> { + while (!fluidThread.isInterrupted()) { + if (System.currentTimeMillis() - fluidLastUpdateTimestamp > FLUID_UPDATE_INTERVAL_MS) { + fluidUpdater(); + fluidLastUpdateTimestamp = System.currentTimeMillis(); + } + } + }); + } + public void resetRenderer() { if (CaveGame.TOUCH) { renderer = new GameRenderer(320, @@ -388,10 +397,11 @@ public class GameProc implements Serializable, Disposable { } private void fluidUpdater() { + int midScreen = (int) (renderer.getCamX() + renderer.getWidth() / 2) / 16; for (int y = 0; y < world.getHeight(); y++) { - for (int x = (int) renderer.getCamX() / 16 - 1; - x < (int) (renderer.getCamX() + renderer.getWidth()) / 16 + 1; x++) { - updateFluids(x, y); + for (int x = 0; x < (int) (renderer.getWidth() / 2) / 16 + 1; x++) { + updateFluids(midScreen + x, y); + updateFluids(midScreen - x, y); } } } @@ -466,6 +476,11 @@ public class GameProc implements Serializable, Disposable { isTouchDown = false; } } + + if (fluidThread == null || !fluidThread.isAlive()) { + createFluidThread(); + fluidThread.start(); + } } @Override -- 2.29.2