From 2948fcd9c40ebf588ef7d0e7cd8dd34ecaa1f9b2 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Fri, 27 Sep 2019 23:50:14 +0700 Subject: [PATCH] Some refactor and javadocs --- .../ru/deadsoftware/cavedroid/CaveGame.java | 14 +- .../ru/deadsoftware/cavedroid/GameScreen.java | 63 ++--- .../cavedroid/game/GameFluidsThread.java | 6 +- .../cavedroid/game/GameInput.java | 75 +++--- .../cavedroid/game/GamePhysics.java | 11 +- .../deadsoftware/cavedroid/game/GameProc.java | 247 +++++++++--------- .../cavedroid/game/GameRenderer.java | 59 +++-- .../cavedroid/game/GameSaver.java | 16 +- .../cavedroid/game/GameWorld.java | 34 +-- .../cavedroid/game/mobs/FallingGravel.java | 10 +- .../cavedroid/game/mobs/FallingSand.java | 9 + .../deadsoftware/cavedroid/game/mobs/Mob.java | 85 +++++- .../deadsoftware/cavedroid/game/mobs/Pig.java | 16 +- .../cavedroid/game/objects/Block.java | 17 +- .../cavedroid/game/objects/Drop.java | 6 +- .../cavedroid/game/objects/Player.java | 21 +- .../cavedroid/menu/MenuRenderer.java | 83 +++--- .../cavedroid/misc/ControlMode.java | 6 +- .../cavedroid/misc/InputHandlerGame.java | 13 +- .../cavedroid/misc/InputHandlerMenu.java | 6 +- .../cavedroid/misc/states/AppState.java | 19 +- .../cavedroid/misc/states/GameState.java | 5 +- .../cavedroid/misc/states/MenuState.java | 4 + 23 files changed, 473 insertions(+), 352 deletions(-) diff --git a/core/src/ru/deadsoftware/cavedroid/CaveGame.java b/core/src/ru/deadsoftware/cavedroid/CaveGame.java index ca11186..57704d9 100644 --- a/core/src/ru/deadsoftware/cavedroid/CaveGame.java +++ b/core/src/ru/deadsoftware/cavedroid/CaveGame.java @@ -2,15 +2,19 @@ package ru.deadsoftware.cavedroid; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; -import ru.deadsoftware.cavedroid.misc.AppState; +import ru.deadsoftware.cavedroid.misc.states.AppState; +import ru.deadsoftware.cavedroid.misc.states.GameState; +import ru.deadsoftware.cavedroid.misc.states.MenuState; public class CaveGame extends Game { public static final String VERSION = "alpha 0.4"; - public static String GAME_FOLDER; - public static AppState STATE; + public static AppState APP_STATE; + public static GameState GAME_STATE; + public static MenuState MENU_STATE; + public static String GAME_FOLDER; public static boolean TOUCH; public CaveGame(String gameFolder) { @@ -20,7 +24,9 @@ public class CaveGame extends Game { public CaveGame(String gameFolder, boolean touch) { GAME_FOLDER = gameFolder; TOUCH = touch; - STATE = AppState.MENU_MAIN; + APP_STATE = AppState.MENU; + MENU_STATE = MenuState.MAIN; + GAME_STATE = GameState.PLAY; } @Override diff --git a/core/src/ru/deadsoftware/cavedroid/GameScreen.java b/core/src/ru/deadsoftware/cavedroid/GameScreen.java index 4717818..f741dbd 100644 --- a/core/src/ru/deadsoftware/cavedroid/GameScreen.java +++ b/core/src/ru/deadsoftware/cavedroid/GameScreen.java @@ -7,24 +7,27 @@ import ru.deadsoftware.cavedroid.game.GameProc; import ru.deadsoftware.cavedroid.game.GameSaver; import ru.deadsoftware.cavedroid.menu.MenuRenderer; import ru.deadsoftware.cavedroid.misc.*; +import ru.deadsoftware.cavedroid.misc.states.AppState; +import ru.deadsoftware.cavedroid.misc.states.GameState; +import ru.deadsoftware.cavedroid.misc.states.MenuState; public class GameScreen implements Screen { + public static GameProc GP; + public static Renderer RENDERER; + public static int FPS; public static boolean SHOW_DEBUG = true; public static boolean SHOW_MAP = false; public static int NEW_GAME_MODE = 0; - public static GameProc GP; - - private Renderer renderer; private MenuRenderer menuRenderer; public GameScreen() { Assets.load(); GameItems.load(); menuRenderer = new MenuRenderer(CaveGame.TOUCH ? 320 : 480); - renderer = menuRenderer; + RENDERER = menuRenderer; Gdx.input.setInputProcessor(new InputHandlerMenu(menuRenderer)); } @@ -36,8 +39,8 @@ public class GameScreen implements Screen { return Gdx.graphics.getHeight(); } - private void game(float delta) { - GP.update(delta); + private void game() { + GP.update(); } private void menu() { @@ -50,58 +53,48 @@ public class GameScreen implements Screen { @Override public void render(float delta) { FPS = (int) (1 / delta); - switch (CaveGame.STATE) { - case GAME_PLAY: - case GAME_CREATIVE_INV: - game(delta); + switch (CaveGame.APP_STATE) { + case GAME: + game(); break; - case MENU_MAIN: + case MENU: menu(); break; - case NEW_GAME: - GP = new GameProc(); - GP.initGame(NEW_GAME_MODE); - renderer = GP.renderer; - Gdx.input.setInputProcessor(new InputHandlerGame()); - CaveGame.STATE = AppState.GAME_PLAY; - break; - - case LOAD_GAME: + case LOAD: GP = GameSaver.load(); - renderer = GP.renderer; + GP.resetRenderer(); + RENDERER = GP.renderer; Gdx.input.setInputProcessor(new InputHandlerGame()); - CaveGame.STATE = AppState.GAME_PLAY; + CaveGame.APP_STATE = AppState.GAME; + CaveGame.GAME_STATE = GameState.PLAY; break; - case SAVE_GAME: + case SAVE: GameSaver.save(GP); - CaveGame.STATE = AppState.MENU_MAIN; - break; - - case GOTO_MENU: + CaveGame.APP_STATE = AppState.MENU; + CaveGame.MENU_STATE = MenuState.MAIN; GP.dispose(); menuRenderer = new MenuRenderer(CaveGame.TOUCH ? 320 : 480); - renderer = menuRenderer; + RENDERER = menuRenderer; Gdx.input.setInputProcessor(new InputHandlerMenu(menuRenderer)); break; } - renderer.render(); + RENDERER.render(); } @Override public void resize(int width, int height) { - switch (CaveGame.STATE) { - case MENU_MAIN: + switch (CaveGame.APP_STATE) { + case MENU: menuRenderer = new MenuRenderer(CaveGame.TOUCH ? 320 : 480); Gdx.input.setInputProcessor(new InputHandlerMenu(menuRenderer)); - renderer = menuRenderer; + RENDERER = menuRenderer; break; - case GAME_PLAY: - case GAME_CREATIVE_INV: + case GAME: GP.resetRenderer(); - renderer = GP.renderer; + RENDERER = GP.renderer; break; } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java index 8f4a891..57c7470 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java @@ -1,5 +1,7 @@ package ru.deadsoftware.cavedroid.game; +import com.badlogic.gdx.utils.TimeUtils; + import java.util.Arrays; import static ru.deadsoftware.cavedroid.GameScreen.GP; @@ -114,8 +116,8 @@ public class GameFluidsThread extends Thread { } private boolean timeToUpdate() { - if (System.currentTimeMillis() - fluidLastUpdateTimestamp >= FLUID_UPDATE_INTERVAL_MS) { - fluidLastUpdateTimestamp = System.currentTimeMillis(); + if (TimeUtils.timeSinceMillis(fluidLastUpdateTimestamp) >= FLUID_UPDATE_INTERVAL_MS) { + fluidLastUpdateTimestamp = TimeUtils.millis(); return true; } return false; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 85d736d..13b2fbf 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -5,15 +5,17 @@ import com.badlogic.gdx.utils.TimeUtils; import ru.deadsoftware.cavedroid.CaveGame; import ru.deadsoftware.cavedroid.GameScreen; import ru.deadsoftware.cavedroid.game.mobs.Pig; -import ru.deadsoftware.cavedroid.misc.AppState; +import ru.deadsoftware.cavedroid.misc.ControlMode; +import ru.deadsoftware.cavedroid.misc.states.AppState; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.states.GameState; import static ru.deadsoftware.cavedroid.GameScreen.GP; public class GameInput { private boolean checkSwim() { - return GameItems.isFluid(GP.world.getForeMap(GP.player.getMapX(), GP.player.getMapY())); + return GameItems.isFluid(GP.world.getForeMap(GP.player.getMapX(), GP.player.getLowerMapY())); } private boolean insideCreativeInv(int screenX, int screenY) { @@ -24,7 +26,7 @@ public class GameInput { } private void wasdPressed(int keycode) { - if (GP.ctrlMode == 0 || !CaveGame.TOUCH) { + if (GP.controlMode == ControlMode.WALK || !CaveGame.TOUCH) { switch (keycode) { case Input.Keys.A: GP.player.mov.x = -GamePhysics.PL_SPEED; @@ -65,8 +67,7 @@ public class GameInput { } else switch (keycode) { case Input.Keys.ALT_LEFT: if (CaveGame.TOUCH) { - GP.ctrlMode++; - if (GP.ctrlMode > 1) GP.ctrlMode = 0; + GP.controlMode = GP.controlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK; } break; @@ -88,15 +89,18 @@ public class GameInput { break; case Input.Keys.E: - if (CaveGame.STATE == AppState.GAME_PLAY) switch (GP.player.gameMode) { - case 0: - //TODO survival inv - break; - case 1: - CaveGame.STATE = AppState.GAME_CREATIVE_INV; - break; + if (CaveGame.GAME_STATE == GameState.PLAY){ + switch (GP.player.gameMode) { + case 0: + //TODO survival inv + break; + case 1: + CaveGame.GAME_STATE = GameState.CREATIVE_INV; + break; + } + } else { + CaveGame.GAME_STATE = GameState.PLAY; } - else CaveGame.STATE = AppState.GAME_PLAY; break; case Input.Keys.G: @@ -109,7 +113,8 @@ public class GameInput { case Input.Keys.ESCAPE: case Input.Keys.BACK: - CaveGame.STATE = AppState.GOTO_MENU; + CaveGame.APP_STATE = AppState.SAVE; + CaveGame.GAME_STATE = GameState.PAUSE; break; case Input.Keys.F1: @@ -152,26 +157,24 @@ public class GameInput { GP.isKeyDown = false; } if (GP.isTouchDown) { - if (CaveGame.STATE == AppState.GAME_CREATIVE_INV && insideCreativeInv(screenX, screenY)) { + if (CaveGame.GAME_STATE == GameState.CREATIVE_INV && insideCreativeInv(screenX, screenY)) { int ix = (int) (screenX - (GP.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 + 8)) / 18; int iy = (int) (screenY - (GP.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 + 18)) / 18; int item = GP.creativeScroll * 8 + (ix + iy * 8); if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) item = -1; if (item >= 0 && item < GameItems.getItemsSize()) { - for (int i = 8; i > 0; i--) { - GP.player.inv[i] = GP.player.inv[i - 1]; - } - GP.player.inv[0] = item; + System.arraycopy(GP.player.inventory, 0, GP.player.inventory, 1, 8); + GP.player.inventory[0] = item; } - } else if (CaveGame.STATE == AppState.GAME_CREATIVE_INV) { - CaveGame.STATE = AppState.GAME_PLAY; + } else if (CaveGame.GAME_STATE == GameState.CREATIVE_INV) { + CaveGame.GAME_STATE = GameState.PLAY; } else if (screenY < Assets.invBar.getRegionHeight() && - screenX > GP.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 && - screenX < GP.renderer.getWidth() / 2 + Assets.invBar.getRegionWidth() / 2) { - GP.player.invSlot = (int) ((screenX - (GP.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2)) / 20); + screenX > GP.renderer.getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 && + screenX < GP.renderer.getWidth() / 2 + (float) Assets.invBar.getRegionWidth() / 2) { + GP.player.slot = (int) ((screenX - (GP.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2)) / 20); } else if (button == Input.Buttons.RIGHT) { GP.useItem(GP.curX, GP.curY, - GP.player.inv[GP.player.invSlot], false); + GP.player.inventory[GP.player.slot], false); } else if (button == Input.Buttons.LEFT) { GP.blockDmg = 0; } @@ -180,30 +183,30 @@ public class GameInput { } public void touchDragged(int screenX, int screenY) { - if (CaveGame.STATE == AppState.GAME_CREATIVE_INV && Math.abs(screenY - GP.touchDownY) > 16) { + if (CaveGame.GAME_STATE == GameState.CREATIVE_INV && Math.abs(screenY - GP.touchDownY) > 16) { if (insideCreativeInv(screenX, screenY)) { GP.creativeScroll -= (screenY - GP.touchDownY) / 16; GP.touchDownX = screenX; GP.touchDownY = screenY; if (GP.creativeScroll < 0) GP.creativeScroll = 0; - if (GP.creativeScroll > GP.maxCreativeScroll) - GP.creativeScroll = GP.maxCreativeScroll; + if (GP.creativeScroll > GameProc.MAX_CREATIVE_SCROLL) + GP.creativeScroll = GameProc.MAX_CREATIVE_SCROLL; } } } public void scrolled(int amount) { - switch (CaveGame.STATE) { - case GAME_PLAY: - GP.player.invSlot += amount; - if (GP.player.invSlot < 0) GP.player.invSlot = 8; - if (GP.player.invSlot > 8) GP.player.invSlot = 0; + switch (CaveGame.GAME_STATE) { + case PLAY: + GP.player.slot += amount; + if (GP.player.slot < 0) GP.player.slot = 8; + if (GP.player.slot > 8) GP.player.slot = 0; break; - case GAME_CREATIVE_INV: + case CREATIVE_INV: GP.creativeScroll += amount; if (GP.creativeScroll < 0) GP.creativeScroll = 0; - if (GP.creativeScroll > GP.maxCreativeScroll) - GP.creativeScroll = GP.maxCreativeScroll; + if (GP.creativeScroll > GameProc.MAX_CREATIVE_SCROLL) + GP.creativeScroll = GameProc.MAX_CREATIVE_SCROLL; break; } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 8678404..25709bd 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -17,7 +17,7 @@ class GamePhysics { static final int PL_SPEED = 2; - private Vector2 gravity = new Vector2(0, .9f);; + private Vector2 gravity = new Vector2(0, .9f); private boolean checkJump(Rectangle rect, int dir) { int bl; @@ -106,7 +106,7 @@ class GamePhysics { while (checkColl(mob.getRect())) mob.pos.y += d; mob.mov.y = 0; if (mob.getType() > 0) { - GP.world.setForeMap(mob.getMapX(), mob.getMapY(), mob.getType()); + GP.world.setForeMap(mob.getMapX(), mob.getMiddleMapY(), mob.getType()); mob.kill(); } } else { @@ -138,7 +138,7 @@ class GamePhysics { pl.pos.x += pl.mov.x * (pl.flyMode ? 1.5f : 1) * (GameItems.isFluid(getBlock(pl.getRect())) && !pl.flyMode ? .8f : 1); mobXColl(pl); - if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.getDir()) && !pl.flyMode && pl.canJump && pl.mov.x != 0) { + if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.getDirection()) && !pl.flyMode && pl.canJump && pl.mov.x != 0) { pl.mov.add(0, -8); pl.canJump = false; } @@ -158,14 +158,13 @@ class GamePhysics { mob.pos.x += mob.mov.x; mobXColl(mob); - if (checkJump(mob.getRect(), mob.getDir()) && mob.canJump && mob.mov.x != 0) { + if (checkJump(mob.getRect(), mob.getDirection()) && mob.canJump && mob.mov.x != 0) { mob.mov.add(0, -8); mob.canJump = false; } } - void update(float delta) { - //TODO use delta time + void update() { for (Iterator it = GP.drops.iterator(); it.hasNext(); ) { Drop drop = it.next(); dropPhy(drop); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index 8ceb7cf..b3b0d3f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -9,122 +9,116 @@ import ru.deadsoftware.cavedroid.GameScreen; import ru.deadsoftware.cavedroid.game.mobs.FallingGravel; import ru.deadsoftware.cavedroid.game.mobs.FallingSand; import ru.deadsoftware.cavedroid.game.mobs.Mob; -import ru.deadsoftware.cavedroid.game.mobs.Pig; import ru.deadsoftware.cavedroid.game.objects.Drop; import ru.deadsoftware.cavedroid.game.objects.Player; -import ru.deadsoftware.cavedroid.misc.AppState; +import ru.deadsoftware.cavedroid.misc.ControlMode; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.states.GameState; import java.io.Serializable; import java.util.ArrayList; +import static ru.deadsoftware.cavedroid.game.GameItems.*; + public class GameProc implements Serializable, Disposable { + + static final int MAX_CREATIVE_SCROLL = getItemsSize() / 8; + + private static final int WORLD_WIDTH = 1024; + private static final int WORLD_HEIGHT = 256; + private static final int UPD_RANGE = 16; + static boolean DO_UPD = false; static int UPD_X = -1, UPD_Y = -1; private transient GameFluidsThread fluidThread; - public transient GameWorld world; public transient GameRenderer renderer; - public transient GamePhysics physics; + transient GamePhysics physics; + public ControlMode controlMode; public Player player; public ArrayList mobs; - public ArrayList drops; + ArrayList drops; public boolean isTouchDown, isKeyDown; - public int ctrlMode, touchDownX, touchDownY, touchDownBtn, keyDownCode; - public long touchDownTime; + public int touchDownX, touchDownY, touchDownBtn, keyDownCode; + long touchDownTime; int curX, curY; - int creativeScroll, maxCreativeScroll; + int creativeScroll; int blockDmg = 0; - public void initGame(int gameMode) { - world = new GameWorld(); - world.generate(1024, 256); + public GameProc(int gameMode) { + world = new GameWorld(WORLD_WIDTH, WORLD_HEIGHT); player = new Player(gameMode); drops = new ArrayList<>(); mobs = new ArrayList<>(); - for (int i = 0; i < 16; i++) { - mobs.add(new Pig(i * 256, 196 * 16)); - } physics = new GamePhysics(); + controlMode = CaveGame.TOUCH ? ControlMode.WALK : ControlMode.CURSOR; + resetRenderer(); + startFluidThread(); + } + public void resetRenderer() { int scale = CaveGame.TOUCH ? 320 : 480; - if (CaveGame.TOUCH) ctrlMode = 1; renderer = new GameRenderer(scale, scale * GameScreen.getHeight() / GameScreen.getWidth()); + } - maxCreativeScroll = GameItems.getItemsSize() / 8; - - createFluidThread(); + private void startFluidThread() { + fluidThread = new GameFluidsThread(); fluidThread.start(); - - GameSaver.save(this); } - private void createFluidThread() { - fluidThread = new GameFluidsThread(); + private boolean isNotAutoselectable(int x, int y) { + return (!world.hasForeAt(x, y) || !world.getForeMapBlock(x, y).hasCollision()); } - public void resetRenderer() { - if (CaveGame.TOUCH) { - renderer = new GameRenderer(320, - 320 * ((float) GameScreen.getHeight() / GameScreen.getWidth())); - } else { - renderer = new GameRenderer(480, - 480 * ((float) GameScreen.getHeight() / GameScreen.getWidth())); + private void checkCursorBounds() { + if (curY < 0) { + curY = 0; + } else if (curY >= world.getHeight()) { + curY = world.getHeight() - 1; } - } - private boolean isAutoselectable(int x, int y) { - return (world.getForeMap(x, y) > 0 && - GameItems.getBlock(world.getForeMap(x, y)).hasCollision()); + if (controlMode == ControlMode.CURSOR) { + if (curX * 16 + 8 < player.pos.x + player.getWidth() / 2) { + player.setDir(0); + } else { + player.setDir(1); + } + } } private void moveCursor() { - int pastX = curX, pastY = curY; - if (ctrlMode == 0 && CaveGame.TOUCH) { + int pastX = curX; + int pastY = curY; + + if (controlMode == ControlMode.WALK && CaveGame.TOUCH) { curX = player.getMapX(); - if (player.getDir() == 0) curX--; - else curX++; - curY = (int) (player.pos.y + player.getWidth()) / 16; - if (!isAutoselectable(curX, curY)) { + curY = (int) (player.pos.y + player.getWidth() / 16); + while (curY - pastY < 2 && isNotAutoselectable(curX, curY)) { curY++; } - if (!isAutoselectable(curX, curY)) { - curY++; - } - if (!isAutoselectable(curX, curY)) { - if (player.getDir() == 0) curX++; - else curX--; + if (!isNotAutoselectable(curX, curY)) { + curX += player.looksLeft() ? 1 : -1; } } else if (!CaveGame.TOUCH) { - curX = (int) (Gdx.input.getX() * - (renderer.getWidth() / GameScreen.getWidth()) + renderer.getCamX()) / 16; - curY = (int) (Gdx.input.getY() * - (renderer.getHeight() / GameScreen.getHeight()) + renderer.getCamY()) / 16; - if ((Gdx.input.getX() * - (renderer.getWidth() / GameScreen.getWidth()) + renderer.getCamX()) < 0) - curX--; + curX = (int) (Gdx.input.getX() * (renderer.getWidth() / GameScreen.getWidth()) + renderer.getCamX()) / 16; + curY = (int) (Gdx.input.getY() * (renderer.getHeight() / GameScreen.getHeight()) + renderer.getCamY()) / 16; + if (curX < 0) curX--; } - if (pastX != curX || pastY != curY) blockDmg = 0; - } - private void checkCursorBounds() { - if (curY < 0) curY = 0; - if (curY >= world.getHeight()) curY = world.getHeight() - 1; - if (ctrlMode == 1) { - if (curX * 16 + 8 < player.pos.x + player.getWidth() / 2) - player.setDir(0); - if (curX * 16 + 8 > player.pos.x + player.getWidth() / 2) - player.setDir(1); + if (pastX != curX || pastY != curY) { + blockDmg = 0; } + + checkCursorBounds(); } private void updateBlock(int x, int y) { if (world.getForeMap(x, y) == 10) { - if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { + if (!world.hasForeAt(x, y + 1) || !world.getForeMapBlock(x, y + 1).hasCollision()) { world.setForeMap(x, y, 0); mobs.add(new FallingSand(x * 16, y * 16)); updateBlock(x, y - 1); @@ -132,103 +126,112 @@ public class GameProc implements Serializable, Disposable { } if (world.getForeMap(x, y) == 11) { - if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { + if (!world.hasForeAt(x, y + 1) || !world.getForeMapBlock(x, y + 1).hasCollision()) { world.setForeMap(x, y, 0); mobs.add(new FallingGravel(x * 16, y * 16)); updateBlock(x, y - 1); } } - 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()) { + if (world.hasForeAt(x, y) && world.getForeMapBlock(x, y).requiresBlock()) { + if (!world.hasForeAt(x, y + 1) || !world.getForeMapBlock(x, y + 1).hasCollision()) { world.destroyForeMap(x, y); updateBlock(x, y - 1); } } if (world.getForeMap(x, y) == 2) { - if (world.getForeMap(x, y - 1) > 0 && (GameItems.getBlock(world.getForeMap(x, y - 1)).hasCollision() || - GameItems.isFluid(world.getForeMap(x, y - 1)))) { + if (world.hasForeAt(x, y - 1) && (world.getForeMapBlock(x, y - 1).hasCollision() || + isFluid(world.getForeMap(x, y - 1)))) { world.setForeMap(x, y, 3); } } } + private void blockUpdater() { + if (DO_UPD) { + for (int y = UPD_Y; y < UPD_Y + UPD_RANGE; y++) { + for (int x = UPD_X; x < UPD_X + UPD_RANGE; x++) { + updateBlock(x, y); + } + } + DO_UPD = false; + } + } + void useItem(int x, int y, int id, boolean bg) { + String key = getItem(id).isBlock() ? getBlockKey(id) : getItemKey(id); 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)); + if (getItem(id).isBlock()) { + if (!bg) { + world.placeToForeground(x, y, getBlockIdByItemId(id)); + } else { + world.placeToBackground(x, y, getBlockIdByItemId(id)); + } } else { - switch (id) { - case 65: - world.placeToForeground(x, y, 8); - player.inv[player.invSlot] = 64; + switch (key) { + case "bucket_water": + world.placeToForeground(x, y, getBlockId("water")); + player.inventory[player.slot] = getItemId("bucket_empty"); break; - case 66: - world.placeToForeground(x, y, 9); - player.inv[player.invSlot] = 64; + case "bucket_lava": + world.placeToForeground(x, y, getBlockId("lava")); + player.inventory[player.slot] = getItemId("bucket_empty"); break; } } } } - public void update(float delta) { - if (DO_UPD) { - for (int y = UPD_Y; y < UPD_Y + 16; y++) - for (int x = UPD_X; x < UPD_X + 16; x++) { - updateBlock(x, y); - } - DO_UPD = false; - } - - physics.update(delta); - moveCursor(); - checkCursorBounds(); - - if (isTouchDown && touchDownBtn == Input.Buttons.LEFT) { - if ((world.getForeMap(curX, curY) > 0 && GameItems.getBlock(world.getForeMap(curX, curY)).getHp() >= 0) || - (world.getForeMap(curX, curY) == 0 && - world.getBackMap(curX, curY) > 0 && - GameItems.getBlock(world.getBackMap(curX, curY)).getHp() >= 0)) { - if (player.gameMode == 0) { - blockDmg++; - if (world.getForeMap(curX, curY) > 0) { - if (blockDmg >= GameItems.getBlock(world.getForeMap(curX, curY)).getHp()) { - 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); - blockDmg = 0; - } + private void pressLMB() { + if ((world.hasForeAt(curX, curY) && world.getForeMapBlock(curX, curY).getHp() >= 0) || + (!world.hasForeAt(curX, curY) && world.hasBackAt(curX, curY) && + world.getBackMapBlock(curX, curY).getHp() >= 0)) { + if (player.gameMode == 0) { + blockDmg++; + if (world.hasForeAt(curX, curY)) { + if (blockDmg >= world.getForeMapBlock(curX, curY).getHp()) { + world.destroyForeMap(curX, curY); + blockDmg = 0; } - } else { - if (world.getForeMap(curX, curY) > 0) world.placeToForeground(curX, curY, 0); - else if (world.getBackMap(curX, curY) > 0) world.placeToBackground(curX, curY, 0); - isTouchDown = false; + } else if (world.hasBackAt(curX, curY)) { + if (blockDmg >= world.getBackMapBlock(curX, curY).getHp()) { + world.destroyBackMap(curX, curY); + blockDmg = 0; + } + } + } else { + if (world.hasForeAt(curX, curY)) { + world.placeToForeground(curX, curY, 0); + } else if (world.hasBackAt(curX, curY)) { + world.placeToBackground(curX, curY, 0); } + isTouchDown = false; } } + } - if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) { - if (touchDownBtn == Input.Buttons.RIGHT) { - useItem(curX, curY, player.inv[player.invSlot], true); - isTouchDown = false; - } else if (touchDownY < Assets.invBar.getRegionHeight() && + private void holdMB() { + if (touchDownBtn == Input.Buttons.RIGHT) { + useItem(curX, curY, player.inventory[player.slot], true); + isTouchDown = false; + } else if (touchDownBtn == Input.Buttons.LEFT) { + if (touchDownY < Assets.invBar.getRegionHeight() && touchDownX > renderer.getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 && touchDownX < renderer.getWidth() / 2 + (float) Assets.invBar.getRegionWidth() / 2) { - CaveGame.STATE = AppState.GAME_CREATIVE_INV; + CaveGame.GAME_STATE= GameState.CREATIVE_INV; isTouchDown = false; } } + } - if (fluidThread == null || !fluidThread.isAlive()) { - createFluidThread(); - fluidThread.start(); - } + public void update() { + physics.update(); + moveCursor(); + + if (isTouchDown && touchDownBtn == Input.Buttons.LEFT) pressLMB(); + if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) holdMB(); + if (fluidThread == null || !fluidThread.isAlive()) startFluidThread(); } @Override diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index 252e77f..c128d85 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -9,6 +9,7 @@ import ru.deadsoftware.cavedroid.GameScreen; import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.objects.Drop; import ru.deadsoftware.cavedroid.misc.Assets; +import ru.deadsoftware.cavedroid.misc.ControlMode; import ru.deadsoftware.cavedroid.misc.Renderer; import static ru.deadsoftware.cavedroid.GameScreen.GP; @@ -40,15 +41,15 @@ public class GameRenderer extends Renderer { private void drawBlock(int x, int y, boolean drawBG) { if (drawBG) { - if ((GP.world.getForeMap(x, y) == 0 || GameItems.getBlock(GP.world.getForeMap(x, y)).isTransparent()) - && GP.world.getBackMap(x, y) > 0) { - spriter.draw(GameItems.getBlock(GP.world.getBackMap(x, y)).getTex(), drawX(x), drawY(y)); - if (GP.world.getForeMap(x, y) == 0 && x == GP.curX && y == GP.curY) + if ((!GP.world.hasForeAt(x, y) || GP.world.getForeMapBlock(x, y).isTransparent()) + && GP.world.hasBackAt(x, y)) { + spriter.draw(GP.world.getBackMapBlock(x, y).getTex(), drawX(x), drawY(y)); + if (!GP.world.hasForeAt(x, y) && x == GP.curX && y == GP.curY) drawWreck(GP.world.getBackMap(GP.curX, GP.curY)); } } - if (GP.world.getForeMap(x, y) > 0 && GameItems.getBlock(GP.world.getForeMap(x, y)).isBackground() == drawBG) { - spriter.draw(GameItems.getBlock(GP.world.getForeMap(x, y)).getTex(), drawX(x), drawY(y)); + if (GP.world.hasForeAt(x, y) && GP.world.getForeMapBlock(x, y).isBackground() == drawBG) { + spriter.draw(GP.world.getForeMapBlock(x, y).getTex(), drawX(x), drawY(y)); if (x == GP.curX && y == GP.curY) drawWreck(GP.world.getForeMap(GP.curX, GP.curY)); } @@ -74,8 +75,8 @@ public class GameRenderer extends Renderer { shaper.setColor(0f, 0f, 0f, .5f); for (int y = minY; y < maxY; y++) { for (int x = minX; x < maxX; x++) { - if ((GP.world.getForeMap(x, y) == 0 || GameItems.getBlock(GP.world.getForeMap(x, y)).isTransparent()) - && GP.world.getBackMap(x, y) > 0) + if ((!GP.world.hasForeAt(x, y) || GP.world.getForeMapBlock(x, y).isTransparent()) + && GP.world.hasBackAt(x, y)) shaper.rect(drawX(x), drawY(y), 16, 16); } } @@ -108,7 +109,7 @@ public class GameRenderer extends Renderer { float y = getHeight() / 2 - (float) Assets.creativeInv.getRegionHeight() / 2; spriter.draw(Assets.creativeInv, x, y); spriter.draw(Assets.creativeScr, x + 156, - y + 18 + (GP.creativeScroll * (72f / GP.maxCreativeScroll))); + y + 18 + (GP.creativeScroll * (72f / GameProc.MAX_CREATIVE_SCROLL))); for (int i = GP.creativeScroll * 8; i < GP.creativeScroll * 8 + 40; i++) { if (i > 0 && i < GameItems.getItemsSize()) if (GameItems.getItem(i).isBlock()) { @@ -122,12 +123,12 @@ public class GameRenderer extends Renderer { } } for (int i = 0; i < 9; i++) { - if (GP.player.inv[i] > 0) - if (GameItems.getItem(GP.player.inv[i]).isBlock()) { - spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(GP.player.inv[i])).getTex(), + if (GP.player.inventory[i] > 0) + if (GameItems.getItem(GP.player.inventory[i]).isBlock()) { + spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(GP.player.inventory[i])).getTex(), x + 8 + i * 18, y + Assets.creativeInv.getRegionHeight() - 24); } else { - spriter.draw(GameItems.getItem(GP.player.inv[i]).getTex(), + spriter.draw(GameItems.getItem(GP.player.inventory[i]).getTex(), x + 8 + i * 18, y + Assets.creativeInv.getRegionHeight() - 24); } } @@ -135,29 +136,29 @@ public class GameRenderer extends Renderer { } private void drawGUI() { - if (GP.world.getForeMap(GP.curX, GP.curY) > 0 || - GP.world.getBackMap(GP.curX, GP.curY) > 0 || - GP.ctrlMode == 1 || + if (GP.world.hasForeAt(GP.curX, GP.curY) || + GP.world.hasBackAt(GP.curX, GP.curY) || + GP.controlMode == ControlMode.CURSOR || !CaveGame.TOUCH) spriter.draw(Assets.guiCur, GP.curX * 16 - getCamX(), GP.curY * 16 - getCamY()); spriter.draw(Assets.invBar, getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2, 0); for (int i = 0; i < 9; i++) { - if (GP.player.inv[i] > 0) { - if (GameItems.getItem(GP.player.inv[i]).isBlock()) { - spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(GP.player.inv[i])).getTex(), + if (GP.player.inventory[i] > 0) { + if (GameItems.getItem(GP.player.inventory[i]).isBlock()) { + spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(GP.player.inventory[i])).getTex(), getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 + 3 + i * 20, 3); } else { - spriter.draw(GameItems.getItem(GP.player.inv[i]).getTex(), + spriter.draw(GameItems.getItem(GP.player.inventory[i]).getTex(), getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 + 3 + i * 20, 3); } } } spriter.draw(Assets.invBarCur, - getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 - 1 + 20 * GP.player.invSlot, + getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 - 1 + 20 * GP.player.slot, -1); } @@ -169,7 +170,7 @@ public class GameRenderer extends Renderer { spriter.draw(Assets.touchLMB, getWidth() - 52, getHeight() - 26); spriter.draw(Assets.touchRMB, getWidth() - 26, getHeight() - 26); spriter.draw(Assets.touchMode, 78, getHeight() - 26); - if (GP.ctrlMode == 1) { + if (GP.controlMode == ControlMode.CURSOR) { spriter.draw(Assets.shade, 83, getHeight() - 21); } } @@ -188,11 +189,11 @@ public class GameRenderer extends Renderer { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); spriter.begin(); - switch (CaveGame.STATE) { - case GAME_PLAY: + switch (CaveGame.GAME_STATE) { + case PLAY: drawGamePlay(); break; - case GAME_CREATIVE_INV: + case CREATIVE_INV: drawGamePlay(); drawCreative(); break; @@ -202,20 +203,20 @@ public class GameRenderer extends Renderer { spriter.end(); - if(GameScreen.SHOW_MAP) { + if (GameScreen.SHOW_MAP) { //DRAW MAP shaper.begin(ShapeRenderer.ShapeType.Filled); shaper.setColor(Color.LIGHT_GRAY); shaper.rect(0, 0, GP.world.getWidth(), 128); for (int y = 128; y < 256; y++) { for (int x = 0; x < getWidth(); x++) { - if (GP.world.getForeMap(x, y) > 0 || GP.world.getBackMap(x, y) > 0) { + if (GP.world.hasForeAt(x, y) || GP.world.hasBackAt(x, y)) { if (GameItems.isWater(GP.world.getForeMap(x, y))) { shaper.setColor(Color.BLUE); } else if (GameItems.isLava(GP.world.getForeMap(x, y))) { shaper.setColor(Color.RED); } else { - if (GP.world.getForeMap(x, y) > 0) { + if (GP.world.hasForeAt(x, y)) { shaper.setColor(Color.BLACK); } else { shaper.setColor(Color.DARK_GRAY); @@ -241,7 +242,7 @@ public class GameRenderer extends Renderer { drawString("Mobs: " + GP.mobs.size(), 0, 50); drawString("Drops: " + GP.drops.size(), 0, 60); drawString("Block: " + GameItems.getBlockKey(GP.world.getForeMap(GP.curX, GP.curY)), 0, 70); - drawString("Hand: " + GameItems.getItemKey(GP.player.inv[GP.player.invSlot]), 0, 80); + drawString("Hand: " + GameItems.getItemKey(GP.player.inventory[GP.player.slot]), 0, 80); drawString("Game mode: " + GP.player.gameMode, 0, 90); } spriter.end(); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java b/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java index edc29e3..0d796b8 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java @@ -2,6 +2,7 @@ package ru.deadsoftware.cavedroid.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; +import org.jetbrains.annotations.NotNull; import ru.deadsoftware.cavedroid.CaveGame; import java.io.*; @@ -11,11 +12,12 @@ public class GameSaver { private static final int VERSION = 0; + @NotNull private static byte[] intToBytes(int i) { return ByteBuffer.allocate(4).putInt(i).array(); } - private static void saveMap(FileHandle file, int[][] map) throws IOException { + private static void saveMap(@NotNull FileHandle file, @NotNull int[][] map) throws IOException { int rl, bl; int width = map.length; int height = map[0].length; @@ -26,12 +28,12 @@ public class GameSaver { for (int y = 0; y < height; y++) { bl = map[0][y]; rl = 0; - for (int x = 0; x < width; x++) { - if (map[x][y] != bl) { + for (int[] ints : map) { + if (ints[y] != bl) { out.write(intToBytes(rl)); out.write(intToBytes(bl)); rl = 0; - bl = map[x][y]; + bl = ints[y]; } rl++; } @@ -42,7 +44,8 @@ public class GameSaver { out.close(); } - private static int[][] loadMap(FileHandle file) throws Exception { + @NotNull + private static int[][] loadMap(@NotNull FileHandle file) throws Exception { int[][] map; int ver, width, height; int rl, bl; @@ -73,8 +76,7 @@ public class GameSaver { if (VERSION == ver) gameProc = (GameProc) in.readObject(); else throw new Exception("version mismatch"); in.close(); - gameProc.world = new GameWorld(); - gameProc.world.setMaps( + gameProc.world = new GameWorld( loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav")), loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav")) ); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java index 9c71cfc..e921b98 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java @@ -1,16 +1,34 @@ package ru.deadsoftware.cavedroid.game; +import org.jetbrains.annotations.NotNull; import ru.deadsoftware.cavedroid.game.objects.Block; import ru.deadsoftware.cavedroid.game.objects.Drop; import static ru.deadsoftware.cavedroid.GameScreen.GP; +@SuppressWarnings("WeakerAccess") public class GameWorld { private int WIDTH, HEIGHT; private int[][] foreMap; private int[][] backMap; + GameWorld(int width, int height) { + WIDTH = width; + HEIGHT = height; + WorldGen.genWorld(WIDTH, HEIGHT); + foreMap = WorldGen.getForeMap(); + backMap = WorldGen.getBackMap(); + WorldGen.clear(); + } + + GameWorld(@NotNull int[][] foreMap, @NotNull int[][] backMap) { + this.foreMap = foreMap.clone(); + this.backMap = backMap.clone(); + WIDTH = foreMap.length; + HEIGHT = foreMap[0].length; + } + public int getWidth() { return WIDTH; } @@ -147,20 +165,4 @@ public class GameWorld { placeToBackground(x, y, 0); } - public void generate(int w, int h) { - WIDTH = w; - HEIGHT = h; - WorldGen.genWorld(WIDTH, HEIGHT); - foreMap = WorldGen.getForeMap(); - backMap = WorldGen.getBackMap(); - WorldGen.clear(); - } - - void setMaps(int[][] foreMap, int[][] backMap) { - this.foreMap = foreMap.clone(); - this.backMap = backMap.clone(); - WIDTH = foreMap.length; - HEIGHT = foreMap[0].length; - } - } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java index b47b600..f6149ea 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java @@ -5,9 +5,17 @@ import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavedroid.game.GameItems; import ru.deadsoftware.cavedroid.misc.Assets; +/** + * Falling gravel is actually a mob, that spawns in place of gravel when there is no block under it, + * falls down to the next block and becomes a block of gravel again. + */ public class FallingGravel extends Mob { - ^ + /** + * Creates a FallingGravel mob at coordinates + * @param x X in pixels + * @param y Y in pixels + */ public FallingGravel(float x, float y) { super(x, y, 16, 16, 0); mov = new Vector2(0, 1); diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java index cfd5f9b..b1777c4 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java @@ -5,8 +5,17 @@ import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavedroid.game.GameItems; import ru.deadsoftware.cavedroid.misc.Assets; +/** + * Falling sand is actually a mob, that spawns in place of gravel when there is no block under it, + * falls down to the next block and becomes a block of sand again. + */ public class FallingSand extends Mob { + /** + * Creates a FallingSand mob at coordinates + * @param x X in pixels + * @param y Y in pixels + */ public FallingSand(float x, float y) { super(x, y, 16, 16, 0); mov = new Vector2(0, 1); diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 3b7edae..bcab586 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -6,21 +6,37 @@ import com.badlogic.gdx.math.Vector2; import java.io.Serializable; +/** + * Mob class. + */ public abstract class Mob implements Serializable { - public boolean flyMode; + public static final int LEFT = 0; + public static final int RIGHT = 1; + private float width, height; private int dir; + public boolean flyMode; public Vector2 pos; public Vector2 mov; private boolean dead; + protected int animDelta = 6; public boolean canJump; - protected int anim, animDelta = 6; - - protected Mob(float x, float y, float width, float height, int dir, boolean player) { + int anim; + + /** + * + * @param x in pixels + * @param y in pixels + * @param width in pixels + * @param height in pixels + * @param dir integer representing a direction where 0 is left and 1 is right. + * You should use {@link #LEFT} and {@link #RIGHT} constants + */ + public Mob(float x, float y, float width, float height, int dir) { pos = new Vector2(x, y); mov = new Vector2(0, 0); this.width = width; @@ -31,18 +47,38 @@ public abstract class Mob implements Serializable { this.dir = dir; } - protected Mob(float x, float y, float width, float height, int dir) { - this(x, y, width, height, dir, false); - } - + /** + * + * @return The X coordinate of a mob in blocks + */ public int getMapX() { return (int) (pos.x + (getWidth() / 2)) / 16; } - public int getMapY() { + /** + * + * @return The Y coordinate of mob's upper edge in blocks + */ + public int getUpperMapY() { + return (int) (pos.y / 16); + } + + /** + * + * @return The Y coordinate if mob's vertical center in blocks + */ + public int getMiddleMapY() { return (int) (pos.y + (getHeight() / 2)) / 16; } + /** + * + * @return The Y coordinate of mob's legs in blocks + */ + public int getLowerMapY() { + return (int) (pos.y + getHeight()) / 16; + } + public float getWidth() { return width; } @@ -51,22 +87,44 @@ public abstract class Mob implements Serializable { return height; } - public int getDir() { + /** + * + * @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right + */ + public int getDirection() { return dir; } + public boolean looksLeft() { + return getDirection() == LEFT; + } + + public boolean looksRight() { + return getDirection() == RIGHT; + } + + /** + * Switches direction in which mob is looking + */ protected void switchDir() { - dir = -dir + 1; + dir = looksLeft() ? RIGHT : LEFT; } public boolean isDead() { return dead; } + /** + * Set's mob's dead variable to true and nothing else. It doesn't delete the mob. + */ public void kill() { dead = true; } + /** + * + * @return A {@link Rectangle} with mob's coordinates and size + */ public Rectangle getRect() { return new Rectangle(pos.x, pos.y, getWidth(), getHeight()); } @@ -77,5 +135,10 @@ public abstract class Mob implements Serializable { public abstract void draw(SpriteBatch spriteBatch, float x, float y); + /** + * + * @return 0 - if regular mob.
+ * 10 - if instance of {@link FallingSand}
11 - if instance of {@link FallingGravel} + */ public abstract int getType(); //0 - mob, 10 - sand, 11 - gravel } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java index 5bc0426..e36da71 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java @@ -9,13 +9,13 @@ public class Pig extends Mob { public Pig(float x, float y) { super(x, y, 25, 18, MathUtils.random(1)); - mov = new Vector2(-1 + getDir() * 2, 0); + mov = new Vector2(looksLeft() ? -1 : 1, 0); } @Override public void changeDir() { switchDir(); - mov.x = -1 + 2 * getDir(); + mov.x = -1 + 2 * getDirection(); } @Override @@ -23,7 +23,7 @@ public class Pig extends Mob { if (MathUtils.randomBoolean(.0025f)) changeDir(); else if (MathUtils.randomBoolean(.0025f)) { if (mov.x != 0f) mov.x = 0; - else mov.x = -1 + 2 * getDir(); + else mov.x = -1 + 2 * getDirection(); } if (mov.x != 0f) anim += animDelta; else anim = 0; @@ -37,17 +37,17 @@ public class Pig extends Mob { Assets.pigSprite[0][1].setRotation(anim); Assets.pigSprite[1][1].setRotation(-anim); //back legs - Assets.pigSprite[1][1].setPosition(x - 4 + (9 - getDir() * 9), y + 6); + Assets.pigSprite[1][1].setPosition(x - 4 + (9 - getDirection() * 9), y + 6); Assets.pigSprite[1][1].draw(spriteBatch); - Assets.pigSprite[1][1].setPosition(x + 17 - (9 * getDir()), y + 6); + Assets.pigSprite[1][1].setPosition(x + 17 - (9 * getDirection()), y + 6); Assets.pigSprite[1][1].draw(spriteBatch); //front legs - Assets.pigSprite[0][1].setPosition(x - 4 + (9 - getDir() * 9), y + 6); + Assets.pigSprite[0][1].setPosition(x - 4 + (9 - getDirection() * 9), y + 6); Assets.pigSprite[0][1].draw(spriteBatch); - Assets.pigSprite[0][1].setPosition(x + 17 - (9 * getDir()), y + 6); + Assets.pigSprite[0][1].setPosition(x + 17 - (9 * getDirection()), y + 6); Assets.pigSprite[0][1].draw(spriteBatch); //head & body - spriteBatch.draw(Assets.pigSprite[getDir()][0], x, y); + spriteBatch.draw(Assets.pigSprite[getDirection()][0], x, y); } @Override diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Block.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Block.java index 2a44916..8cda832 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Block.java +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Block.java @@ -10,9 +10,24 @@ public class Block { private String drop, meta; private Sprite tex; - //coll - collision, bg - background, tp - transparent, rb - requires block under it private boolean coll, bg, tp, rb, fluid; + /** + * + * @param left margin from left edge + * @param top margin from top edge + * @param right margin from right edge + * @param bottom margin from bottom edge + * @param hp hit points + * @param drop id of an item the block will drop when destroyed + * @param coll true if block has collision + * @param bg true if block should be drawn behind player + * @param tp true if block is transparent and renderer should draw a block behind it + * @param rb true if block should break when there is no block with collision under it + * @param fluid true if fluid + * @param meta extra info for storing + * @param tex block's texture + */ public Block(int left, int top, int right, int bottom, int hp, String drop, boolean coll, boolean bg, boolean tp, boolean rb, boolean fluid, String meta, Sprite tex) { this.x = left; diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java index ed8d4ac..00bc317 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java @@ -57,9 +57,9 @@ public class Drop implements Serializable { } public void pickUpDrop(Player pl) { - for (int i = 0; i < pl.inv.length; i++) { - if (pl.inv[i] == 0 || pl.inv[i] == id) { - pl.inv[i] = id; + for (int i = 0; i < pl.inventory.length; i++) { + if (pl.inventory[i] == 0 || pl.inventory[i] == id) { + pl.inventory[i] = id; pickedUp = true; break; } diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Player.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Player.java index 1459506..0f12f18 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Player.java @@ -1,10 +1,10 @@ package ru.deadsoftware.cavedroid.game.objects; import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavedroid.game.GameItems; +import ru.deadsoftware.cavedroid.game.GameWorld; import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.misc.Assets; @@ -14,16 +14,15 @@ import static ru.deadsoftware.cavedroid.GameScreen.GP; public class Player extends Mob implements Serializable { - public int[] inv; - public int invSlot; + public int[] inventory; + public int slot; public int gameMode; public boolean swim; public Player(int gameMode) { - super(0, 0, 4, 30, 1, true); + super(0, 0, 4, 30, 1); this.gameMode = gameMode; - inv = new int[9]; - pos = getSpawnPoint().cpy(); + inventory = new int[9]; swim = false; } @@ -40,13 +39,13 @@ public class Player extends Mob implements Serializable { GP.world.setForeMap(x, y, 1); break; } - if (GP.world.getForeMap(x, y) > 0 && GameItems.getBlock(GP.world.getForeMap(x, y)).hasCollision()) break; + if (GP.world.hasForeAt(x, y) && GP.world.getForeMapBlock(x, y).hasCollision()) break; } - return new Vector2(x * 16 + 8 - (float) getWidth() / 2, (float) y * 16 - getHeight()); + return new Vector2(x * 16 + 8 - getWidth() / 2, (float) y * 16 - getHeight()); } public void setDir(int dir) { - if (dir != getDir()) switchDir(); + if (dir != getDirection()) switchDir(); } @Override @@ -83,9 +82,9 @@ public class Player extends Mob implements Serializable { Assets.plSprite[0][3].setPosition(x - 6, y + 10); Assets.plSprite[0][3].draw(spriteBatch); //head - spriteBatch.draw(Assets.plSprite[getDir()][0], x - 2, y - 2); + spriteBatch.draw(Assets.plSprite[getDirection()][0], x - 2, y - 2); //body - spriteBatch.draw(Assets.plSprite[getDir()][1], x - 2, y + 8); + spriteBatch.draw(Assets.plSprite[getDirection()][1], x - 2, y + 8); //front hand Assets.plSprite[0][2].setPosition(x - 6, y); Assets.plSprite[0][2].draw(spriteBatch); diff --git a/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java b/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java index 28da58e..ee62620 100644 --- a/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/menu/MenuRenderer.java @@ -2,14 +2,22 @@ package ru.deadsoftware.cavedroid.menu; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.utils.Array; +import org.jetbrains.annotations.NotNull; import ru.deadsoftware.cavedroid.CaveGame; import ru.deadsoftware.cavedroid.GameScreen; import ru.deadsoftware.cavedroid.game.GameItems; +import ru.deadsoftware.cavedroid.game.GameProc; import ru.deadsoftware.cavedroid.game.GameSaver; import ru.deadsoftware.cavedroid.menu.objects.Button; -import ru.deadsoftware.cavedroid.misc.AppState; +import ru.deadsoftware.cavedroid.misc.InputHandlerGame; +import ru.deadsoftware.cavedroid.misc.states.AppState; import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.Renderer; +import ru.deadsoftware.cavedroid.misc.states.GameState; +import ru.deadsoftware.cavedroid.misc.states.MenuState; + +import static ru.deadsoftware.cavedroid.GameScreen.GP; +import static ru.deadsoftware.cavedroid.GameScreen.RENDERER; public class MenuRenderer extends Renderer { @@ -31,21 +39,36 @@ public class MenuRenderer extends Renderer { } - public void buttonClicked(Button button) { - if (button.getLabel().toLowerCase().equals("new game")) { - CaveGame.STATE = AppState.MENU_NEW_GAME; - } else if (button.getLabel().toLowerCase().equals("load game")) { - CaveGame.STATE = AppState.GOTO_LOAD_GAME; - } else if (button.getLabel().toLowerCase().equals("quit")) { - Gdx.app.exit(); - } else if (button.getLabel().toLowerCase().equals("survival")) { - GameScreen.NEW_GAME_MODE = 0; - CaveGame.STATE = AppState.GOTO_NEW_GAME; - } else if (button.getLabel().toLowerCase().equals("creative")) { - GameScreen.NEW_GAME_MODE = 1; - CaveGame.STATE = AppState.GOTO_NEW_GAME; - } else if (button.getLabel().toLowerCase().equals("back")) { - CaveGame.STATE = AppState.MENU_MAIN; + private void newGame(int gameMode) { + GP = new GameProc(gameMode); + GP.player.respawn(); + GameSaver.save(GP); + RENDERER = GP.renderer; + Gdx.input.setInputProcessor(new InputHandlerGame()); + CaveGame.APP_STATE = AppState.GAME; + CaveGame.GAME_STATE = GameState.PLAY; + } + + public void buttonClicked(@NotNull Button button) { + switch (button.getLabel().toLowerCase()) { + case "new game": + CaveGame.MENU_STATE = MenuState.NEW_GAME; + break; + case "load game": + CaveGame.APP_STATE = AppState.LOAD; + break; + case "quit": + Gdx.app.exit(); + break; + case "survival": + newGame(0); + break; + case "creative": + newGame(1); + break; + case "back": + CaveGame.MENU_STATE = MenuState.MAIN; + break; } } @@ -85,37 +108,27 @@ public class MenuRenderer extends Renderer { } spriter.draw(Assets.gameLogo, getWidth() / 2 - (float) Assets.gameLogo.getWidth() / 2, 8); - switch (CaveGame.STATE) { - case MENU_MAIN: + switch (CaveGame.MENU_STATE) { + case MAIN: drawButtons(menuMainBtns); break; - case MENU_NEW_GAME: + case NEW_GAME: drawButtons(menuNGBtns); break; - case GOTO_NEW_GAME: - case GOTO_LOAD_GAME: + case LOADING: drawLabel("Generating World..."); + CaveGame.APP_STATE = AppState.GAME; + CaveGame.GAME_STATE = GameState.PLAY; break; - case GOTO_MENU: + case SAVING: drawLabel("Saving Game..."); + CaveGame.APP_STATE = AppState.MENU; + CaveGame.MENU_STATE = MenuState.MAIN; break; } drawString("CaveDroid " + CaveGame.VERSION, 0, getHeight() - Assets.getStringHeight("CaveDroid " + CaveGame.VERSION) * 1.5f); spriter.end(); - - switch (CaveGame.STATE) { - case GOTO_NEW_GAME: - CaveGame.STATE = AppState.NEW_GAME; - break; - case GOTO_LOAD_GAME: - CaveGame.STATE = AppState.LOAD_GAME; - break; - case GOTO_MENU: - CaveGame.STATE = AppState.SAVE_GAME; - break; - } - } } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/ControlMode.java b/core/src/ru/deadsoftware/cavedroid/misc/ControlMode.java index ddb0c1e..dab3ac3 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/ControlMode.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/ControlMode.java @@ -1,4 +1,6 @@ -package ru.deadsoftware.cavedroid.game; +package ru.deadsoftware.cavedroid.misc; -public class ControlMode { +public enum ControlMode { + WALK, + CURSOR } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java b/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java index 3eeb6f9..a84e22a 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java @@ -40,12 +40,15 @@ public class InputHandlerGame implements InputProcessor { if (CaveGame.TOUCH) { if (screenX > 26 && screenX < 52 && screenY > GP.renderer.getHeight() - 52 && screenY < GP.renderer.getHeight() - 26) { - if (GP.ctrlMode == 1) gameInput.keyDown(Input.Keys.W); - else gameInput.keyDown(Input.Keys.SPACE); + if (GP.controlMode == ControlMode.WALK) { + gameInput.keyDown(Input.Keys.W); + } else { + gameInput.keyDown(Input.Keys.SPACE); + } } else if (screenX > 0 && screenX < 26 && screenY > GP.renderer.getHeight() - 26) { gameInput.keyDown(Input.Keys.A); } else if (screenX > 26 && screenX < 52 && screenY > GP.renderer.getHeight() - 26) { - if (GP.ctrlMode == 1) gameInput.keyDown(Input.Keys.S); + if (GP.controlMode == ControlMode.CURSOR) gameInput.keyDown(Input.Keys.S); else gameInput.keyDown(Input.Keys.CONTROL_LEFT); } else if (screenX > 52 && screenX < 78 && screenY > GP.renderer.getHeight() - 26) { gameInput.keyDown(Input.Keys.D); @@ -72,12 +75,12 @@ public class InputHandlerGame implements InputProcessor { if (CaveGame.TOUCH) { if (screenX > 26 && screenX < 52 && screenY > GP.renderer.getHeight() - 52 && screenY < GP.renderer.getHeight() - 26) { - if (GP.ctrlMode == 1) gameInput.keyUp(Input.Keys.W); + if (GP.controlMode == ControlMode.CURSOR) gameInput.keyUp(Input.Keys.W); else gameInput.keyUp(Input.Keys.SPACE); } else if (screenX > 0 && screenX < 26 && screenY > GP.renderer.getHeight() - 26) { gameInput.keyUp(Input.Keys.A); } else if (screenX > 26 && screenX < 52 && screenY > GP.renderer.getHeight() - 26) { - if (GP.ctrlMode == 1) gameInput.keyUp(Input.Keys.S); + if (GP.controlMode == ControlMode.CURSOR) gameInput.keyUp(Input.Keys.S); else gameInput.keyUp(Input.Keys.CONTROL_LEFT); } else if (screenX > 52 && screenX < 78 && screenY > GP.renderer.getHeight() - 26) { gameInput.keyUp(Input.Keys.D); diff --git a/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerMenu.java b/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerMenu.java index 8f9108d..5616d7b 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerMenu.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerMenu.java @@ -38,8 +38,8 @@ public class InputHandlerMenu implements InputProcessor { public boolean touchUp(int screenX, int screenY, int pointer, int mb) { screenX *= menuRenderer.getWidth() / GameScreen.getWidth(); screenY *= menuRenderer.getHeight() / GameScreen.getHeight(); - switch (CaveGame.STATE) { - case MENU_MAIN: + switch (CaveGame.MENU_STATE) { + case MAIN: for (Button button : menuRenderer.menuMainBtns) { if (button.getRect().contains(screenX, screenY) && button.getType() > 0) { menuRenderer.buttonClicked(button); @@ -47,7 +47,7 @@ public class InputHandlerMenu implements InputProcessor { } } break; - case MENU_NEW_GAME: + case NEW_GAME: for (Button button : menuRenderer.menuNGBtns) { if (button.getRect().contains(screenX, screenY) && button.getType() > 0) { menuRenderer.buttonClicked(button); diff --git a/core/src/ru/deadsoftware/cavedroid/misc/states/AppState.java b/core/src/ru/deadsoftware/cavedroid/misc/states/AppState.java index 8af5127..e706403 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/states/AppState.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/states/AppState.java @@ -1,17 +1,8 @@ -package ru.deadsoftware.cavedroid.misc; +package ru.deadsoftware.cavedroid.misc.states; public enum AppState { - GOTO_NEW_GAME, - GOTO_LOAD_GAME, - GOTO_MENU, - - NEW_GAME, - LOAD_GAME, - SAVE_GAME, - - MENU_MAIN, - MENU_NEW_GAME, - - GAME_PLAY, - GAME_CREATIVE_INV + MENU, + GAME, + SAVE, + LOAD } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/states/GameState.java b/core/src/ru/deadsoftware/cavedroid/misc/states/GameState.java index 7ed4115..f2ceb44 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/states/GameState.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/states/GameState.java @@ -1,4 +1,7 @@ -package ru.deadsoftware.cavedroid.misc; +package ru.deadsoftware.cavedroid.misc.states; public enum GameState { + PLAY, + CREATIVE_INV, + PAUSE } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/states/MenuState.java b/core/src/ru/deadsoftware/cavedroid/misc/states/MenuState.java index db3c586..69c39c4 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/states/MenuState.java +++ b/core/src/ru/deadsoftware/cavedroid/misc/states/MenuState.java @@ -1,4 +1,8 @@ package ru.deadsoftware.cavedroid.misc.states; public enum MenuState { + MAIN, + NEW_GAME, + LOADING, + SAVING } -- 2.29.2