X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2FGameProc.java;h=c2fca908a889f7dff2591e2239fee7df1b64b609;hb=50346da46f787f84aeeb891630eea1463eb4d8de;hp=aa11931410fd72428ff93927d73f2b834e1b24b6;hpb=0f0a00cc4c8b1d80006682491179c41a285ef846;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index aa11931..c2fca90 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -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(); - mobs = new ArrayList(); + 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,25 +387,31 @@ 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) { - switch (GameItems.getItem(id).getType()) { - case 0: - if (!bg) world.placeToForeground(x, y, GameItems.getItem(id).getBlock()); - else world.placeToBackground(x, y, GameItems.getItem(id).getBlock()); - break; - case 2: - 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; - } - break; + 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; + } } } } @@ -407,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(); @@ -426,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; } } @@ -448,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(); + } }