DEADSOFTWARE

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