From fe86b2fc3788c58fb134415780b312b4c7ef5c97 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Sat, 21 Apr 2018 19:08:04 +0700 Subject: [PATCH] Fix bugs --- .../ru/deadsoftware/cavecraft/GameScreen.java | 5 +++ .../ru/deadsoftware/cavecraft/GameState.java | 1 + .../cavecraft/game/GameInputHandler.java | 41 +++++++++++-------- .../deadsoftware/cavecraft/game/GameProc.java | 4 +- .../cavecraft/game/GameRenderer.java | 2 +- .../cavecraft/game/GameWorld.java | 17 ++++++-- .../deadsoftware/cavecraft/game/WorldGen.java | 11 ++--- 7 files changed, 53 insertions(+), 28 deletions(-) diff --git a/core/src/ru/deadsoftware/cavecraft/GameScreen.java b/core/src/ru/deadsoftware/cavecraft/GameScreen.java index e1eea18..2a2c262 100644 --- a/core/src/ru/deadsoftware/cavecraft/GameScreen.java +++ b/core/src/ru/deadsoftware/cavecraft/GameScreen.java @@ -35,6 +35,11 @@ public class GameScreen implements Screen { @Override public void render(float delta) { + if (CaveGame.STATE == GameState.RESTART) { + gameProc = new GameProc(); + Gdx.input.setInputProcessor(new InputHandler(gameProc)); + CaveGame.STATE = GameState.GAME_PLAY; + } FPS = (int)(1/delta); gameProc.update(delta); gameProc.renderer.render(); diff --git a/core/src/ru/deadsoftware/cavecraft/GameState.java b/core/src/ru/deadsoftware/cavecraft/GameState.java index 9de340d..dd6b021 100644 --- a/core/src/ru/deadsoftware/cavecraft/GameState.java +++ b/core/src/ru/deadsoftware/cavecraft/GameState.java @@ -1,6 +1,7 @@ package ru.deadsoftware.cavecraft; public enum GameState { + RESTART, GAME_PLAY, GAME_CREATIVE_INV } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java index 89fba69..dc19afd 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInputHandler.java @@ -13,9 +13,9 @@ public class GameInputHandler { this.gameProc = gameProc; } - private void wasdPressed(int keyCode) { + private void wasdPressed(int keycode) { if (gameProc.ctrlMode==0 || !CaveGame.TOUCH) { - switch (keyCode) { + switch (keycode) { case Input.Keys.A: gameProc.player.moveX.x = -GamePhysics.PL_SPEED; gameProc.player.dir = 0; @@ -26,7 +26,7 @@ public class GameInputHandler { break; } } else if (CaveGame.TOUCH){ - switch (keyCode) { + switch (keycode) { case Input.Keys.A: gameProc.cursorX--; break; @@ -43,11 +43,13 @@ public class GameInputHandler { } } - public void keyDown(int keyCode) { - if (keyCode == Input.Keys.W || keyCode == Input.Keys.A || - keyCode == Input.Keys.S || keyCode == Input.Keys.D) { - wasdPressed(keyCode); - } else switch (keyCode) { + public void keyDown(int keycode) { + gameProc.isKeyDown = true; + gameProc.keyDownCode = keycode; + if (keycode == Input.Keys.W || keycode == Input.Keys.A || + keycode == Input.Keys.S || keycode == Input.Keys.D) { + wasdPressed(keycode); + } else switch (keycode) { case Input.Keys.ALT_LEFT: if (CaveGame.TOUCH) { gameProc.ctrlMode++; @@ -74,11 +76,14 @@ public class GameInputHandler { if (CaveGame.STATE == GameState.GAME_PLAY) CaveGame.STATE = GameState.GAME_CREATIVE_INV; else CaveGame.STATE = GameState.GAME_PLAY; break; + + case Input.Keys.N: + CaveGame.STATE = GameState.RESTART; } } - public void keyUp(int keyCode) { - switch (keyCode) { + public void keyUp(int keycode) { + switch (keycode) { case Input.Keys.A: case Input.Keys.D: gameProc.player.moveX.x = 0; break; @@ -98,16 +103,14 @@ public class GameInputHandler { screenXgameProc.renderer.camera.viewportHeight/2-Assets.creativeInv.getRegionHeight()/2 && screenY= 0 && item < Items.BLOCKS.size) { for (int i = 8; i > 0; i--) { gameProc.player.inventory[i] = gameProc.player.inventory[i - 1]; } - if (item >= 0 && item < Items.BLOCKS.size) gameProc.player.inventory[0] = item; - } catch (Exception e) { - Gdx.app.error("GameInputHandler", e.toString()); + gameProc.player.inventory[0] = item; } } else if (CaveGame.STATE == GameState.GAME_CREATIVE_INV) { CaveGame.STATE = GameState.GAME_PLAY; @@ -121,6 +124,10 @@ public class GameInputHandler { } public void touchUp(int screenX, int screenY, int button) { + if (gameProc.isKeyDown) { + keyUp(gameProc.keyDownCode); + gameProc.isKeyDown = false; + } if (gameProc.isTouchDown) { if (button == Input.Buttons.RIGHT){ gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index 7ded79f..6a2bf04 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -26,8 +26,8 @@ public class GameProc { public int invSlot; public int ctrlMode; - public boolean isTouchDown = false; - public int touchDownX, touchDownY; + public boolean isTouchDown, isKeyDown; + public int touchDownX, touchDownY, keyDownCode; public int touchDownButton; public long touchDownTime; diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java index 5a82102..b8b5324 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java @@ -231,7 +231,7 @@ public class GameRenderer { drawString("CaveCraft "+CaveGame.VERSION, 0, 0); drawString("FPS: "+GameScreen.FPS, 0, 20); drawString("X: "+(int)(gameProc.player.position.x/16), 0, 40); - drawString("Y: "+(int)(gameProc.player.position.y/16), 0, 60); + drawString("Y: "+(gameProc.world.getHeight()-(int)(gameProc.player.position.y/16)), 0, 60); drawString("Seed: "+WorldGen.getSeed(), 0, 80); drawString("Mobs: "+gameProc.mobs.size, 0, 100); fontBatch.end(); diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java index c4655bf..e6b0b00 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java @@ -3,6 +3,8 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.ArrayMap; +import ru.deadsoftware.cavecraft.CaveGame; +import ru.deadsoftware.cavecraft.GameState; import ru.deadsoftware.cavecraft.Items; public class GameWorld { @@ -84,13 +86,22 @@ public class GameWorld { while (!found) { for (int i = 0; i < getHeight(); i++) { if (getForeMap((int)x, i)>0 && - Items.BLOCKS.getValueAt(getForeMap((int)x, i)).collision) { - y = i-3; + Items.BLOCKS.getValueAt(getForeMap((int)x, i)).collision && + getForeMap((int)x, i-1)==0 && getForeMap((int)x, i-1)==0) { + y = i-2; found = true; break; } } - if (!found) x--; + if (!found) { + x--; + if (x<0) x=getWidth()-1; + if ((int)x == getWidth()/2+1) { + x--; + y=0; + break; + } + } } x = x*16 + 4; y *= 16; diff --git a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java index 32cb8f9..954bdff 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java +++ b/core/src/ru/deadsoftware/cavecraft/game/WorldGen.java @@ -54,16 +54,14 @@ public class WorldGen { genWorld(width, height, TimeUtils.millis()); } - static void genWorld(int width, int height, long s) { + static void genWorld(int width, int height, long worldseed) { int dirtH; - seed = s; + seed = worldseed; rand = new RandomXS128(seed); foreMap = new int[width][height]; backMap = new int[width][height]; hMap = genLandscape(width, height/4, height/8, height/2); for (int x=0; x2 && x