From 7217e9b86d6e44604703ffb9956bcade97cfe9c9 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Tue, 24 Apr 2018 16:09:30 +0700 Subject: [PATCH] Add world saving --- android/build.gradle | 4 +- build.gradle | 2 +- .../ru/deadsoftware/cavecraft/CaveGame.java | 16 +++- .../deadsoftware/cavecraft/game/GameProc.java | 7 +- .../cavecraft/game/GameWorld.java | 33 +++++-- .../cavecraft/game/WorldSaver.java | 96 +++++++++++++++++++ 6 files changed, 143 insertions(+), 15 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavecraft/game/WorldSaver.java diff --git a/android/build.gradle b/android/build.gradle index 1689a1a..b15a589 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -21,8 +21,8 @@ android { applicationId "ru.deadsoftware.cavecraft" minSdkVersion 9 targetSdkVersion 20 - versionCode 3 - versionName "alpha0.2.1" + versionCode 4 + versionName "alpha0.3-dev" } buildTypes { release { diff --git a/build.gradle b/build.gradle index 8dca349..a53e4cf 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ allprojects { apply plugin: "eclipse" apply plugin: "idea" - version = 'alpha0.2.1' + version = 'alpha0.3-dev' ext { appName = "CaveCraft" gdxVersion = '1.9.7' diff --git a/core/src/ru/deadsoftware/cavecraft/CaveGame.java b/core/src/ru/deadsoftware/cavecraft/CaveGame.java index 8a26b6a..31b763a 100644 --- a/core/src/ru/deadsoftware/cavecraft/CaveGame.java +++ b/core/src/ru/deadsoftware/cavecraft/CaveGame.java @@ -1,10 +1,12 @@ package ru.deadsoftware.cavecraft; import com.badlogic.gdx.Game; +import com.badlogic.gdx.Gdx; public class CaveGame extends Game { - public static final String VERSION = "alpha 0.2.1"; + public static final String VERSION = "alpha 0.3-dev"; + public static String GAME_FOLDER; public static GameState STATE; @@ -21,6 +23,18 @@ public class CaveGame extends Game { @Override public void create () { + switch (Gdx.app.getType()) { + case Desktop: + GAME_FOLDER = System.getProperty("user.home")+"/.cavecraft"; + break; + case Android: + GAME_FOLDER = "/sdcard/cavecraft"; + break; + default: + Gdx.app.exit(); + } + Gdx.app.log("CaveGame", "Folder: "+GAME_FOLDER); + Gdx.files.local(GAME_FOLDER).mkdirs(); setScreen(new GameScreen()); } diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index dfe4d6b..a4a6c94 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -31,7 +31,12 @@ public class GameProc { public long touchDownTime; public GameProc() { - world = new GameWorld(1024,256); + world = new GameWorld(); + if (WorldSaver.exists()) { + world.load(); + } else { + world.generate(1024, 256); + } if (CaveGame.TOUCH) { renderer = new GameRenderer(this,320, 320*((float)GameScreen.getHeight()/GameScreen.getWidth())); diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java index 064eca5..4ed875d 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameWorld.java @@ -6,20 +6,11 @@ import ru.deadsoftware.cavecraft.Items; public class GameWorld { - private final int WIDTH, HEIGHT; + private int WIDTH, HEIGHT; private int[][] foreMap; private int[][] backMap; - public GameWorld(int w, int h) { - WIDTH = w; - HEIGHT = h; - WorldGen.genWorld(WIDTH,HEIGHT); - foreMap = WorldGen.getForeMap(); - backMap = WorldGen.getBackMap(); - WorldGen.clear(); - } - public int getWidth() { return WIDTH; } @@ -111,4 +102,26 @@ public class GameWorld { return new Vector2(x,y); } + public void generate(int w, int h) { + WIDTH = w; + HEIGHT = h; + WorldGen.genWorld(WIDTH,HEIGHT); + 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(); + 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 new file mode 100644 index 0000000..41967a2 --- /dev/null +++ b/core/src/ru/deadsoftware/cavecraft/game/WorldSaver.java @@ -0,0 +1,96 @@ +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