clean: improve code structure
This commit is contained in:
22
src/main/java/eu/midnightdust/yaytris/Main.java
Normal file
22
src/main/java/eu/midnightdust/yaytris/Main.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package eu.midnightdust.yaytris;
|
||||
|
||||
import javax.swing.UIManager;
|
||||
|
||||
public class Main {
|
||||
|
||||
/**
|
||||
* The application's entry point.
|
||||
* Initializes the UI library to match the system style.
|
||||
* Also loads saved settings, translations, and highscores from JSON.
|
||||
*
|
||||
* @param args command line arguments – will be ignored
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
System.setProperty("java.awt.headless", "false");
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (Exception | Error e) { System.out.printf("%s: %s\n", "Error setting system look and feel", e); }
|
||||
Tetris.init();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package eu.midnightdust.yaytris;
|
||||
|
||||
import eu.midnightdust.yaytris.game.Space;
|
||||
import eu.midnightdust.yaytris.game.Tetromino;
|
||||
import eu.midnightdust.yaytris.ui.ScoreMenu;
|
||||
import eu.midnightdust.yaytris.ui.TetrisUI;
|
||||
import eu.midnightdust.yaytris.util.GravityTimerTask;
|
||||
import eu.midnightdust.yaytris.util.sound.SoundUtil;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.time.LocalTime;
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
@@ -20,24 +20,13 @@ public class Tetris {
|
||||
private static TimerTask gravityTask;
|
||||
private static LocalTime startTime;
|
||||
|
||||
/**
|
||||
* The application's entry point.
|
||||
* Initializes the UI library to match the system style.
|
||||
* Also loads saved settings, translations, and highscores from JSON.
|
||||
*
|
||||
* @param args command line arguments – will be ignored
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
System.setProperty("java.awt.headless", "false");
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (Exception | Error e) { System.out.printf("%s: %s\n", "Error setting system look and feel", e); }
|
||||
public static void init() {
|
||||
Settings.load();
|
||||
Translation.load(Settings.language.locale);
|
||||
HighScores.load();
|
||||
timer = new Timer("Tetris falling pieces");
|
||||
space = new Space();
|
||||
ui = new TetrisUI();
|
||||
Tetris.timer = new Timer("Tetris falling pieces");
|
||||
Tetris.space = new Space();
|
||||
Tetris.ui = new TetrisUI();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,7 +61,7 @@ public class Tetris {
|
||||
|
||||
/**
|
||||
* Starts a new game of Tetris :D
|
||||
* This involves starting our gravity task, playing music and spawning the first {@link eu.midnightdust.yaytris.game.Tetromino}
|
||||
* This involves starting our gravity task, playing music and spawning the first {@link Tetromino}
|
||||
*
|
||||
* @see Space#spawnTetromino()
|
||||
*/
|
||||
@@ -97,7 +86,8 @@ public class Tetris {
|
||||
timer.purge();
|
||||
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).gameOver();
|
||||
ui.transferFocus();
|
||||
if (HighScores.diffToMap(Settings.difficulty).values().stream().noneMatch(hs -> hs > space.getScore())) ui.showHighscoreDialog(space.getScore());
|
||||
if (HighScores.diffToMap(Settings.difficulty).values().stream().noneMatch(hs -> hs > space.getScore()))
|
||||
ui.showHighscoreDialog(space.getScore());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,6 +100,7 @@ public class Tetris {
|
||||
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateScore(score);
|
||||
updateLevel(score);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the elapsed time
|
||||
*
|
||||
@@ -118,6 +109,7 @@ public class Tetris {
|
||||
public static void updateTime() {
|
||||
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateTime(startTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the displayed level
|
||||
*
|
||||
@@ -136,5 +128,4 @@ public class Tetris {
|
||||
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateLevel(newLevel);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public class TetrisUI extends JFrame implements KeyListener {
|
||||
private void rescale() {
|
||||
this.setSize((int) (400 * guiScale), (int) (320 * guiScale));
|
||||
titleLabel.setBounds(scale(225), scale(7), scale(110), scale(30));
|
||||
titleLabel.setIcon(new ImageIcon(new ImageIcon(titleImage).getImage().getScaledInstance(scale(110), scale(30), Image.SCALE_DEFAULT)));
|
||||
titleLabel.setIcon(new ImageIcon(titleImage.getScaledInstance(scale(110), scale(30), Image.SCALE_DEFAULT)));
|
||||
gamePanel.setBounds(scale(10), scale(10), scale(150), scale(282));
|
||||
this.setLocationRelativeTo(null);
|
||||
}
|
||||
@@ -165,7 +165,7 @@ public class TetrisUI extends JFrame implements KeyListener {
|
||||
*/
|
||||
public void showHighscoreDialog(int score) {
|
||||
String playerName = JOptionPane.showInputDialog(null, t("dialog.highscore.action"), t("dialog.highscore.title"), JOptionPane.PLAIN_MESSAGE);
|
||||
HighScores.addScore(playerName, score);
|
||||
if (playerName != null) HighScores.addScore(playerName, score);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user