X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavecraft%2Fgame%2FGameSaver.java;h=2b5b906d877a3dadcd77baca27e1e346d8eeeff1;hb=e14d8af238926a5cbfc067cfc9a9b032e51f1cac;hp=070b5c24009524548980c60dee82cb0a86328930;hpb=6ab2a53526f16de139d39a59c3d800e5f3013c68;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameSaver.java b/core/src/ru/deadsoftware/cavecraft/game/GameSaver.java index 070b5c2..2b5b906 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameSaver.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameSaver.java @@ -2,96 +2,69 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; +import com.badlogic.gdx.utils.ArrayMap; import ru.deadsoftware.cavecraft.CaveGame; -import ru.deadsoftware.cavecraft.Items; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; +import java.io.*; import java.nio.ByteBuffer; public class GameSaver { private static final int VERSION = 0; - private static int[][] fMap, bMap; - private static int readIndex; - - private static int bytesInt(byte[] bytes) { - ByteBuffer wrapped = ByteBuffer.wrap(bytes); - int res = wrapped.getInt(readIndex); - readIndex+=4; - return res; + private static byte[] intToBytes(int i) { + return ByteBuffer.allocate(4).putInt(i).array(); } - private static void writeInt(FileHandle file, int i, boolean append) { - byte[] bytes = ByteBuffer.allocate(4).putInt(i).array(); - file.writeBytes(bytes, append); - } - - private static void saveMap(FileHandle file, int[][] map) { + private static void saveMap(FileHandle file, int[][] map) throws IOException{ int rl,bl; int width = map.length; int height = map[0].length; - writeInt(file, VERSION, false); - writeInt(file, width, true); - writeInt(file, height, true); + BufferedOutputStream out = new BufferedOutputStream(file.write(false)); + out.write(intToBytes(VERSION)); + out.write(intToBytes(width)); + out.write(intToBytes(height)); for (int y=0; y(); + gameProc.physics = new GamePhysics(gameProc); + gameProc.resetRenderer(); } catch (Exception e) { Gdx.app.error("GameSaver",e.getMessage(),e); + Gdx.app.exit(); } - gameProc.world = new GameWorld(); - gameProc.world.load(); - gameProc.physics = new GamePhysics(gameProc); - gameProc.resetRenderer(); - fMap = null; - bMap = null; return gameProc; } @@ -122,11 +98,11 @@ public class GameSaver { out.writeInt(VERSION); out.writeObject(gameProc); out.close(); + saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"), gameProc.world.getFullForeMap()); + saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"), gameProc.world.getFullBackMap()); } catch (Exception e) { e.printStackTrace(); } - saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"), gameProc.world.getFullForeMap()); - saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"), gameProc.world.getFullBackMap()); } public static boolean exists() {