1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.files
.FileHandle
;
5 import ru
.deadsoftware
.cavecraft
.CaveGame
;
6 import ru
.deadsoftware
.cavecraft
.Items
;
8 import java
.io
.ObjectInputStream
;
9 import java
.io
.ObjectOutputStream
;
10 import java
.nio
.ByteBuffer
;
12 public class GameSaver
{
14 private static final int VERSION
= 0;
16 private static int[][] fMap
, bMap
;
17 private static int readIndex
;
19 private static int bytesInt(byte[] bytes
) {
20 ByteBuffer wrapped
= ByteBuffer
.wrap(bytes
);
21 int res
= wrapped
.getInt(readIndex
);
26 private static void writeInt(FileHandle file
, int i
, boolean append
) {
27 byte[] bytes
= ByteBuffer
.allocate(4).putInt(i
).array();
28 file
.writeBytes(bytes
, append
);
31 private static void saveMap(FileHandle file
, int[][] map
) {
33 int width
= map
.length
;
34 int height
= map
[0].length
;
35 writeInt(file
, VERSION
, false);
36 writeInt(file
, width
, true);
37 writeInt(file
, height
, true);
38 for (int y
=0; y
<map
[0].length
; y
++) {
41 for (int x
=0; x
<map
.length
; x
++) {
42 if (map
[x
][y
]!=bl
|| x
==map
.length
-1) {
43 if (x
==map
.length
-1) rl
++;
44 writeInt(file
, rl
, true);
45 writeInt(file
, bl
, true);
54 private static int[][] loadMap(FileHandle file
) throws Exception
{
56 int ver
, width
, height
;
58 byte[] data
= file
.readBytes();
62 width
= bytesInt(data
);
63 height
= bytesInt(data
);
64 map
= new int[width
][height
];
65 for (int y
=0; y
<height
; y
++) {
66 for (int x
=0; x
<width
; x
+=rl
) {
69 for (int i
=x
; i
<x
+rl
; i
++) map
[i
][y
] = bl
;
72 } else throw new Exception("version mismatch");
76 public static int[][] getLoadedForeMap() {
80 public static int[][] getLoadedBackMap() {
84 public static void loadMap() {
86 fMap
= loadMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/foremap.sav"));
87 bMap
= loadMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/backmap.sav"));
88 } catch (Exception e
) {
89 Gdx
.app
.error("GameSaver",e
.getMessage(),e
);
93 public static GameProc
load() {
94 FileHandle file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/game.sav");
95 GameProc gameProc
= null;
97 ObjectInputStream in
= new ObjectInputStream(file
.read());
98 int ver
= in
.readInt();
99 if (VERSION
== ver
) gameProc
= (GameProc
)in
.readObject();
100 else throw new Exception("version mismatch");
102 } catch (Exception e
) {
103 Gdx
.app
.error("GameSaver",e
.getMessage(),e
);
105 gameProc
.world
= new GameWorld();
106 gameProc
.world
.load();
107 gameProc
.physics
= new GamePhysics(gameProc
);
108 gameProc
.resetRenderer();
114 public static void save(GameProc gameProc
) {
115 FileHandle file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/");
117 file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/game.sav");
119 ObjectOutputStream out
= new ObjectOutputStream(file
.write(false));
120 out
.writeInt(VERSION
);
121 out
.writeObject(gameProc
);
123 } catch (Exception e
) {
126 saveMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/foremap.sav"), gameProc
.world
.getFullForeMap());
127 saveMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/backmap.sav"), gameProc
.world
.getFullBackMap());
130 public static boolean exists() {
131 return (Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/game.sav").exists() &&
132 Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/foremap.sav").exists() &&
133 Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/backmap.sav").exists());