Pause menu now pauses the time mode*

Also conserve gamemode on "Replay"
This commit is contained in:
Lonami Exo 2017-02-05 13:22:40 +01:00
parent bf0fa208f5
commit d35c8481e5
4 changed files with 49 additions and 18 deletions

View file

@ -75,14 +75,15 @@ public abstract class BaseScorer {
addScore(calculateClearScore(stripsCleared, boardSize)); addScore(calculateClearScore(stripsCleared, boardSize));
} }
public void pause() { }
public void resume() { }
abstract public boolean isGameOver(); abstract public boolean isGameOver();
abstract protected boolean isNewRecord();
abstract public int getCurrentScore(); abstract public int getCurrentScore();
abstract public void saveScore(); abstract public void saveScore();
abstract protected boolean isNewRecord();
public void draw(SpriteBatch batch) { public void draw(SpriteBatch batch) {
// If we beat a new record, the cup color will linear interpolate to the high score color // If we beat a new record, the cup color will linear interpolate to the high score color
cupColor.lerp(isNewRecord() ? Klooni.theme.highScore : Klooni.theme.currentScore, 0.05f); cupColor.lerp(isNewRecord() ? Klooni.theme.highScore : Klooni.theme.currentScore, 0.05f);

View file

@ -10,7 +10,7 @@ public class TimeScorer extends BaseScorer {
//region Members //region Members
private final long startTime; private long startTime;
// Indicates where we would die in time. Score adds to this, so we take // Indicates where we would die in time. Score adds to this, so we take
// longer to die. To get the "score" we simply calculate `deadTime - startTime` // longer to die. To get the "score" we simply calculate `deadTime - startTime`
@ -18,6 +18,10 @@ public class TimeScorer extends BaseScorer {
private static final long START_TIME = 20 * 1000000000L; private static final long START_TIME = 20 * 1000000000L;
// We need to know when the game was paused to "stop" counting
private long pauseTime;
private int pausedTimeLeft;
//endregion //endregion
//region Constructor //region Constructor
@ -28,6 +32,8 @@ public class TimeScorer extends BaseScorer {
startTime = TimeUtils.nanoTime(); startTime = TimeUtils.nanoTime();
deadTime = startTime + START_TIME; deadTime = startTime + START_TIME;
pausedTimeLeft = -1;
} }
//endregion //endregion
@ -48,9 +54,8 @@ public class TimeScorer extends BaseScorer {
return (long)((score / 4.0) * 1e+09); return (long)((score / 4.0) * 1e+09);
} }
@Override private int getTimeLeft() {
public boolean isGameOver() { return Math.max(nanosToSeconds(deadTime - TimeUtils.nanoTime()), 0);
return TimeUtils.nanoTime() > deadTime;
} }
//endregion //endregion
@ -62,6 +67,11 @@ public class TimeScorer extends BaseScorer {
return nanosToSeconds(deadTime - startTime); return nanosToSeconds(deadTime - startTime);
} }
@Override
public boolean isGameOver() {
return TimeUtils.nanoTime() > deadTime;
}
@Override @Override
public void saveScore() { public void saveScore() {
// TODO Save high time score // TODO Save high time score
@ -73,9 +83,27 @@ public class TimeScorer extends BaseScorer {
return false; return false;
} }
@Override
public void pause() {
pauseTime = TimeUtils.nanoTime();
pausedTimeLeft = getTimeLeft();
}
@Override
public void resume() {
if (pauseTime != 0L) {
long difference = TimeUtils.nanoTime() - pauseTime;
startTime += difference;
deadTime += difference;
pauseTime = 0L;
pausedTimeLeft = -1;
}
}
@Override @Override
public void draw(SpriteBatch batch) { public void draw(SpriteBatch batch) {
int timeLeft = Math.max(nanosToSeconds(deadTime - TimeUtils.nanoTime()), 0); int timeLeft = pausedTimeLeft < 0 ? getTimeLeft() : pausedTimeLeft;
leftLabel.setText(Integer.toString(timeLeft)); leftLabel.setText(Integer.toString(timeLeft));
super.draw(batch); super.draw(batch);

View file

@ -22,7 +22,7 @@ class GameScreen implements Screen, InputProcessor {
//region Members //region Members
private final BaseScorer scorerlol; private final BaseScorer scorer;
private final Board board; private final Board board;
private final PieceHolder holder; private final PieceHolder holder;
@ -57,10 +57,10 @@ class GameScreen implements Screen, InputProcessor {
final GameLayout layout = new GameLayout(); final GameLayout layout = new GameLayout();
switch (gameMode) { switch (gameMode) {
case GAME_MODE_SCORE: case GAME_MODE_SCORE:
scorerlol = new Scorer(game, layout); scorer = new Scorer(game, layout);
break; break;
case GAME_MODE_TIME: case GAME_MODE_TIME:
scorerlol = new TimeScorer(game, layout); scorer = new TimeScorer(game, layout);
break; break;
default: default:
throw new RuntimeException("Unknown game mode given: "+gameMode); throw new RuntimeException("Unknown game mode given: "+gameMode);
@ -68,7 +68,7 @@ class GameScreen implements Screen, InputProcessor {
board = new Board(layout, BOARD_SIZE); board = new Board(layout, BOARD_SIZE);
holder = new PieceHolder(layout, HOLDER_PIECE_COUNT, board.cellSize); holder = new PieceHolder(layout, HOLDER_PIECE_COUNT, board.cellSize);
pauseMenu = new PauseMenuStage(layout, game, scorerlol); pauseMenu = new PauseMenuStage(layout, game, scorer, gameMode);
gameOverSound = Gdx.audio.newSound(Gdx.files.internal("sound/game_over.mp3")); gameOverSound = Gdx.audio.newSound(Gdx.files.internal("sound/game_over.mp3"));
} }
@ -109,13 +109,13 @@ class GameScreen implements Screen, InputProcessor {
Klooni.theme.glClearBackground(); Klooni.theme.glClearBackground();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (scorerlol.isGameOver() && !pauseMenu.isShown()) { if (scorer.isGameOver() && !pauseMenu.isShown()) {
doGameOver(); doGameOver();
} }
batch.begin(); batch.begin();
scorerlol.draw(batch); scorer.draw(batch);
board.draw(batch); board.draw(batch);
holder.update(); holder.update();
holder.draw(batch); holder.draw(batch);
@ -158,8 +158,8 @@ class GameScreen implements Screen, InputProcessor {
return false; return false;
if (action == PieceHolder.ON_BOARD_DROP) { if (action == PieceHolder.ON_BOARD_DROP) {
scorerlol.addPieceScore(area); scorer.addPieceScore(area);
scorerlol.addBoardScore(board.clearComplete(), board.cellCount); scorer.addBoardScore(board.clearComplete(), board.cellCount);
// After the piece was put, check if it's game over // After the piece was put, check if it's game over
if (isGameOver()) { if (isGameOver()) {

View file

@ -40,7 +40,7 @@ class PauseMenuStage extends Stage {
//region Constructor //region Constructor
// We need the score to save the maximum score if a new record was beaten // We need the score to save the maximum score if a new record was beaten
PauseMenuStage(final GameLayout layout, final Klooni game, final BaseScorer scorer) { PauseMenuStage(final GameLayout layout, final Klooni game, final BaseScorer scorer, final int gameMode) {
this.scorer = scorer; this.scorer = scorer;
shapeRenderer = new ShapeRenderer(20); // 20 vertex seems to be enough for a rectangle shapeRenderer = new ShapeRenderer(20); // 20 vertex seems to be enough for a rectangle
@ -72,7 +72,7 @@ class PauseMenuStage extends Stage {
replayButton.addListener(new ChangeListener() { replayButton.addListener(new ChangeListener() {
@Override @Override
public void changed(ChangeEvent event, Actor actor) { public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new GameScreen(game, GameScreen.GAME_MODE_SCORE)); game.setScreen(new GameScreen(game, gameMode));
dispose(); dispose();
} }
}); });
@ -122,6 +122,7 @@ class PauseMenuStage extends Stage {
} }
} }
)); ));
scorer.resume();
} }
//endregion //endregion
@ -130,6 +131,7 @@ class PauseMenuStage extends Stage {
// Shows the pause menu, indicating whether it's game over or not // Shows the pause menu, indicating whether it's game over or not
void show(final boolean gameOver) { void show(final boolean gameOver) {
scorer.pause();
scorer.saveScore(); scorer.saveScore();
// Save the last input processor so then we can return the handle to it // Save the last input processor so then we can return the handle to it