Save maximum score

This commit is contained in:
Lonami Exo 2017-01-27 19:57:13 +01:00
parent b6b7590cf4
commit daef6d047e
3 changed files with 36 additions and 16 deletions

View file

@ -1,6 +1,7 @@
package io.github.lonamiwebs.klooni.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
@ -13,7 +14,10 @@ import com.badlogic.gdx.utils.Align;
public class Scorer {
private int currentScore;
private final Preferences prefs;
private int currentScore, maxScore;
private boolean newRecord;
private float shownScore; // To interpolate between shown score -> real score
private final int boardSize;
@ -24,7 +28,10 @@ public class Scorer {
final Rectangle cupArea;
public Scorer(GameLayout layout, int boardSize) {
prefs = Gdx.app.getPreferences("io.github.lonamiwebs.klooni.game");
currentScore = 0;
maxScore = prefs.getInteger("maxScore", 0);
this.boardSize = boardSize;
cupTexture = new Texture(Gdx.files.internal("ui/cup.png"));
@ -35,17 +42,29 @@ public class Scorer {
currentScoreLabel = new Label("0", scoreStyle);
currentScoreLabel.setAlignment(Align.right);
maxScoreLabel = new Label("0", scoreStyle);
maxScoreLabel = new Label(Integer.toString(maxScore), scoreStyle);
layout.update(this);
}
public void addPieceScore(int areaPut) {
currentScore += areaPut;
addScore(areaPut);
}
public void addBoardScore(int stripsCleared) {
currentScore += calculateClearScore(stripsCleared);
addScore(calculateClearScore(stripsCleared));
}
private void addScore(int score) {
currentScore += score;
newRecord = currentScore > maxScore;
}
public void saveScore() {
if (newRecord) {
prefs.putInteger("maxScore", currentScore);
prefs.flush();
}
}
int calculateClearScore(int stripsCleared) {

View file

@ -17,9 +17,7 @@ import io.github.lonamiwebs.klooni.game.Scorer;
public class GameScreen implements Screen, InputProcessor {
private Klooni game;
private Scorer scorer;
private final Scorer scorer;
private Board board;
private PieceHolder holder;
@ -28,26 +26,23 @@ public class GameScreen implements Screen, InputProcessor {
private SpriteBatch batch;
private final Color clearColor;
private int score;
private final PauseMenuStage pauseMenu;
public GameScreen(Klooni aGame) {
game = aGame;
score = 0;
GameScreen(final Klooni game) {
clearColor = new Color(0.9f, 0.9f, 0.7f, 1f);
batch = new SpriteBatch();
pauseMenu = new PauseMenuStage(game);
layout = new GameLayout();
scorer = new Scorer(layout, 10);
board = new Board(layout, 10);
holder = new PieceHolder(layout, 3);
pauseMenu = new PauseMenuStage(game, scorer);
}
boolean isGameOver() {
private boolean isGameOver() {
for (Piece piece : holder.getAvailablePieces()) {
if (board.canPutPiece(piece)) {
return false;

View file

@ -16,6 +16,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.game.Scorer;
public class PauseMenuStage extends Stage {
@ -23,7 +24,10 @@ public class PauseMenuStage extends Stage {
private boolean shown;
private boolean hiding;
public PauseMenuStage(final Klooni game) {
private Scorer scorer;
public PauseMenuStage(final Klooni game, final Scorer aScorer) {
scorer = aScorer;
Table table = new Table();
table.setFillParent(true);
@ -90,7 +94,9 @@ public class PauseMenuStage extends Stage {
});
}
public void show() {
void show() {
scorer.saveScore();
lastInputProcessor = Gdx.input.getInputProcessor();
Gdx.input.setInputProcessor(this);
shown = true;