Register high score if it fits anywhere in the top 10.

When adding it, render in appropriate position.
This commit is contained in:
Matt Low 2020-01-22 20:18:14 +04:00
parent 5645b4bcf5
commit fc912863ad
2 changed files with 41 additions and 17 deletions

View File

@ -18,6 +18,7 @@ public class HighScoreEntryState extends State {
private Texture background;
private Score score;
private final int position;
private int currLetter;
private char[] name;
@ -26,8 +27,9 @@ public class HighScoreEntryState extends State {
private boolean buttonPressed;
public HighScoreEntryState(PacDude game, Score score) {
public HighScoreEntryState(PacDude game, Score score, int position) {
super(game);
this.position = position;
this.score = score;
this.currLetter = 0;
this.flash = false;
@ -51,17 +53,22 @@ public class HighScoreEntryState extends State {
game.fontRenderer.setColor(Color.CHARTREUSE);
game.fontRenderer.draw(game.batch, "score name", (8 * Constants.TILE_SIZE) + Constants.TILE_SIZE/2, 22 * Constants.TILE_SIZE);
game.fontRenderer.setColor(Color.BLUE);
game.fontRenderer.draw(game.batch, "" + score.score, (8 * Constants.TILE_SIZE) + Constants.TILE_SIZE/2, 18 * 9);
for (int i = 0; i < 3; i++) {
game.fontRenderer.draw(game.batch, String.valueOf((i == currLetter && flash)? '_' : name[i]), (16 + i) * Constants.TILE_SIZE + Constants.TILE_SIZE/2, 18 * 9);
}
game.fontRenderer.setColor(Color.WHITE);
Score[] scores = game.highScores.getHighScores(9);
for (int i = 0; i < scores.length; i++) {
Score score = scores[i];
game.fontRenderer.draw(game.batch, String.format("%-8d%s", score.score, score.scorer), (8 * Constants.TILE_SIZE) + Constants.TILE_SIZE/2, (17 - i) * 9);
for (int n = 0; n < scores.length + 1; n++) {
int y = (18 - n) * 9;
if (n == position) {
game.fontRenderer.setColor(Color.BLUE);
game.fontRenderer.draw(game.batch, "" + score.score, (8 * Constants.TILE_SIZE) + Constants.TILE_SIZE/2, y);
for (int i = 0; i < 3; i++) {
game.fontRenderer.draw(game.batch, String.valueOf((i == currLetter && flash)? '_' : name[i]), (16 + i) * Constants.TILE_SIZE + Constants.TILE_SIZE/2, y);
}
} else {
Score score = scores[n > position? n - 1 : n];
game.fontRenderer.setColor(Color.WHITE);
game.fontRenderer.draw(game.batch, String.format("%-8d%s", score.score, score.scorer), (8 * Constants.TILE_SIZE) + Constants.TILE_SIZE/2, y);
}
}
game.fontRenderer.setColor(buttonPressed ? Color.BLUE : Color.WHITE);

View File

@ -200,6 +200,28 @@ public class PlayState extends LevelState {
pacman.moving = true;
}
private GameState endGame() {
Score[] scores = game.highScores.getHighScores(10);
boolean addToScores = scores.length < 10;
int scorePosition = scores.length;
for (int i = 0; i < scores.length; i++) {
if (score > scores[i].score) {
addToScores = true;
scorePosition = i;
break;
}
}
if (addToScores) {
game.setNextState(new HighScoreEntryState(game, new Score("aaa", score), scorePosition));
return null;
}
return GameState.PRE_NEW_GAME;
}
@Override
public void setup() {
levelBackground = game.assets.getLevelBackground();
@ -227,12 +249,7 @@ public class PlayState extends LevelState {
newRound();
return GameState.NEW_ROUND_WAIT;
case GAME_OVER:
if (score > highScore) {
game.setNextState(new HighScoreEntryState(game, new Score("aaa", score)));
return null;
}
preNewGame();
return GameState.PRE_NEW_GAME;
return endGame();
case GHOST_CAUGHT_POINTS_WAIT:
updateBackgroundAudio();
return GameState.PLAYING;