From fc912863ad996144dc5897522c2b2b4553837222 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Wed, 22 Jan 2020 20:18:14 +0400 Subject: [PATCH] Register high score if it fits anywhere in the top 10. When adding it, render in appropriate position. --- .../me/pacman/state/HighScoreEntryState.java | 29 ++++++++++++------- core/src/com/me/pacman/state/PlayState.java | 29 +++++++++++++++---- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/core/src/com/me/pacman/state/HighScoreEntryState.java b/core/src/com/me/pacman/state/HighScoreEntryState.java index 9995c3b..3694c2e 100644 --- a/core/src/com/me/pacman/state/HighScoreEntryState.java +++ b/core/src/com/me/pacman/state/HighScoreEntryState.java @@ -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); diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index 0fe7fc0..211b98d 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -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;