feat: scale speed based on level

This commit is contained in:
Martin Prokoph
2025-08-01 17:59:48 +02:00
parent a228e9ab1a
commit 6cc7b95852
8 changed files with 46 additions and 3 deletions

View File

@@ -9,6 +9,7 @@ public class Settings {
public static int musicVolume = 100;
public static int soundVolume = 100;
public static float guiScale = 3.f;
public static boolean shouldScaleSpeed = true;
public static Difficulty difficulty = Difficulty.NORMAL;
public static void load() {

View File

@@ -59,10 +59,23 @@ public class Tetris {
public static void updateScore(int score) {
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateScore(score);
updateLevel(score);
}
public static void updateTime() {
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateTime(startTime);
}
public static void updateLevel(int score) {
int newLevel = Math.max(0, (int) (score / 1000f));
if (newLevel != space.level) {
if (gravityTask != null && Settings.shouldScaleSpeed) {
gravityTask.cancel();
gravityTask = new GravityTimerTask();
timer.scheduleAtFixedRate(gravityTask, 0, Math.max(10, Settings.difficulty.getTimerPeriod() - (Settings.difficulty.getTimerPeriod() / 8) * newLevel));
}
space.level = newLevel;
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateLevel(newLevel);
}
}
public static class GravityTimerTask extends TimerTask {
@Override

View File

@@ -13,6 +13,7 @@ public class Space {
private TetrominoShape nextShape;
private Tetromino currentTetromino;
private int score;
public int level;
public Space() {
gameMap = new Color[14][8];
@@ -79,7 +80,7 @@ public class Space {
gameMap[line][i] = newBlobs[i];
}
if (Arrays.stream(gameMap[line]).noneMatch(Objects::isNull)) { // Line completed
combo += 10;
combo += 40;
completedLines.add(line);
combo *= completedLines.size();
}

View File

@@ -9,8 +9,12 @@ import static eu.midnightdust.yaytris.ui.TetrisUI.setFontScale;
public class AbstractMenu extends JPanel {
@Override
public Component add(Component comp) {
comp.setBounds(scale(60), scale(20+23*this.getComponentCount()), scale(100), scale(20));
comp.setBounds(scale(60), scale(20+getSpacing()*this.getComponentCount()), scale(100), scale(20));
if (comp instanceof JComponent) setFontScale((JComponent) comp);
return super.add(comp);
}
public int getSpacing() {
return 23;
}
}

View File

@@ -15,6 +15,7 @@ public class ScoreMenu extends AbstractMenu {
final PreviewCanvas previewCanvas;
final JLabel gameOverLabel;
final JLabel currentScoreLabel;
final JLabel currentLevelLabel;
final JLabel currentTimeLabel;
ScoreMenu(int x, int y, int width, int height, TetrisUI ui) {
@@ -34,6 +35,9 @@ public class ScoreMenu extends AbstractMenu {
this.currentScoreLabel = new JLabel("Score: 0");
this.add(currentScoreLabel);
this.currentLevelLabel = new JLabel("Level: 0");
this.add(currentLevelLabel);
this.currentTimeLabel = new JLabel("Zeit: 00:00");
this.add(currentTimeLabel);
@@ -58,4 +62,14 @@ public class ScoreMenu extends AbstractMenu {
public void gameOver() {
this.gameOverLabel.setText("Game over :(");
}
public void updateLevel(int level) {
this.currentLevelLabel.setText(String.format("Level: %s", level));
this.repaint();
}
@Override
public int getSpacing() {
return 17;
}
}

View File

@@ -41,6 +41,14 @@ public class SettingsMenu extends JPanel {
});
this.add(scaleSlider);
this.add(new JLabel("Geschwindigkeitszunahme:"));
JCheckBox speedCheckbox = new JCheckBox("Aktiviert", Settings.shouldScaleSpeed);
speedCheckbox.addChangeListener(change -> {
Settings.shouldScaleSpeed = speedCheckbox.isSelected();
Settings.write();
});
this.add(speedCheckbox);
this.add(new JLabel("Schwierigkeit:"));
JComboBox<Difficulty> difficultySelector = new JComboBox<>(Difficulty.values());
difficultySelector.setSelectedItem(Settings.difficulty);

View File

@@ -94,6 +94,7 @@ public class NightJson {
case "long": return Long.parseLong(currentString);
case "float": return Float.parseFloat(currentString);
case "double": return Double.parseDouble(currentString);
case "boolean": return Boolean.parseBoolean(currentString);
}
if (field.getType().isEnum()) return Arrays.stream(field.getType().getEnumConstants())
.filter(enumConstant -> Objects.equals(enumConstant.toString(), currentString)).findFirst().orElseThrow();

View File

@@ -1,6 +1,7 @@
{
"musicVolume": 100,
"musicVolume": 25,
"soundVolume": 100,
"guiScale": 6.0,
"shouldScaleSpeed": false,
"difficulty": "Normal"
}