feat: scale speed based on level
This commit is contained in:
@@ -9,6 +9,7 @@ public class Settings {
|
|||||||
public static int musicVolume = 100;
|
public static int musicVolume = 100;
|
||||||
public static int soundVolume = 100;
|
public static int soundVolume = 100;
|
||||||
public static float guiScale = 3.f;
|
public static float guiScale = 3.f;
|
||||||
|
public static boolean shouldScaleSpeed = true;
|
||||||
public static Difficulty difficulty = Difficulty.NORMAL;
|
public static Difficulty difficulty = Difficulty.NORMAL;
|
||||||
|
|
||||||
public static void load() {
|
public static void load() {
|
||||||
|
|||||||
@@ -59,10 +59,23 @@ public class Tetris {
|
|||||||
|
|
||||||
public static void updateScore(int score) {
|
public static void updateScore(int score) {
|
||||||
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateScore(score);
|
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateScore(score);
|
||||||
|
updateLevel(score);
|
||||||
}
|
}
|
||||||
public static void updateTime() {
|
public static void updateTime() {
|
||||||
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateTime(startTime);
|
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 {
|
public static class GravityTimerTask extends TimerTask {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ public class Space {
|
|||||||
private TetrominoShape nextShape;
|
private TetrominoShape nextShape;
|
||||||
private Tetromino currentTetromino;
|
private Tetromino currentTetromino;
|
||||||
private int score;
|
private int score;
|
||||||
|
public int level;
|
||||||
|
|
||||||
public Space() {
|
public Space() {
|
||||||
gameMap = new Color[14][8];
|
gameMap = new Color[14][8];
|
||||||
@@ -79,7 +80,7 @@ public class Space {
|
|||||||
gameMap[line][i] = newBlobs[i];
|
gameMap[line][i] = newBlobs[i];
|
||||||
}
|
}
|
||||||
if (Arrays.stream(gameMap[line]).noneMatch(Objects::isNull)) { // Line completed
|
if (Arrays.stream(gameMap[line]).noneMatch(Objects::isNull)) { // Line completed
|
||||||
combo += 10;
|
combo += 40;
|
||||||
completedLines.add(line);
|
completedLines.add(line);
|
||||||
combo *= completedLines.size();
|
combo *= completedLines.size();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,12 @@ import static eu.midnightdust.yaytris.ui.TetrisUI.setFontScale;
|
|||||||
public class AbstractMenu extends JPanel {
|
public class AbstractMenu extends JPanel {
|
||||||
@Override
|
@Override
|
||||||
public Component add(Component comp) {
|
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);
|
if (comp instanceof JComponent) setFontScale((JComponent) comp);
|
||||||
return super.add(comp);
|
return super.add(comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getSpacing() {
|
||||||
|
return 23;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ public class ScoreMenu extends AbstractMenu {
|
|||||||
final PreviewCanvas previewCanvas;
|
final PreviewCanvas previewCanvas;
|
||||||
final JLabel gameOverLabel;
|
final JLabel gameOverLabel;
|
||||||
final JLabel currentScoreLabel;
|
final JLabel currentScoreLabel;
|
||||||
|
final JLabel currentLevelLabel;
|
||||||
final JLabel currentTimeLabel;
|
final JLabel currentTimeLabel;
|
||||||
|
|
||||||
ScoreMenu(int x, int y, int width, int height, TetrisUI ui) {
|
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.currentScoreLabel = new JLabel("Score: 0");
|
||||||
this.add(currentScoreLabel);
|
this.add(currentScoreLabel);
|
||||||
|
|
||||||
|
this.currentLevelLabel = new JLabel("Level: 0");
|
||||||
|
this.add(currentLevelLabel);
|
||||||
|
|
||||||
this.currentTimeLabel = new JLabel("Zeit: 00:00");
|
this.currentTimeLabel = new JLabel("Zeit: 00:00");
|
||||||
this.add(currentTimeLabel);
|
this.add(currentTimeLabel);
|
||||||
|
|
||||||
@@ -58,4 +62,14 @@ public class ScoreMenu extends AbstractMenu {
|
|||||||
public void gameOver() {
|
public void gameOver() {
|
||||||
this.gameOverLabel.setText("Game over :(");
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,14 @@ public class SettingsMenu extends JPanel {
|
|||||||
});
|
});
|
||||||
this.add(scaleSlider);
|
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:"));
|
this.add(new JLabel("Schwierigkeit:"));
|
||||||
JComboBox<Difficulty> difficultySelector = new JComboBox<>(Difficulty.values());
|
JComboBox<Difficulty> difficultySelector = new JComboBox<>(Difficulty.values());
|
||||||
difficultySelector.setSelectedItem(Settings.difficulty);
|
difficultySelector.setSelectedItem(Settings.difficulty);
|
||||||
|
|||||||
@@ -94,6 +94,7 @@ public class NightJson {
|
|||||||
case "long": return Long.parseLong(currentString);
|
case "long": return Long.parseLong(currentString);
|
||||||
case "float": return Float.parseFloat(currentString);
|
case "float": return Float.parseFloat(currentString);
|
||||||
case "double": return Double.parseDouble(currentString);
|
case "double": return Double.parseDouble(currentString);
|
||||||
|
case "boolean": return Boolean.parseBoolean(currentString);
|
||||||
}
|
}
|
||||||
if (field.getType().isEnum()) return Arrays.stream(field.getType().getEnumConstants())
|
if (field.getType().isEnum()) return Arrays.stream(field.getType().getEnumConstants())
|
||||||
.filter(enumConstant -> Objects.equals(enumConstant.toString(), currentString)).findFirst().orElseThrow();
|
.filter(enumConstant -> Objects.equals(enumConstant.toString(), currentString)).findFirst().orElseThrow();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"musicVolume": 100,
|
"musicVolume": 25,
|
||||||
"soundVolume": 100,
|
"soundVolume": 100,
|
||||||
"guiScale": 6.0,
|
"guiScale": 6.0,
|
||||||
|
"shouldScaleSpeed": false,
|
||||||
"difficulty": "Normal"
|
"difficulty": "Normal"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user