95 lines
3.0 KiB
Java
95 lines
3.0 KiB
Java
package Main;
|
|
|
|
import GUI.HitoriDialogManager;
|
|
import domain.HitoriBoardLoader;
|
|
import domain.HitoriGameMain;
|
|
import javafx.application.Application;
|
|
import javafx.scene.Scene;
|
|
import javafx.scene.layout.BorderPane;
|
|
import javafx.stage.Stage;
|
|
import GUI.GameUIController;
|
|
|
|
public class MainMethod extends Application {
|
|
private HitoriBoardLoader boardLoader;
|
|
private GameUIController controller;
|
|
private HitoriDialogManager dialogManager;
|
|
private String currentBoardName;
|
|
|
|
public static void main(String[] args) {
|
|
launch(args);
|
|
}
|
|
|
|
@Override
|
|
public void start(Stage primaryStage) {
|
|
boardLoader = new HitoriBoardLoader();
|
|
dialogManager = new HitoriDialogManager(primaryStage);
|
|
|
|
checkSavedGame(primaryStage);
|
|
}
|
|
|
|
private void checkSavedGame(Stage primaryStage) {
|
|
HitoriGameMain savedGame = HitoriGameMain.loadGameState();
|
|
if (savedGame != null) {
|
|
if (dialogManager.confirmLoadSavedGame()) {
|
|
initializeGame(savedGame.getBoard(), primaryStage);
|
|
} else {
|
|
showBoardSelectionDialog(primaryStage);
|
|
}
|
|
} else {
|
|
showBoardSelectionDialog(primaryStage);
|
|
}
|
|
}
|
|
|
|
private void showBoardSelectionDialog(Stage primaryStage) {
|
|
dialogManager.showBoardSelectionDialog(boardLoader).ifPresentOrElse(
|
|
boardName -> {
|
|
currentBoardName = boardName;
|
|
int[][] selectedBoard = boardLoader.getBoard(boardName);
|
|
if (selectedBoard != null) {
|
|
initializeGame(selectedBoard, primaryStage);
|
|
}
|
|
},
|
|
() -> System.exit(0)
|
|
);
|
|
}
|
|
|
|
private void initializeGame(int[][] board, Stage primaryStage) {
|
|
controller = new GameUIController(board, dialogManager);
|
|
createGameUI(primaryStage);
|
|
}
|
|
|
|
private void createGameUI(Stage primaryStage) {
|
|
primaryStage.setTitle("Hitori Game - " + currentBoardName);
|
|
|
|
BorderPane mainLayout = new BorderPane();
|
|
mainLayout.setCenter(controller.getBoardPanel());
|
|
mainLayout.setTop(controller.getControlPanel());
|
|
mainLayout.setRight(controller.getScorePanel());
|
|
|
|
Scene scene = new Scene(mainLayout, 800, 600);
|
|
primaryStage.setScene(scene);
|
|
|
|
// Window state listeners
|
|
primaryStage.iconifiedProperty().addListener((observable, oldValue, newValue) -> {
|
|
if (newValue && !controller.isPaused()) {
|
|
controller.togglePause();
|
|
}
|
|
});
|
|
|
|
primaryStage.focusedProperty().addListener((observable, oldValue, newValue) -> {
|
|
if (!newValue && !controller.isPaused()) {
|
|
controller.togglePause();
|
|
}
|
|
});
|
|
|
|
primaryStage.setOnCloseRequest(event -> controller.cleanup());
|
|
primaryStage.show();
|
|
}
|
|
|
|
@Override
|
|
public void stop() {
|
|
if (controller != null) {
|
|
controller.cleanup();
|
|
}
|
|
}
|
|
} |