DEADSOFTWARE

Move some methods
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameSaver.java
1 package ru.deadsoftware.cavedroid.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.files.FileHandle;
5 import org.jetbrains.annotations.NotNull;
6 import ru.deadsoftware.cavedroid.CaveGame;
8 import java.io.*;
9 import java.nio.ByteBuffer;
11 public class GameSaver {
13 private static final int VERSION = 0;
15 @NotNull
16 private static byte[] intToBytes(int i) {
17 return ByteBuffer.allocate(4).putInt(i).array();
18 }
20 private static void saveMap(@NotNull FileHandle file, @NotNull int[][] map) throws IOException {
21 int rl, bl;
22 int width = map.length;
23 int height = map[0].length;
24 BufferedOutputStream out = new BufferedOutputStream(file.write(false));
25 out.write(intToBytes(VERSION));
26 out.write(intToBytes(width));
27 out.write(intToBytes(height));
28 for (int y = 0; y < height; y++) {
29 bl = map[0][y];
30 rl = 0;
31 for (int[] ints : map) {
32 if (ints[y] != bl) {
33 out.write(intToBytes(rl));
34 out.write(intToBytes(bl));
35 rl = 0;
36 bl = ints[y];
37 }
38 rl++;
39 }
40 out.write(intToBytes(rl));
41 out.write(intToBytes(bl));
42 }
43 out.flush();
44 out.close();
45 }
47 @NotNull
48 private static int[][] loadMap(@NotNull FileHandle file) throws Exception {
49 int[][] map;
50 int ver, width, height;
51 int rl, bl;
52 DataInputStream in = new DataInputStream(file.read());
53 ver = in.readInt();
54 if (VERSION == ver) {
55 width = in.readInt();
56 height = in.readInt();
57 map = new int[width][height];
58 for (int y = 0; y < height; y++) {
59 for (int x = 0; x < width; x += rl) {
60 rl = in.readInt();
61 bl = in.readInt();
62 for (int i = x; i < x + rl; i++) map[i][y] = bl;
63 }
64 }
65 in.close();
66 } else throw new Exception("version mismatch");
67 return map;
68 }
70 public static GameProc load() {
71 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
72 GameProc gameProc = null;
73 try {
74 ObjectInputStream in = new ObjectInputStream(file.read());
75 int ver = in.readInt();
76 if (VERSION == ver) gameProc = (GameProc) in.readObject();
77 else throw new Exception("version mismatch");
78 in.close();
79 gameProc.world = new GameWorld(
80 loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav")),
81 loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"))
82 );
83 gameProc.physics = new GamePhysics();
84 gameProc.input = new GameInput();
85 } catch (Exception e) {
86 Gdx.app.error("GameSaver", e.getMessage(), e);
87 Gdx.app.exit();
88 }
89 return gameProc;
90 }
92 public static void save(GameProc gp) {
93 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/");
94 file.mkdirs();
95 file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
96 try {
97 ObjectOutputStream out = new ObjectOutputStream(file.write(false));
98 out.writeInt(VERSION);
99 out.writeObject(gp);
100 out.close();
101 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"), gp.world.getFullForeMap());
102 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"), gp.world.getFullBackMap());
103 } catch (Exception e) {
104 e.printStackTrace();
108 public static boolean exists() {
109 return (Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav").exists() &&
110 Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav").exists() &&
111 Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav").exists());