38e295931f27b864ed98fa94ecad05698e323e94
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
;
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();
19 private static void saveMap(FileHandle file
, int[][] map
) throws IOException
{
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
++) {
30 for (int[] ints
: map
) {
32 out
.write(intToBytes(rl
));
33 out
.write(intToBytes(bl
));
39 out
.write(intToBytes(rl
));
40 out
.write(intToBytes(bl
));
47 private static int[][] loadMap(FileHandle file
) throws Exception
{
49 int ver
, width
, height
;
51 DataInputStream in
= new DataInputStream(file
.read());
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
) {
61 for (int i
= x
; i
< x
+ rl
; i
++) map
[i
][y
] = bl
;
65 } else throw new Exception("version mismatch");
69 public static GameProc
load() {
70 FileHandle file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/game.sav");
71 GameProc gameProc
= null;
73 ObjectInputStream in
= new ObjectInputStream(file
.read());
74 int ver
= in
.readInt();
75 if (VERSION
== ver
) gameProc
= (GameProc
) in
.readObject();
76 else throw new Exception("version mismatch");
78 gameProc
.world
= new GameWorld(
79 loadMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/foremap.sav")),
80 loadMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/backmap.sav"))
82 gameProc
.physics
= new GamePhysics();
83 gameProc
.input
= new GameInput();
84 } catch (Exception e
) {
85 Gdx
.app
.error("GameSaver", e
.getMessage(), e
);
91 public static void save(GameProc gp
) {
92 FileHandle file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/");
94 file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/game.sav");
96 ObjectOutputStream out
= new ObjectOutputStream(file
.write(false));
97 out
.writeInt(VERSION
);
100 saveMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/foremap.sav"), gp
.world
.getFullForeMap());
101 saveMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/backmap.sav"), gp
.world
.getFullBackMap());
102 } catch (Exception e
) {
107 public static boolean exists() {
108 return (Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/game.sav").exists() &&
109 Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/foremap.sav").exists() &&
110 Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/backmap.sav").exists());