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));
}
public void pause() { }
public void resume() { }
abstract public boolean isGameOver();
abstract protected boolean isNewRecord();
abstract public int getCurrentScore();
abstract public void saveScore();
abstract protected boolean isNewRecord();
public void draw(SpriteBatch batch) {
// 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);

View file

@ -10,7 +10,7 @@ public class TimeScorer extends BaseScorer {
//region Members
private final long startTime;
private long startTime;
// 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`
@ -18,6 +18,10 @@ public class TimeScorer extends BaseScorer {
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
//region Constructor
@ -28,6 +32,8 @@ public class TimeScorer extends BaseScorer {
startTime = TimeUtils.nanoTime();
deadTime = startTime + START_TIME;
pausedTimeLeft = -1;
}
//endregion
@ -48,9 +54,8 @@ public class TimeScorer extends BaseScorer {
return (long)((score / 4.0) * 1e+09);
}
@Override
public boolean isGameOver() {
return TimeUtils.nanoTime() > deadTime;
private int getTimeLeft() {
return Math.max(nanosToSeconds(deadTime - TimeUtils.nanoTime()), 0);
}
//endregion
@ -62,6 +67,11 @@ public class TimeScorer extends BaseScorer {
return nanosToSeconds(deadTime - startTime);
}
@Override
public boolean isGameOver() {
return TimeUtils.nanoTime() > deadTime;
}
@Override
public void saveScore() {
// TODO Save high time score
@ -73,9 +83,27 @@ public class TimeScorer extends BaseScorer {
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
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));
super.draw(batch);

View file

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

View file

@ -40,7 +40,7 @@ class PauseMenuStage extends Stage {
//region Constructor
// 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;
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() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new GameScreen(game, GameScreen.GAME_MODE_SCORE));
game.setScreen(new GameScreen(game, gameMode));
dispose();
}
});
@ -122,6 +122,7 @@ class PauseMenuStage extends Stage {
}
}
));
scorer.resume();
}
//endregion
@ -130,6 +131,7 @@ class PauseMenuStage extends Stage {
// Shows the pause menu, indicating whether it's game over or not
void show(final boolean gameOver) {
scorer.pause();
scorer.saveScore();
// Save the last input processor so then we can return the handle to it