DEADSOFTWARE

Add world saving
authorfred-boy <fred-boy@protonmail.com>
Tue, 24 Apr 2018 09:09:30 +0000 (16:09 +0700)
committerfred-boy <fred-boy@protonmail.com>
Tue, 24 Apr 2018 09:09:30 +0000 (16:09 +0700)
android/build.gradle
build.gradle
core/src/ru/deadsoftware/cavecraft/CaveGame.java
core/src/ru/deadsoftware/cavecraft/game/GameProc.java
core/src/ru/deadsoftware/cavecraft/game/GameWorld.java
core/src/ru/deadsoftware/cavecraft/game/WorldSaver.java [new file with mode: 0644]

index 1689a1af1a73897546a0cb15b6b6c9cde62baf9c..b15a589ba5c401fde5c6f8adbfc97e950146e33c 100644 (file)
@@ -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 {
index 8dca349f8dd6c628bd49f797350865736c300fe5..a53e4cf72655d010f17bf92e2e6ced152cf667fa 100644 (file)
@@ -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'
index 8a26b6a9310d90fa654fca7f4ff5f3a9092d083d..31b763a3e7106d87c1fe26abc0686f6487c51b11 100644 (file)
@@ -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());
        }
 
index dfe4d6bdd306f6a6c139a1a52f00d19283705bc2..a4a6c94f698892faf86e7fd98cd3f30a27e7a088 100644 (file)
@@ -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()));
index 064eca58ffa72052a0b6afd0695aea1772c159c2..4ed875de4e4f87b705d7301f6c80645293948fe1 100644 (file)
@@ -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 (file)
index 0000000..41967a2
--- /dev/null
@@ -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<map[0].length; y++) {
+            int bl = map[0][y];
+            int rl = 1;
+            for (int x=0; x<map.length; x++) {
+                if (map[x][y]!=bl || x==map.length-1) {
+                    writeInt(file, rl, true);
+                    writeInt(file, bl, true);
+                    rl=1;
+                    bl=map[x][y];
+                } else {
+                    rl++;
+                }
+            }
+        }
+    }
+
+    private static int[][] loadMap(FileHandle file) {
+        int[][] map = null;
+        int ver, width, height;
+        int rl,bl;
+        byte[] data = file.readBytes();
+        readIndex = 0;
+        ver = bytesInt(data);
+        if (VERSION <= ver) {
+            width = bytesInt(data);
+            height = bytesInt(data);
+            map = new int[width][height];
+            for (int y=0; y<height; y++) {
+                for (int x=0; x<width; x+=rl) {
+                    rl = bytesInt(data);
+                    bl = bytesInt(data);
+                    for (int i=x; i<x+rl; i++) map[i][y] = bl;
+                }
+            }
+        }
+        return map;
+    }
+
+    public static int[][] getLoadedForeMap() {
+        return fMap;
+    }
+
+    public static int[][] getLoadedBackMap() {
+        return bMap;
+    }
+
+    public static void load() {
+        fMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"));
+        bMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"));
+    }
+
+    public static void save(int[][] foreMap, int[][] backMap) {
+        Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/").mkdirs();
+        saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"), foreMap);
+        saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"), backMap);
+    }
+
+    public static boolean exists() {
+        return (Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav").exists() &&
+                Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav").exists());
+    }
+
+}