feat: save highscores
This commit is contained in:
24
src/main/java/eu/midnightdust/yaytris/HighScores.java
Normal file
24
src/main/java/eu/midnightdust/yaytris/HighScores.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package eu.midnightdust.yaytris;
|
||||
|
||||
import eu.midnightdust.yaytris.util.NightJson;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class HighScores {
|
||||
private static final NightJson json = new NightJson(HighScores.class, "tetris_scores.json5");
|
||||
public static Map<String, Integer> scores = new HashMap<>();
|
||||
|
||||
public static void addScore(int score) {
|
||||
scores.put(String.valueOf(System.currentTimeMillis()), score);
|
||||
write();
|
||||
}
|
||||
|
||||
public static void load() {
|
||||
json.readJson();
|
||||
}
|
||||
|
||||
public static void write() {
|
||||
json.writeJson();
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ public class Tetris {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (Exception | Error e) { System.out.printf("%s: %s\n", "Error setting system look and feel", e); }
|
||||
Settings.load();
|
||||
HighScores.load();
|
||||
timer = new Timer("Tetris falling pieces");
|
||||
space = new Space();
|
||||
ui = new TetrisUI();
|
||||
@@ -51,6 +52,7 @@ public class Tetris {
|
||||
|
||||
public static void stopGame() {
|
||||
SoundUtil.stopMusic("/music/theme.wav");
|
||||
HighScores.addScore(space.getScore());
|
||||
if (gravityTask != null) gravityTask.cancel();
|
||||
timer.purge();
|
||||
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).gameOver();
|
||||
|
||||
@@ -65,6 +65,10 @@ public class Space {
|
||||
return gameMap;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void onLinesChanged(Tetromino tetromino, int... lines) {
|
||||
int combo = 0;
|
||||
Set<Integer> completedLines = new TreeSet<>();
|
||||
|
||||
3
tetris_scores.json5
Normal file
3
tetris_scores.json5
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"scores": {"1754065988809": 105,"1754067853918": 113,"1754067067271": 49}
|
||||
}
|
||||
Reference in New Issue
Block a user