1. André Schnabel
  2. Weltraumsoldat3D

Commits

André Schnabel  committed afd8413

autosave/load

  • Participants
  • Parent commits 91ca3b7
  • Branches default

Comments (0)

Files changed (1)

File src/com/andreschnabel/weltraumsoldat3d/GameScreen.java

View file
 package com.andreschnabel.weltraumsoldat3d;
 
-import static com.andreschnabel.weltraumsoldat3d.Constants.*;
+import static com.andreschnabel.weltraumsoldat3d.Constants.ANDROID_TEST;
+import static com.andreschnabel.weltraumsoldat3d.Constants.GAME_OVER_DUR;
+import static com.andreschnabel.weltraumsoldat3d.Constants.GRAB_MOUSE;
+import static com.andreschnabel.weltraumsoldat3d.Constants.MSG_DUR;
+import static com.andreschnabel.weltraumsoldat3d.Constants.NUM_MAPS;
+
+import java.io.BufferedWriter;
+import java.io.IOException;
 
 import com.andreschnabel.weltraumsoldat3d.entities.Player;
 import com.andreschnabel.weltraumsoldat3d.framework.BasicScreen;
 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.Texture.TextureFilter;
 import com.badlogic.gdx.graphics.g2d.TextureAtlas;
+import com.badlogic.gdx.math.Vector3;
 
 /**
  * Main game screen.
 	 * needed to measure game duration. */	
 	private long gameStartTime;
 	
+	
+	private int loadedMapNum = -1;
+	private Vector3 loadedCamPos = null;
+	
 	//private FPSLogger fpsLogger = new FPSLogger();
 	
+	private static final String SAVEGAME_FILENAME = "weltraumsoldat3Dsavegame";
+	
 	/**
 	 * Constructs a new game screen.
 	 * @param mapNum number of the map to start with.
 	public void show() {
 		super.setup();
 		
+		loadGame();
+		
 		player = new Player();
 		
 		atlas = new TextureAtlas(Utils.loadRes("pack"));
 		doorAnimMgr = DoorAnimationManager.getNewInstance();		
 		soundManager = SoundManager.getInstance();
 		
-		curMap = mapNum;
+		curMap = (loadedMapNum == -1) ? mapNum : loadedMapNum;
 		loadMap(curMap);
 		
+		if(loadedCamPos != null) {
+			cam.position.set(loadedCamPos.x, 0, loadedCamPos.z);
+		}
+		
 		soundManager.playSnd(SoundManager.START_RND_SND);
 		addInstructions();
 		
 				hudRenderer.getBitmapFont(), atlas.findRegion("button"));
 	}
 	
+	private void loadGame() {
+		String fstr = Gdx.files.external(SAVEGAME_FILENAME).readString();
+		if(fstr.length() == 0) return;
+		String parts[] = fstr.split("\n");
+		if(parts.length != 3) return;
+		loadedMapNum = Integer.valueOf(parts[0]);
+		loadedCamPos = new Vector3(Float.valueOf(parts[1]), 0, Float.valueOf(parts[2]));
+	}
+
 	private String[] instructions = {
 			"Welcome to Weltraumsoldat3D.",
 			/*"Use the top diplay border to walk forward.",
 	
 	@Override
 	public void dispose() {
+		saveGame();
+		
 		disposeMapRelated();
 		hudRenderer.dispose();
 		atlas.dispose();
 		doorAnimMgr.dispose();
 	}
 	
+	private void saveGame() {
+		BufferedWriter bw = new BufferedWriter(
+				Gdx.files.external(SAVEGAME_FILENAME)
+				.writer(false));
+		try {
+			bw.write(curMap + "\n" + cam.position.x + "\n" + cam.position.z);
+			bw.flush();
+			bw.close();
+		} catch (IOException e) {
+			System.out.println("Unable to save game state!");
+			e.printStackTrace();
+		}
+		
+	}
+
 	@Override
 	public void render(final float delta) {
 		if(Gdx.input.isKeyPressed(Keys.ESCAPE))