Save on pause, don't save time mode, ignore *.sav files

This commit is contained in:
Lonami Exo 2017-02-10 13:14:15 +01:00
parent f954766d99
commit c02c00ee64
5 changed files with 27 additions and 21 deletions

3
.gitignore vendored
View file

@ -1,3 +1,6 @@
## Klooni save data
*.sav
## Java
*.class

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

View file

@ -44,7 +44,6 @@ public class Klooni extends Game {
skin.add("play_saved_texture", new Texture(Gdx.files.internal("ui/play_saved.png")));
skin.add("star_texture", new Texture(Gdx.files.internal("ui/star.png")));
skin.add("stopwatch_texture", new Texture(Gdx.files.internal("ui/stopwatch.png")));
skin.add("stopwatch_saved_texture", new Texture(Gdx.files.internal("ui/stopwatch_saved.png")));
skin.add("palette_texture", new Texture(Gdx.files.internal("ui/palette.png")));
skin.add("home_texture", new Texture(Gdx.files.internal("ui/home.png")));
skin.add("replay_texture", new Texture(Gdx.files.internal("ui/replay.png")));

View file

@ -88,15 +88,16 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
gameOverSound = Gdx.audio.newSound(Gdx.files.internal("sound/game_over.mp3"));
if (gameMode == GAME_MODE_SCORE) {
if (loadSave) {
// The user might have a previous game. If this is the case, load it
tryLoad();
}
else {
} else {
// Ensure that there is no old save, we don't want to load it, thus delete it
deleteSave();
}
}
}
//endregion
@ -121,6 +122,7 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
gameOverSound.play();
// The user should not be able to return to the game if its game over
if (gameMode == GAME_MODE_SCORE)
deleteSave();
}
}
@ -137,9 +139,15 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
Gdx.input.setInputProcessor(this);
}
// Save the state, the user might leave the game in any of the following 2 methods
private void showPauseMenu() {
pauseMenu.show(false);
save(); // Save the state, the user might leave the game
save();
}
@Override
public void pause() {
save();
}
@Override
@ -214,14 +222,11 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
@Override
public void resize(int width, int height) { }
@Override
public void pause() { }
@Override
public void resume() { }
@Override
public void hide() { }
public void hide() { /* Hide can only be called if the menu was shown. Place logic there. */ }
@Override
public boolean keyDown(int keycode) {
@ -253,8 +258,10 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
//region Saving and loading
private void save() {
// Only save if the game is not over
if (gameOverDone)
// Only save if the game is not over and the game mode is not the time mode. It
// makes no sense to save the time game mode since it's supposed to be something quick.
// Don't save either if the score is 0, which means the player did nothing.
if (gameOverDone || gameMode != GAME_MODE_SCORE || scorer.getCurrentScore() == 0)
return;
final FileHandle handle = Gdx.files.local(SAVE_DAT_FILENAME);
@ -266,7 +273,7 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
}
}
static void deleteSave() {
private void deleteSave() {
final FileHandle handle = Gdx.files.local(SAVE_DAT_FILENAME);
if (handle.exists())
handle.delete();
@ -277,7 +284,6 @@ class GameScreen implements Screen, InputProcessor, BinSerializable {
}
private boolean tryLoad() {
// Load will fail if the game modes differ, but that's okay
final FileHandle handle = Gdx.files.local(SAVE_DAT_FILENAME);
if (handle.exists()) {
try {

View file

@ -66,9 +66,7 @@ public class MainMenuScreen extends InputListener implements Screen {
table.add(starButton).space(16);
// Time mode
// TODO Would be nice if it actually showed the saved texture iff that's the saved game mode
final SoftButton statsButton = new SoftButton(
2, GameScreen.hasSavedData() ? "stopwatch_saved_texture" : "stopwatch_texture");
final SoftButton statsButton = new SoftButton(2, "stopwatch_texture");
statsButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {