diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java
index d404c43aca84b79aaca1bd7b5aac1d1db923238b..c3cdd47b355a5d2434f27c3df622dbb1ea669836 100644 (file)
package ru.deadsoftware.cavedroid.game;
package ru.deadsoftware.cavedroid.game;
+import com.badlogic.gdx.utils.TimeUtils;
+import ru.deadsoftware.cavedroid.game.mobs.MobsController;
+
import java.util.Arrays;
import java.util.Arrays;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
import static ru.deadsoftware.cavedroid.game.GameItems.*;
import static ru.deadsoftware.cavedroid.game.GameItems.*;
-public class GameFluidsThread extends Thread {
+class GameFluidsThread extends Thread {
- private static final int FLUID_UPDATE_INTERVAL_MS = 150;
+ private 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};
private static final int[] LAVA_IDS = {9, 64, 65, 66, 67};
private static final int FLUID_STATES = 5;
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);
private int getBlockState(int id) {
return isWater(id) ? Arrays.binarySearch(WATER_IDS, id) : Arrays.binarySearch(LAVA_IDS, id);
private int getNextBlockStateId(int id) {
int nextState = getNextBlockState(id);
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];
}
if (isWater(id)) {
return WATER_IDS[nextState];
}
}
private int id(int x, int y) {
}
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) {
}
private boolean sameFluid(int thisId, int thatId) {
private boolean drainFluid(int x, int y) {
if (getBlockState(id(x, y)) > 0) {
if (noFluidNearby(x, y)) {
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))) {
}
if (!isFluid(id(x, y))) {
- GP.world.setForeMap(x, y, 0);
+ mGameWorld.setForeMap(x, y, 0);
return true;
}
}
return true;
}
}
private void flowFluidTo(int thisId, int x, int y, int nextStateId) {
int thatId = id(x, y);
if (fluidCanFlowThere(thisId, thatId)) {
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) {
} else if (isWater(thisId) && isLava(thatId)) {
if (getBlockState(thatId) > 0) {
- GP.world.setForeMap(x, y, 4); //cobblestone
+ mGameWorld.setForeMap(x, y, 4); //cobblestone
} else {
} else {
- GP.world.setForeMap(x, y, 68); //obsidian
+ mGameWorld.setForeMap(x, y, 68); //obsidian
}
} else if (isLava(thisId) && isWater(thatId)) {
}
} else if (isLava(thisId) && isWater(thatId)) {
- GP.world.setForeMap(x, y, 1); //stone
+ mGameWorld.setForeMap(x, y, 1); //stone
}
}
}
}
}
private void updateFluids(int x, int y) {
}
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() {
flowFluid(x, y);
}
private void fluidUpdater() {
- int midScreen = (int) (GP.renderer.getCamX() + GP.renderer.getWidth() / 2) / 16;
- for (int y = 0; y < GP.world.getHeight(); y++) {
- for (int x = 0; x < (int) (GP.renderer.getWidth() / 2) / 16 + 1; 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);
}
}
}
updateFluids(midScreen + x, y);
updateFluids(midScreen - x, y);
}
}
}
+ private boolean timeToUpdate() {
+ if (TimeUtils.timeSinceMillis(mFluidLastUpdateTimestamp) >= FLUID_UPDATE_INTERVAL_MS) {
+ mFluidLastUpdateTimestamp = TimeUtils.millis();
+ return true;
+ }
+ return false;
+ }
+
@Override
public void run() {
@Override
public void run() {
- while (!this.isInterrupted()) {
- if (System.currentTimeMillis() - fluidLastUpdateTimestamp > FLUID_UPDATE_INTERVAL_MS) {
+ while (!this.isInterrupted() && mMainThread.isAlive()) {
+ if (timeToUpdate()) {
fluidUpdater();
fluidUpdater();
- fluidLastUpdateTimestamp = System.currentTimeMillis();
}
}
}
}
}
}