X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2FGameFluidsThread.java;h=c3cdd47b355a5d2434f27c3df622dbb1ea669836;hb=5b3244dceab39900a44eeb4d6108715c56735626;hp=8f4a891930bc570ce868899a99e965da9329d492;hpb=043e0557948f7691cfff71ad91debb5876148e97;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java index 8f4a891..c3cdd47 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java @@ -1,11 +1,13 @@ package ru.deadsoftware.cavedroid.game; +import com.badlogic.gdx.utils.TimeUtils; +import ru.deadsoftware.cavedroid.game.mobs.MobsController; + import java.util.Arrays; -import static ru.deadsoftware.cavedroid.GameScreen.GP; import static ru.deadsoftware.cavedroid.game.GameItems.*; -public class GameFluidsThread extends Thread { +class GameFluidsThread extends Thread { private static final int FLUID_UPDATE_INTERVAL_MS = 100; private static final int FLUID_STATES = 5; @@ -13,7 +15,20 @@ public class GameFluidsThread extends Thread { private static final int[] WATER_IDS = {8, 60, 61, 62, 63}; private static final int[] LAVA_IDS = {9, 64, 65, 66, 67}; - private long fluidLastUpdateTimestamp = 0; + private long mFluidLastUpdateTimestamp = 0; + + private final GameWorld mGameWorld; + private final MobsController mMobsController; + + private final Thread mMainThread; + + GameFluidsThread(GameWorld gameWorld, + MobsController mobsController, + Thread mainThread) { + mGameWorld = gameWorld; + mMobsController = mobsController; + mMainThread = mainThread; + } private int getBlockState(int id) { return isWater(id) ? Arrays.binarySearch(WATER_IDS, id) : Arrays.binarySearch(LAVA_IDS, id); @@ -32,7 +47,9 @@ public class GameFluidsThread extends Thread { private int getNextBlockStateId(int id) { int nextState = getNextBlockState(id); - if (nextState == -1) return 0; + if (nextState == -1) { + return 0; + } if (isWater(id)) { return WATER_IDS[nextState]; } @@ -40,7 +57,7 @@ public class GameFluidsThread extends Thread { } private int id(int x, int y) { - return GP.world.getForeMap(x, y); + return mGameWorld.getForeMap(x, y); } private boolean sameFluid(int thisId, int thatId) { @@ -56,10 +73,10 @@ public class GameFluidsThread extends Thread { private boolean drainFluid(int x, int y) { if (getBlockState(id(x, y)) > 0) { if (noFluidNearby(x, y)) { - GP.world.setForeMap(x, y, getNextBlockStateId(id(x, y))); + mGameWorld.setForeMap(x, y, getNextBlockStateId(id(x, y))); } if (!isFluid(id(x, y))) { - GP.world.setForeMap(x, y, 0); + mGameWorld.setForeMap(x, y, 0); return true; } } @@ -69,15 +86,15 @@ public class GameFluidsThread extends Thread { private void flowFluidTo(int thisId, int x, int y, int nextStateId) { int thatId = id(x, y); if (fluidCanFlowThere(thisId, thatId)) { - GP.world.setForeMap(x, y, nextStateId); + mGameWorld.setForeMap(x, y, nextStateId); } else if (isWater(thisId) && isLava(thatId)) { if (getBlockState(thatId) > 0) { - GP.world.setForeMap(x, y, 4); //cobblestone + mGameWorld.setForeMap(x, y, 4); //cobblestone } else { - GP.world.setForeMap(x, y, 68); //obsidian + mGameWorld.setForeMap(x, y, 68); //obsidian } } else if (isLava(thisId) && isWater(thatId)) { - GP.world.setForeMap(x, y, 1); //stone + mGameWorld.setForeMap(x, y, 1); //stone } } @@ -98,15 +115,19 @@ public class GameFluidsThread extends Thread { } private void updateFluids(int x, int y) { - if (!isFluid(id(x, y))) return; - if (drainFluid(x, y)) return; + if (!isFluid(id(x, y))) { + return; + } + if (drainFluid(x, y)) { + return; + } flowFluid(x, y); } private void fluidUpdater() { - int midScreen = (int) (GP.renderer.getCamX() + GP.renderer.getWidth() / 2) / 16; - for (int y = GP.world.getHeight() - 1; y >= 0; y--) { - for (int x = 0; x <= GP.world.getWidth() / 2; x++) { + int midScreen = (int) mMobsController.getPlayer().x / 16; + for (int y = mGameWorld.getHeight() - 1; y >= 0; y--) { + for (int x = 0; x <= mGameWorld.getWidth() / 2; x++) { updateFluids(midScreen + x, y); updateFluids(midScreen - x, y); } @@ -114,8 +135,8 @@ public class GameFluidsThread extends Thread { } private boolean timeToUpdate() { - if (System.currentTimeMillis() - fluidLastUpdateTimestamp >= FLUID_UPDATE_INTERVAL_MS) { - fluidLastUpdateTimestamp = System.currentTimeMillis(); + if (TimeUtils.timeSinceMillis(mFluidLastUpdateTimestamp) >= FLUID_UPDATE_INTERVAL_MS) { + mFluidLastUpdateTimestamp = TimeUtils.millis(); return true; } return false; @@ -123,7 +144,7 @@ public class GameFluidsThread extends Thread { @Override public void run() { - while (!this.isInterrupted()) { + while (!this.isInterrupted() && mMainThread.isAlive()) { if (timeToUpdate()) { fluidUpdater(); }