DEADSOFTWARE

Stop fluid updater when game exited
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameProc.java
index 7364e5967c767fcb3cc9be56d27400080c95a7a5..c2fca908a889f7dff2591e2239fee7df1b64b609 100644 (file)
@@ -2,6 +2,7 @@ package ru.deadsoftware.cavedroid.game;
 
 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.Input;
+import com.badlogic.gdx.utils.Disposable;
 import com.badlogic.gdx.utils.TimeUtils;
 import ru.deadsoftware.cavedroid.CaveGame;
 import ru.deadsoftware.cavedroid.GameScreen;
@@ -16,12 +17,15 @@ import ru.deadsoftware.cavedroid.misc.Assets;
 
 import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.Random;
 
-public class GameProc implements Serializable {
+public class GameProc implements Serializable, Disposable {
 
     static boolean DO_UPD = false;
     static int UPD_X = -1, UPD_Y = -1;
 
+    private transient Thread fluidThread;
+
     public transient GameWorld world;
     public transient GameRenderer renderer;
     public transient GamePhysics physics;
@@ -43,8 +47,8 @@ public class GameProc implements Serializable {
         world = new GameWorld();
         world.generate(1024, 256);
         player = new Player(gameMode);
-        drops = new ArrayList<Drop>();
-        mobs = new ArrayList<Mob>();
+        drops = new ArrayList<>();
+        mobs = new ArrayList<>();
         for (int i = 0; i < 16; i++) {
             mobs.add(new Pig(i * 256, 196 * 16));
         }
@@ -58,6 +62,15 @@ public class GameProc implements Serializable {
                     480 * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
         }
         maxCreativeScroll = GameItems.getItemsSize() / 8;
+
+        fluidThread = new Thread(() -> {
+            while (!fluidThread.isInterrupted()) {
+                fluidUpdater();
+            }
+        });
+
+        fluidThread.start();
+
         GameSaver.save(this);
     }
 
@@ -339,7 +352,6 @@ public class GameProc implements Serializable {
             } else if (GameItems.isWater(world.getForeMap(x, y + 1))) {
                 world.setForeMap(x, y + 1, 1);
             }
-            return;
         }
     }
 
@@ -362,7 +374,7 @@ public class GameProc implements Serializable {
 
         if (world.getForeMap(x, y) > 0 && GameItems.getBlock(world.getForeMap(x, y)).requiresBlock()) {
             if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) {
-                world.destroyForeMap(x, y, this);
+                world.destroyForeMap(x, y);
                 updateBlock(x, y - 1);
             }
         }
@@ -375,10 +387,32 @@ public class GameProc implements Serializable {
         }
     }
 
+    private void fluidUpdater() {
+        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);
+            }
+        }
+    }
+
     void useItem(int x, int y, int id, boolean bg) {
-        if (id > 0 && GameItems.getItem(id).getType() == 0) {
-            if (!bg) world.placeToForeground(x, y, GameItems.getItem(id).getBlock());
-            else world.placeToBackground(x, y, GameItems.getItem(id).getBlock());
+        if (id > 0) {
+            if (GameItems.getItem(id).isBlock()) {
+                if (!bg) world.placeToForeground(x, y, GameItems.getBlockIdByItemId(id));
+                else world.placeToBackground(x, y, GameItems.getBlockIdByItemId(id));
+            } else {
+                switch (id) {
+                    case 65:
+                        world.placeToForeground(x, y, 8);
+                        player.inv[player.invSlot] = 64;
+                        break;
+                    case 66:
+                        world.placeToForeground(x, y, 9);
+                        player.inv[player.invSlot] = 64;
+                        break;
+                }
+            }
         }
     }
 
@@ -391,12 +425,6 @@ public class GameProc implements Serializable {
             DO_UPD = false;
         }
 
-        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);
-            }
-        }
-
         physics.update(delta);
         moveCursor();
         checkCursorBounds();
@@ -410,12 +438,12 @@ public class GameProc implements Serializable {
                     blockDmg++;
                     if (world.getForeMap(curX, curY) > 0) {
                         if (blockDmg >= GameItems.getBlock(world.getForeMap(curX, curY)).getHp()) {
-                            world.destroyForeMap(curX, curY, this);
+                            world.destroyForeMap(curX, curY);
                             blockDmg = 0;
                         }
                     } else if (world.getBackMap(curX, curY) > 0) {
                         if (blockDmg >= GameItems.getBlock(world.getBackMap(curX, curY)).getHp()) {
-                            world.destroyBackMap(curX, curY, this);
+                            world.destroyBackMap(curX, curY);
                             blockDmg = 0;
                         }
                     }
@@ -432,12 +460,16 @@ public class GameProc implements Serializable {
                 useItem(curX, curY, player.inv[player.invSlot], true);
                 isTouchDown = false;
             } else if (touchDownY < Assets.invBar.getRegionHeight() &&
-                    touchDownX > renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 &&
-                    touchDownX < renderer.getWidth() / 2 + Assets.invBar.getRegionWidth() / 2) {
+                    touchDownX > renderer.getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 &&
+                    touchDownX < renderer.getWidth() / 2 + (float) Assets.invBar.getRegionWidth() / 2) {
                 CaveGame.STATE = AppState.GAME_CREATIVE_INV;
                 isTouchDown = false;
             }
         }
     }
 
+    @Override
+    public void dispose() {
+        fluidThread.interrupt();
+    }
 }