package fr.monkhanny.dorfromantik.controller; import fr.monkhanny.dorfromantik.gui.GameModeSelectionPanel; import fr.monkhanny.dorfromantik.gui.MainMenu; import fr.monkhanny.dorfromantik.game.Board; import fr.monkhanny.dorfromantik.utils.Database; import fr.monkhanny.dorfromantik.Options; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import java.sql.SQLException; import java.awt.Dimension; import java.awt.Point; public class GameModeController implements ActionListener { private GameModeSelectionPanel gameModeSelectionPanel; private JFrame gameFrame; private MainMenu mainMenu; private Database database; private JFrame gameModeFrame; private static Board board; // Constructeur sans le panneau public GameModeController(JFrame gameFrame, MainMenu mainMenu, JFrame gameModeFrame) { this.gameFrame = gameFrame; this.mainMenu = mainMenu; this.gameModeFrame = gameModeFrame; // Connexion à la base de données try { this.database = new Database(); } catch (Exception e) { System.err.println("Erreur lors de la connexion à la base de données: " + e.getMessage()); } } // Méthode pour associer le panneau public void setGameModeSelectionPanel(GameModeSelectionPanel panel) { this.gameModeSelectionPanel = panel; } @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); switch (command) { case "Mode 1": startGame("Mode 1", getSeedFromDatabaseByName("Mode 1")); break; case "Mode 2": startGame("Mode 2", getSeedFromDatabaseByName("Mode 2")); break; case "Mode 3": startGame("Mode 3", getSeedFromDatabaseByName("Mode 3")); break; case "Mode 4": startGame("Mode 4", getSeedFromDatabaseByName("Mode 4")); break; case "Démarrer": long seed = gameModeSelectionPanel.getLongSeed(); startGame("Custom Mode", seed); addCustomSeedToDatabase("Custom Mode", seed); break; default: System.out.println("Commande inconnue: " + command); } } private long getSeedFromDatabaseByName(String modeName) { try { Options.SEED = this.database.getSeedByName(modeName); return Options.SEED; } catch (SQLException e) { e.printStackTrace(); return -1; // Retourner une valeur par défaut si une erreur survient } } private void addCustomSeedToDatabase(String name, long seed) { try { Options.SEED = seed; this.database.addCustomSeed(name, seed); } catch (SQLException e) { e.printStackTrace(); System.err.println("Erreur lors de l'ajout de la seed custom."); } } private void startGame(String mode, long seed) { // Supprimer la potentielle ancienne instance de board GameModeController.board = null; // Cacher la fenêtre avant de faire des modifications this.gameFrame.setVisible(false); if (Options.FULL_SCREEN) { gameFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); // Set frame to full screen } else { Dimension mainMenuSize = this.mainMenu.getSize(); Point mainMenuLocation = this.mainMenu.getLocation(); gameFrame.setSize(mainMenuSize); gameFrame.setLocation(mainMenuLocation); } GameModeController.board = new Board(this.gameFrame,seed); //this.gameModeFrame.setVisible(false); this.gameFrame.setVisible(true); this.gameFrame.add(board); } public static Board getGameModeBoard() { return board; } }