Skip to content

Commit

Permalink
Pause menu now pauses the time mode*
Browse files Browse the repository at this point in the history
Also conserve gamemode on "Replay"
  • Loading branch information
Lonami committed Feb 5, 2017
1 parent bf0fa20 commit d35c848
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
7 changes: 4 additions & 3 deletions core/src/io/github/lonamiwebs/klooni/game/BaseScorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ public final void addBoardScore(int stripsCleared, int boardSize) {
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);
Expand Down
38 changes: 33 additions & 5 deletions core/src/io/github/lonamiwebs/klooni/game/TimeScorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ 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`
private long deadTime;

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
Expand All @@ -28,6 +32,8 @@ public TimeScorer(final Klooni game, GameLayout layout) {

startTime = TimeUtils.nanoTime();
deadTime = startTime + START_TIME;

pausedTimeLeft = -1;
}

//endregion
Expand All @@ -48,9 +54,8 @@ private long scoreToNanos(int score) {
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
Expand All @@ -62,6 +67,11 @@ public int getCurrentScore() {
return nanosToSeconds(deadTime - startTime);
}

@Override
public boolean isGameOver() {
return TimeUtils.nanoTime() > deadTime;
}

@Override
public void saveScore() {
// TODO Save high time score
Expand All @@ -73,9 +83,27 @@ protected boolean isNewRecord() {
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);
Expand Down
16 changes: 8 additions & 8 deletions core/src/io/github/lonamiwebs/klooni/screens/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,18 +57,18 @@ 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);
}

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"));
}
Expand Down Expand Up @@ -109,13 +109,13 @@ public void render(float delta) {
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);
Expand Down Expand Up @@ -158,8 +158,8 @@ public boolean touchUp(int screenX, int screenY, int pointer, int button) {
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,7 +72,7 @@ public void changed (ChangeEvent event, Actor actor) {
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();
}
});
Expand Down Expand Up @@ -122,6 +122,7 @@ public void run() {
}
}
));
scorer.resume();
}

//endregion
Expand All @@ -130,6 +131,7 @@ public void run() {

// 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
Expand Down

0 comments on commit d35c848

Please sign in to comment.