41967a283da7e6d89a2270807b77ac0870457334
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
;
7 import java
.nio
.ByteBuffer
;
9 public class WorldSaver
{
11 private static final int VERSION
= 0;
13 private static int[][] fMap
, bMap
;
14 private static int readIndex
;
16 private static int bytesInt(byte[] bytes
) {
17 ByteBuffer wrapped
= ByteBuffer
.wrap(bytes
);
18 int res
= wrapped
.getInt(readIndex
);
23 private static void writeInt(FileHandle file
, int i
, boolean append
) {
24 byte[] bytes
= ByteBuffer
.allocate(4).putInt(i
).array();
25 file
.writeBytes(bytes
, append
);
28 private static void saveMap(FileHandle file
, int[][] map
) {
29 int width
= map
.length
;
30 int height
= map
[0].length
;
31 writeInt(file
, VERSION
, false);
32 writeInt(file
, width
, true);
33 writeInt(file
, height
, true);
34 for (int y
=0; y
<map
[0].length
; y
++) {
37 for (int x
=0; x
<map
.length
; x
++) {
38 if (map
[x
][y
]!=bl
|| x
==map
.length
-1) {
39 writeInt(file
, rl
, true);
40 writeInt(file
, bl
, true);
50 private static int[][] loadMap(FileHandle file
) {
52 int ver
, width
, height
;
54 byte[] data
= file
.readBytes();
58 width
= bytesInt(data
);
59 height
= bytesInt(data
);
60 map
= new int[width
][height
];
61 for (int y
=0; y
<height
; y
++) {
62 for (int x
=0; x
<width
; x
+=rl
) {
65 for (int i
=x
; i
<x
+rl
; i
++) map
[i
][y
] = bl
;
72 public static int[][] getLoadedForeMap() {
76 public static int[][] getLoadedBackMap() {
80 public static void load() {
81 fMap
= loadMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/foremap.sav"));
82 bMap
= loadMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/backmap.sav"));
85 public static void save(int[][] foreMap
, int[][] backMap
) {
86 Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/").mkdirs();
87 saveMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/foremap.sav"), foreMap
);
88 saveMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/backmap.sav"), backMap
);
91 public static boolean exists() {
92 return (Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/foremap.sav").exists() &&
93 Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/backmap.sav").exists());