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

View file

@ -17,9 +17,7 @@ import io.github.lonamiwebs.klooni.game.Scorer;
public class GameScreen implements Screen, InputProcessor { public class GameScreen implements Screen, InputProcessor {
private Klooni game; private final Scorer scorer;
private Scorer scorer;
private Board board; private Board board;
private PieceHolder holder; private PieceHolder holder;
@ -28,26 +26,23 @@ public class GameScreen implements Screen, InputProcessor {
private SpriteBatch batch; private SpriteBatch batch;
private final Color clearColor; private final Color clearColor;
private int score;
private final PauseMenuStage pauseMenu; private final PauseMenuStage pauseMenu;
public GameScreen(Klooni aGame) { GameScreen(final Klooni game) {
game = aGame;
score = 0;
clearColor = new Color(0.9f, 0.9f, 0.7f, 1f); clearColor = new Color(0.9f, 0.9f, 0.7f, 1f);
batch = new SpriteBatch(); batch = new SpriteBatch();
pauseMenu = new PauseMenuStage(game);
layout = new GameLayout(); layout = new GameLayout();
scorer = new Scorer(layout, 10); scorer = new Scorer(layout, 10);
board = new Board(layout, 10); board = new Board(layout, 10);
holder = new PieceHolder(layout, 3); holder = new PieceHolder(layout, 3);
pauseMenu = new PauseMenuStage(game, scorer);
} }
boolean isGameOver() { private boolean isGameOver() {
for (Piece piece : holder.getAvailablePieces()) { for (Piece piece : holder.getAvailablePieces()) {
if (board.canPutPiece(piece)) { if (board.canPutPiece(piece)) {
return false; 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 com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import io.github.lonamiwebs.klooni.Klooni; import io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.game.Scorer;
public class PauseMenuStage extends Stage { public class PauseMenuStage extends Stage {
@ -23,7 +24,10 @@ public class PauseMenuStage extends Stage {
private boolean shown; private boolean shown;
private boolean hiding; 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 table = new Table();
table.setFillParent(true); table.setFillParent(true);
@ -90,7 +94,9 @@ public class PauseMenuStage extends Stage {
}); });
} }
public void show() { void show() {
scorer.saveScore();
lastInputProcessor = Gdx.input.getInputProcessor(); lastInputProcessor = Gdx.input.getInputProcessor();
Gdx.input.setInputProcessor(this); Gdx.input.setInputProcessor(this);
shown = true; shown = true;