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 java.io.*; import java.nio.ByteBuffer; public class GameSaver { private static final int VERSION = 0; private static byte[] intToBytes(int i) { return ByteBuffer.allocate(4).putInt(i).array(); } private static void saveMap(FileHandle file, int[][] map) throws IOException{ int rl,bl; int width = map.length; int height = map[0].length; 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(); } return gameProc; } public static void save(GameProc gameProc) { FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/"); file.mkdirs(); file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav"); try { ObjectOutputStream out = new ObjectOutputStream(file.write(false)); 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(); } } public static boolean exists() { return (Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav").exists() && Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav").exists() && Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav").exists()); } }