From 6ab2a53526f16de139d39a59c3d800e5f3013c68 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Tue, 24 Apr 2018 21:33:30 +0700 Subject: [PATCH] Complete game save --- .../ru/deadsoftware/cavecraft/AppState.java | 4 +- .../ru/deadsoftware/cavecraft/GameScreen.java | 11 +- .../cavecraft/InputHandlerMenu.java | 4 +- .../ru/deadsoftware/cavecraft/Renderer.java | 6 + .../cavecraft/game/GameInput.java | 7 +- .../cavecraft/game/GamePhysics.java | 2 +- .../deadsoftware/cavecraft/game/GameProc.java | 34 ++--- .../cavecraft/game/GameRenderer.java | 10 +- .../cavecraft/game/GameSaver.java | 138 ++++++++++++++++++ .../cavecraft/game/GameWorld.java | 55 +++---- .../cavecraft/game/WorldSaver.java | 96 ------------ .../cavecraft/game/objects/Player.java | 7 +- .../cavecraft/menu/MenuRenderer.java | 58 ++++++-- .../cavecraft/menu/objects/Button.java | 4 + 14 files changed, 253 insertions(+), 183 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavecraft/game/GameSaver.java delete mode 100644 core/src/ru/deadsoftware/cavecraft/game/WorldSaver.java diff --git a/core/src/ru/deadsoftware/cavecraft/AppState.java b/core/src/ru/deadsoftware/cavecraft/AppState.java index e38e701..233bbb6 100644 --- a/core/src/ru/deadsoftware/cavecraft/AppState.java +++ b/core/src/ru/deadsoftware/cavecraft/AppState.java @@ -1,11 +1,11 @@ package ru.deadsoftware.cavecraft; public enum AppState { - GOTO_GAME, + GOTO_NEW_GAME, + GOTO_LOAD_GAME, GOTO_MENU, MENU_MAIN, - MENU_GENWORLD, GAME_PLAY, GAME_CREATIVE_INV diff --git a/core/src/ru/deadsoftware/cavecraft/GameScreen.java b/core/src/ru/deadsoftware/cavecraft/GameScreen.java index 1c6ad79..682319d 100644 --- a/core/src/ru/deadsoftware/cavecraft/GameScreen.java +++ b/core/src/ru/deadsoftware/cavecraft/GameScreen.java @@ -3,6 +3,7 @@ package ru.deadsoftware.cavecraft; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import ru.deadsoftware.cavecraft.game.GameProc; +import ru.deadsoftware.cavecraft.game.GameSaver; import ru.deadsoftware.cavecraft.menu.MenuRenderer; public class GameScreen implements Screen { @@ -38,7 +39,6 @@ public class GameScreen implements Screen { @Override public void show() { - } @Override @@ -53,13 +53,20 @@ public class GameScreen implements Screen { menu(); break; - case GOTO_GAME: + case GOTO_NEW_GAME: gameProc = new GameProc(); renderer = gameProc.renderer; Gdx.input.setInputProcessor(new InputHandlerGame(gameProc)); CaveGame.STATE = AppState.GAME_PLAY; break; + case GOTO_LOAD_GAME:_GAME: + gameProc = GameSaver.load(); + renderer = gameProc.renderer; + Gdx.input.setInputProcessor(new InputHandlerGame(gameProc)); + CaveGame.STATE = AppState.GAME_PLAY; + break; + case GOTO_MENU: renderer = menuRenderer; Gdx.input.setInputProcessor(new InputHandlerMenu(menuRenderer)); diff --git a/core/src/ru/deadsoftware/cavecraft/InputHandlerMenu.java b/core/src/ru/deadsoftware/cavecraft/InputHandlerMenu.java index 75aa9a9..232e217 100644 --- a/core/src/ru/deadsoftware/cavecraft/InputHandlerMenu.java +++ b/core/src/ru/deadsoftware/cavecraft/InputHandlerMenu.java @@ -36,8 +36,8 @@ public class InputHandlerMenu implements InputProcessor{ public boolean touchUp(int screenX, int screenY, int pointer, int mb) { screenX *= renderer.getWidth()/GameScreen.getWidth(); screenY *= renderer.getHeight()/GameScreen.getHeight(); - for (Button button : renderer.buttons) { - if (button.getRect().contains(screenX, screenY)) renderer.buttonClicked(button); + for (Button button : renderer.menuMainButtons) { + if (button.getRect().contains(screenX, screenY) && button.getType()>0) renderer.buttonClicked(button); } return false; } diff --git a/core/src/ru/deadsoftware/cavecraft/Renderer.java b/core/src/ru/deadsoftware/cavecraft/Renderer.java index 928d1fb..43ea03e 100644 --- a/core/src/ru/deadsoftware/cavecraft/Renderer.java +++ b/core/src/ru/deadsoftware/cavecraft/Renderer.java @@ -41,6 +41,12 @@ public abstract class Renderer { Assets.minecraftFont.draw(spriteBatch, str, x, y); } + public void drawString(String str) { + Assets.minecraftFont.draw(spriteBatch, str, + getWidth()/2 - Assets.getStringWidth(str)/2, + getHeight()/2 - Assets.getStringHeight(str)/2); + } + public abstract void render(); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java index 10e0e43..cddeb4d 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameInput.java @@ -2,8 +2,10 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Input; import com.badlogic.gdx.utils.TimeUtils; -import ru.deadsoftware.cavecraft.*; import ru.deadsoftware.cavecraft.AppState; +import ru.deadsoftware.cavecraft.Assets; +import ru.deadsoftware.cavecraft.CaveGame; +import ru.deadsoftware.cavecraft.Items; public class GameInput { @@ -78,10 +80,11 @@ public class GameInput { break; case Input.Keys.N: - CaveGame.STATE = AppState.GOTO_GAME; + CaveGame.STATE = AppState.GOTO_NEW_GAME; break; case Input.Keys.ESCAPE: case Input.Keys.BACK: + GameSaver.save(gameProc); CaveGame.STATE = AppState.GOTO_MENU; break; } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java index 3c2e9ec..07587e6 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java @@ -97,7 +97,7 @@ public class GamePhysics { if (pl.position.x+pl.texWidth/2<0) pl.position.x+=gameProc.world.getWidth()*16; if (pl.position.x+pl.texWidth/2>gameProc.world.getWidth()*16) pl.position.x-=gameProc.world.getWidth()*16; if (pl.position.y > gameProc.world.getHeight()*16) { - pl.position = gameProc.world.getSpawnPoint(0).cpy(); + pl.position = gameProc.world.getSpawnPoint().cpy(); } if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) { pl.moveY.add(0, -8); diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index a2107fa..4fe96c6 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -2,25 +2,25 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; -import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.TimeUtils; import ru.deadsoftware.cavecraft.*; -import ru.deadsoftware.cavecraft.AppState; import ru.deadsoftware.cavecraft.game.mobs.Mob; -import ru.deadsoftware.cavecraft.game.mobs.Pig; import ru.deadsoftware.cavecraft.game.objects.Player; -public class GameProc { +import java.io.Serializable; +import java.util.ArrayList; + +public class GameProc implements Serializable{ public static double RUN_TIME = 0; public Player player; - public Array mobs; + public ArrayList mobs; - public GameWorld world; - public GameRenderer renderer; - public GamePhysics physics; + public transient GameWorld world; + public transient GameRenderer renderer; + public transient GamePhysics physics; public int cursorX, cursorY; public int invSlot; @@ -33,11 +33,11 @@ public class GameProc { public GameProc() { world = new GameWorld(); - if (WorldSaver.exists()) { - world.load(); - } else { - world.generate(1024, 256); - } + world.generate(8,256); + player = new Player(world.getSpawnPoint()); + mobs = new ArrayList(); + physics = new GamePhysics(this); + if (!CaveGame.TOUCH) ctrlMode = 1; if (CaveGame.TOUCH) { renderer = new GameRenderer(this,320, 320*((float)GameScreen.getHeight()/GameScreen.getWidth())); @@ -45,13 +45,7 @@ public class GameProc { renderer = new GameRenderer(this,480, 480*((float)GameScreen.getHeight()/GameScreen.getWidth())); } - physics = new GamePhysics(this); - player = new Player(world.getSpawnPoint(0)); - mobs = new Array(); - for (int i=0; i0) { - x = x % (getWidth()); - } + x = x%getWidth(); + if (x<0) x=getWidth()-Math.abs(x); map = foreMap[x][y]; } catch (ArrayIndexOutOfBoundsException e) { Gdx.app.error("GameWorld",e.toString()); @@ -37,12 +41,8 @@ public class GameWorld { public void setForeMap(int x, int y, int value) { try { - if (x<0) { - x = x % (getWidth()); - x = getWidth()- Math.abs(x); - } else if (x>0) { - x = x % (getWidth()); - } + x = x%getWidth(); + if (x<0) x=getWidth()-Math.abs(x); foreMap[x][y] = value; } catch (ArrayIndexOutOfBoundsException e) { Gdx.app.error("GameWorld", e.toString()); @@ -52,12 +52,8 @@ public class GameWorld { public int getBackMap(int x, int y) { int map = 0; try { - if (x<0) { - x = x % (getWidth()); - x = getWidth()- Math.abs(x); - } else if (x>0) { - x = x % (getWidth()); - } + x = x%getWidth(); + if (x<0) x=getWidth()-Math.abs(x); map = backMap[x][y]; } catch (ArrayIndexOutOfBoundsException e) { Gdx.app.error("GameWorld",e.toString()); @@ -67,12 +63,8 @@ public class GameWorld { public void setBackMap(int x, int y, int value) { try { - if (x<0) { - x = x % (getWidth()); - x = getWidth()- Math.abs(x); - } else if (x>0) { - x = x % (getWidth()); - } + x = x%getWidth(); + if (x<0) x=getWidth()-Math.abs(x); backMap[x][y] = value; } catch (ArrayIndexOutOfBoundsException e) { Gdx.app.error("GameWorld", e.toString()); @@ -91,8 +83,8 @@ public class GameWorld { } } - public Vector2 getSpawnPoint(int x) { - int y=0; + public Vector2 getSpawnPoint() { + int x=0,y=0; while (true) { y++; if (getForeMap(x,y)>0 && Items.BLOCKS.getValueAt(getForeMap(x,y)).collision) break; @@ -109,17 +101,12 @@ public class GameWorld { foreMap = WorldGen.getForeMap(); backMap = WorldGen.getBackMap(); WorldGen.clear(); - save(); - } - - public void save() { - WorldSaver.save(foreMap, backMap); } public void load() { - WorldSaver.load(); - foreMap = WorldSaver.getLoadedForeMap(); - backMap = WorldSaver.getLoadedBackMap(); + GameSaver.loadMap(); + foreMap = GameSaver.getLoadedForeMap(); + backMap = GameSaver.getLoadedBackMap(); WIDTH = foreMap.length; HEIGHT = foreMap[0].length; } diff --git a/core/src/ru/deadsoftware/cavecraft/game/WorldSaver.java b/core/src/ru/deadsoftware/cavecraft/game/WorldSaver.java deleted file mode 100644 index 41967a2..0000000 --- a/core/src/ru/deadsoftware/cavecraft/game/WorldSaver.java +++ /dev/null @@ -1,96 +0,0 @@ -package ru.deadsoftware.cavecraft.game; - -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.files.FileHandle; -import ru.deadsoftware.cavecraft.CaveGame; - -import java.nio.ByteBuffer; - -public class WorldSaver { - - private static final int VERSION = 0; - - private static int[][] fMap, bMap; - private static int readIndex; - - private static int bytesInt(byte[] bytes) { - ByteBuffer wrapped = ByteBuffer.wrap(bytes); - int res = wrapped.getInt(readIndex); - readIndex+=4; - return res; - } - - private static void writeInt(FileHandle file, int i, boolean append) { - byte[] bytes = ByteBuffer.allocate(4).putInt(i).array(); - file.writeBytes(bytes, append); - } - - private static void saveMap(FileHandle file, int[][] map) { - int width = map.length; - int height = map[0].length; - writeInt(file, VERSION, false); - writeInt(file, width, true); - writeInt(file, height, true); - for (int y=0; y buttons; + public Array