From: fredboy Date: Mon, 15 Apr 2024 16:07:43 +0000 (+0700) Subject: Fix greedy fluids updater X-Git-Tag: alpha0.4.1~2 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=75809eb6447af4925b8e46ddb181152c6d2de47a;p=cavedroid.git Fix greedy fluids updater --- diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index 8c4fefb..d5a1a95 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -24,8 +24,6 @@ public class GameProc implements Disposable { mGamePhysics = gamePhysics; mGameInput = gameInput; mGameRenderer = gameRenderer; - - mGameWorld.startFluidsThread(); } public void update(float delta) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 272992a..cb3c2a5 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -59,7 +59,7 @@ public class Player extends Mob { public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { final float correctedAnimationDelta = mAnimDelta * delta; - if (mVelocity.x != 0 || Math.abs(Assets.playerSprite[0][2].getRotation()) > Math.abs(correctedAnimationDelta)) { + if (mVelocity.x != 0f || Math.abs(Assets.playerSprite[0][2].getRotation()) > Math.abs(correctedAnimationDelta)) { Assets.playerSprite[0][2].rotate(correctedAnimationDelta); Assets.playerSprite[1][2].rotate(-correctedAnimationDelta); Assets.playerSprite[0][3].rotate(-correctedAnimationDelta); diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameFluidsThread.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameFluidsThread.java index 570b2c1..7d80855 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameFluidsThread.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameFluidsThread.java @@ -4,12 +4,13 @@ import com.badlogic.gdx.utils.TimeUtils; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import java.util.Arrays; +import java.util.TimerTask; import static ru.deadsoftware.cavedroid.game.GameItems.*; -class GameFluidsThread extends Thread { +class GameFluidsThread extends TimerTask { - private static final int FLUID_UPDATE_INTERVAL_MS = 100; + public static final int FLUID_UPDATE_INTERVAL_MS = 100; private static final int FLUID_STATES = 5; private static final int[] WATER_IDS = {8, 60, 61, 62, 63}; @@ -144,10 +145,6 @@ class GameFluidsThread extends Thread { @Override public void run() { - while (!this.isInterrupted() && mMainThread.isAlive()) { - if (timeToUpdate()) { - fluidUpdater(); - } - } + fluidUpdater(); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java index 18afcd4..60a34e1 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java @@ -13,6 +13,8 @@ import ru.deadsoftware.cavedroid.game.objects.DropController; import javax.annotation.CheckForNull; import javax.inject.Inject; +import java.sql.Time; +import java.util.Timer; @GameScope public class GameWorld implements Disposable { @@ -23,6 +25,8 @@ public class GameWorld implements Disposable { private final DropController mDropController; private final MobsController mMobsController; + + private final Timer mGameFluidsTimer; private final GameFluidsThread mGameFluidsThread; private final int mWidth; @@ -59,6 +63,9 @@ public class GameWorld implements Disposable { } mGameFluidsThread = new GameFluidsThread(this, mMobsController, Thread.currentThread()); + + mGameFluidsTimer = new Timer(); + mGameFluidsTimer.scheduleAtFixedRate(mGameFluidsThread, 0, GameFluidsThread.FLUID_UPDATE_INTERVAL_MS); } public int getWidth() { @@ -245,18 +252,10 @@ public class GameWorld implements Disposable { } mShouldUpdate = false; } - - if (!mGameFluidsThread.isAlive()) { - mGameFluidsThread.start(); - } - } - - public void startFluidsThread() { - mGameFluidsThread.start(); } @Override public void dispose() { - mGameFluidsThread.interrupt(); + mGameFluidsTimer.cancel(); } } \ No newline at end of file