1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.files
.FileHandle
;
5 import com
.badlogic
.gdx
.utils
.ArrayMap
;
6 import ru
.deadsoftware
.cavecraft
.CaveGame
;
9 import java
.nio
.ByteBuffer
;
11 public class GameSaver
{
13 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
<map
[0].length
; y
++) {
30 for (int x
=0; x
<map
.length
; x
++) {
32 out
.write(intToBytes(rl
));
33 out
.write(intToBytes(bl
));
39 out
.write(intToBytes(rl
));
40 out
.write(intToBytes(bl
));
46 private static int[][] loadMap(FileHandle file
) throws Exception
{
48 int ver
, width
, height
;
50 DataInputStream in
= new DataInputStream(file
.read());
54 height
= in
.readInt();
55 map
= new int[width
][height
];
56 for (int y
=0; y
<height
; y
++) {
57 for (int x
=0; x
<width
; x
+=rl
) {
60 for (int i
=x
; i
<x
+rl
; i
++) map
[i
][y
] = bl
;
64 } else throw new Exception("version mismatch");
68 public static GameProc
load() {
69 FileHandle file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/game.sav");
70 GameProc gameProc
= null;
72 ObjectInputStream in
= new ObjectInputStream(file
.read());
73 int ver
= in
.readInt();
74 if (VERSION
== ver
) gameProc
= (GameProc
)in
.readObject();
75 else throw new Exception("version mismatch");
77 gameProc
.world
= new GameWorld();
78 gameProc
.world
.setMaps(
79 loadMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/foremap.sav")),
80 loadMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/backmap.sav"))
82 gameProc
.world
.metaMap
= new ArrayMap
<String
, Integer
>();
83 gameProc
.physics
= new GamePhysics(gameProc
);
84 gameProc
.resetRenderer();
85 } catch (Exception e
) {
86 Gdx
.app
.error("GameSaver",e
.getMessage(),e
);
92 public static void save(GameProc gameProc
) {
93 FileHandle file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/");
95 file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/game.sav");
97 ObjectOutputStream out
= new ObjectOutputStream(file
.write(false));
98 out
.writeInt(VERSION
);
99 out
.writeObject(gameProc
);
101 saveMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/foremap.sav"), gameProc
.world
.getFullForeMap());
102 saveMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/backmap.sav"), gameProc
.world
.getFullBackMap());
103 } catch (Exception e
) {
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());