diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java b/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java
index 0d796b8b3a22583ed5e7e540bcb3b0ebe5bfe6a0..51dbd5bfb935f100b93a9ddd0c4a4f33e80a1de9 100644 (file)
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
-import org.jetbrains.annotations.NotNull;
-import ru.deadsoftware.cavedroid.CaveGame;
+import ru.deadsoftware.cavedroid.MainConfig;
+import ru.deadsoftware.cavedroid.game.mobs.MobsController;
+import ru.deadsoftware.cavedroid.game.objects.DropController;
+import ru.deadsoftware.cavedroid.game.world.GameWorld;
+import javax.annotation.CheckForNull;
import java.io.*;
import java.nio.ByteBuffer;
public class GameSaver {
- private static final int VERSION = 0;
+ private static final String TAG = "GameSaver";
+
+ public static class Data {
+ @CheckForNull
+ private MobsController mMobsController;
+ @CheckForNull
+ private DropController mDropController;
+ @CheckForNull
+ private int[][] mForeMap, mBackMap;
+
+ public Data(MobsController mobsController, DropController dropController, int[][] foreMap, int[][] backMap) {
+ mMobsController = mobsController;
+ mDropController = dropController;
+ mForeMap = foreMap;
+ mBackMap = backMap;
+ }
+
+ public MobsController retrieveMobsController() {
+ assert mMobsController != null;
+ MobsController mobsController = mMobsController;
+ mMobsController = null;
+ return mobsController;
+ }
+
+ public DropController retrieveDropController() {
+ assert mDropController != null;
+ DropController dropController = mDropController;
+ mDropController = null;
+ return dropController;
+ }
+
+ public int[][] retrieveForeMap() {
+ assert mForeMap != null;
+ int[][] foreMap = mForeMap;
+ mForeMap = null;
+ return foreMap;
+ }
+
+ public int[][] retrieveBackMap() {
+ assert mBackMap != null;
+ int[][] backMap = mBackMap;
+ mBackMap = null;
+ return backMap;
+ }
+
+ public boolean isEmpty() {
+ return mMobsController == null && mDropController == null && mForeMap == null && mBackMap == null;
+ }
+ }
+
+ private static final int SAVE_VERSION = 1;
- @NotNull
private static byte[] intToBytes(int i) {
return ByteBuffer.allocate(4).putInt(i).array();
}
- private static void saveMap(@NotNull FileHandle file, @NotNull int[][] map) throws IOException {
- int rl, bl;
+ private static void saveMap(FileHandle file, int[][] map) throws IOException {
+ int run, block;
int width = map.length;
int height = map[0].length;
+
BufferedOutputStream out = new BufferedOutputStream(file.write(false));
- out.write(intToBytes(VERSION));
+
+ out.write(intToBytes(SAVE_VERSION));
out.write(intToBytes(width));
out.write(intToBytes(height));
+
for (int y = 0; y < height; y++) {
- bl = map[0][y];
- rl = 0;
+ block = map[0][y];
+ run = 0;
for (int[] ints : map) {
- if (ints[y] != bl) {
- out.write(intToBytes(rl));
- out.write(intToBytes(bl));
- rl = 0;
- bl = ints[y];
+ if (ints[y] != block) {
+ out.write(intToBytes(run));
+ out.write(intToBytes(block));
+ run = 0;
+ block = ints[y];
}
- rl++;
+ run++;
}
- out.write(intToBytes(rl));
- out.write(intToBytes(bl));
+ out.write(intToBytes(run));
+ out.write(intToBytes(block));
}
+
out.flush();
out.close();
}
- @NotNull
- private static int[][] loadMap(@NotNull FileHandle file) throws Exception {
+ private static int[][] loadMap(FileHandle file) throws Exception {
int[][] map;
- int ver, width, height;
- int rl, bl;
+ int version, width, height;
+ int run, block;
+
DataInputStream in = new DataInputStream(file.read());
- ver = in.readInt();
- if (VERSION == ver) {
+
+ version = in.readInt();
+
+ if (SAVE_VERSION == version) {
width = in.readInt();
height = in.readInt();
map = new int[width][height];
for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x += rl) {
- rl = in.readInt();
- bl = in.readInt();
- for (int i = x; i < x + rl; i++) map[i][y] = bl;
+ for (int x = 0; x < width; x += run) {
+ run = in.readInt();
+ block = in.readInt();
+ for (int i = x; i < x + run; i++) {
+ map[i][y] = block;
+ }
}
}
- in.close();
- } else throw new Exception("version mismatch");
+ } else {
+ throw new Exception("version mismatch");
+ }
+
+ in.close();
return map;
}
- public static GameProc load() {
- FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
- GameProc gameProc = null;
+ @CheckForNull
+ public static Data load(MainConfig mainConfig) {
+ String folder = mainConfig.getGameFolder();
+ FileHandle file = Gdx.files.absolute(folder + "/saves/game.sav");
+
try {
ObjectInputStream in = new ObjectInputStream(file.read());
- int ver = in.readInt();
- if (VERSION == ver) gameProc = (GameProc) in.readObject();
- else throw new Exception("version mismatch");
+ int version = in.readInt();
+ DropController dropController;
+ MobsController mobsController;
+
+ if (SAVE_VERSION == version) {
+ dropController = (DropController) in.readObject();
+ mobsController = (MobsController) in.readObject();
+ } else {
+ throw new Exception("version mismatch");
+ }
+
in.close();
- gameProc.world = new GameWorld(
- loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav")),
- loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"))
- );
- gameProc.physics = new GamePhysics();
- gameProc.resetRenderer();
+
+ int[][] foreMap = loadMap(Gdx.files.absolute(mainConfig.getGameFolder() + "/saves/foremap.sav"));
+ int[][] backMap = loadMap(Gdx.files.absolute(mainConfig.getGameFolder() + "/saves/backmap.sav"));
+
+ if (dropController == null || mobsController == null) {
+ throw new Exception("couldn't load");
+ }
+
+ return new Data(mobsController, dropController, foreMap, backMap);
} catch (Exception e) {
- Gdx.app.error("GameSaver", e.getMessage(), e);
- Gdx.app.exit();
+ Gdx.app.error("GameSaver", e.getMessage());
}
- return gameProc;
+
+ return null;
}
- public static void save(GameProc gp) {
- FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/");
+ public static void save(MainConfig mainConfig,
+ DropController dropController,
+ MobsController mobsController,
+ GameWorld gameWorld) {
+
+ Gdx.app.debug(TAG, "Saves are disabled for this build");
+ return;
+
+
+/* String folder = mainConfig.getGameFolder();
+ FileHandle file = Gdx.files.absolute(folder + "/saves/");
file.mkdirs();
- file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
+ file = Gdx.files.absolute(folder + "/saves/game.sav");
+
try {
ObjectOutputStream out = new ObjectOutputStream(file.write(false));
- out.writeInt(VERSION);
- out.writeObject(gp);
+ out.writeInt(SAVE_VERSION);
+ out.writeObject(dropController);
+ out.writeObject(mobsController);
out.close();
- saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"), gp.world.getFullForeMap());
- saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"), gp.world.getFullBackMap());
+ // TODO: 4/20/24 save map
+// saveMap(Gdx.files.absolute(folder + "/saves/foremap.sav"), gameWorld.getFullForeMap());
+// saveMap(Gdx.files.absolute(folder + "/saves/backmap.sav"), gameWorld.getFullBackMap());
} catch (Exception e) {
e.printStackTrace();
- }
+ }*/
}
- public static boolean exists() {
- return (Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav").exists() &&
- Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav").exists() &&
- Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav").exists());
+ public static boolean exists(MainConfig mainConfig) {
+ return false;
+// String folder = mainConfig.getGameFolder();
+// return (Gdx.files.absolute(folder + "/saves/game.sav").exists() &&
+// Gdx.files.absolute(folder + "/saves/foremap.sav").exists() &&
+// Gdx.files.absolute(folder + "/saves/backmap.sav").exists());
}
-
}