Forráskód Böngészése

Added ability to restart game

Ondřej Schlaichert 8 hónapja
szülő
commit
cdf7863726

+ 17 - 9
src/main/java/eu/oschl/gui/ActionProcessor.java

@@ -8,7 +8,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 public class ActionProcessor extends Observable {
-    private final ArrayList<Action> actions;
+    private ArrayList<Action> actions;
 
     public ActionProcessor(ArrayList<Action> actions) {
         this.actions = actions;
@@ -25,18 +25,26 @@ public class ActionProcessor extends Observable {
      * @return a new ActionProcessor instance with initialized actions
      */
     public static ActionProcessor create(Game game) {
-        ArrayList<Action> actions = new ArrayList<>(List.of(
-            new Explore(game),
-            new Slay(game),
-            new PressButton(game),
-            new TakeItem(game),
-            new Enter(game),
-            new UseItem(game)
-        ));
+        ArrayList<Action> actions = getActions(game);
 
         return new ActionProcessor(actions);
     }
 
+    private static ArrayList<Action> getActions(Game game) {
+        return new ArrayList<>(List.of(
+                new Explore(game),
+                new Slay(game),
+                new PressButton(game),
+                new TakeItem(game),
+                new Enter(game),
+                new UseItem(game)
+        ));
+    }
+
+    public void changeGame(Game game) {
+        this.actions = getActions(game);
+    }
+
     /**
      * Executes the action of the provided ID and arguments.
      *

+ 63 - 2
src/main/java/eu/oschl/gui/Session.java

@@ -1,13 +1,22 @@
 package eu.oschl.gui;
 
+import eu.oschl.schmorn.Setup;
 import eu.oschl.textadventure.Game;
 import javafx.application.Application;
+import javafx.application.Platform;
+import javafx.event.Event;
 import javafx.fxml.FXMLLoader;
 import javafx.scene.Scene;
+import javafx.scene.control.Alert;
+import javafx.scene.control.ButtonType;
+import javafx.scene.input.KeyCode;
+import javafx.scene.input.KeyCodeCombination;
+import javafx.scene.input.KeyCombination;
 import javafx.stage.Stage;
 
 import java.io.IOException;
 import java.util.Objects;
+import java.util.Optional;
 
 public class Session extends Application {
     private static Game game;
@@ -21,14 +30,27 @@ public class Session extends Application {
         return actionProcessor;
     }
 
-    public static void launchGui(Game game, String[] args)
+    public static void launchGui(String[] args)
     {
-        Session.game = game;
+        createNewGame();
+
         Session.actionProcessor = ActionProcessor.create(game);
 
         Application.launch(Session.class, args);
     }
 
+    private static void createNewGame() {
+        Session.game = Setup.createGame();
+
+        if (Session.actionProcessor != null) {
+            Session.actionProcessor.changeGame(Session.game);
+        } else {
+            Session.actionProcessor = ActionProcessor.create(Session.game);
+        }
+
+        Session.actionProcessor.sendEvent();
+    }
+
     @Override
     public void start(Stage stage) throws IOException {
         FXMLLoader fxmlLoader = new FXMLLoader(Session.class.getResource("game.fxml"));
@@ -39,6 +61,45 @@ public class Session extends Application {
 
         stage.setTitle("Schmorn");
         stage.setScene(scene);
+
+        stage.setOnCloseRequest(event -> handleCloseRequest(stage, event));
+
+        KeyCombination restartKeyCombination = new KeyCodeCombination(KeyCode.R, KeyCombination.CONTROL_DOWN);
+        scene.getAccelerators().put(restartKeyCombination, () -> handleRestartRequest(stage));
+
         stage.show();
     }
+
+    private void handleCloseRequest(Stage stage, Event event) {
+        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
+        alert.initOwner(stage);
+        alert.setTitle("Quit Game");
+        alert.setHeaderText("You are about to quit playing.");
+        alert.setContentText("Are you sure you want to quit?");
+
+        Optional<ButtonType> result = alert.showAndWait();
+
+        if (result.isPresent() && result.get() == ButtonType.OK) {
+            Platform.exit();
+        } else {
+            event.consume();
+        }
+    }
+
+    private void handleRestartRequest(Stage stage) {
+        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
+        alert.initOwner(stage);
+        alert.setTitle("Restart Game");
+        alert.setHeaderText("You are about to restart the game.");
+        alert.setContentText("Are you sure you want to restart?");
+
+        Optional<ButtonType> result = alert.showAndWait();
+
+        if (result.isPresent() && result.get() == ButtonType.OK) {
+            createNewGame();
+
+            Output.clear();
+            Output.print(game.getPrologue());
+        }
+    }
 }

+ 30 - 0
src/main/java/eu/oschl/schmorn/Launcher.java

@@ -0,0 +1,30 @@
+package eu.oschl.schmorn;
+
+import eu.oschl.textadventure.Game;
+
+public class Launcher {
+    private static String[] args;
+
+    public static void setArgs(String[] args) {
+        Launcher.args = args;
+    }
+
+    public static void startGame() {
+        var useCli = Launcher.args.length > 0 && args[0].equalsIgnoreCase("--cli");
+
+        if (useCli) {
+            startCli(Setup.createGame());
+        } else {
+            startGui(args);
+        }
+    }
+
+    private static void startCli(Game game) {
+        var session = new eu.oschl.cli.Session(game);
+        session.start();
+    }
+
+    private static void startGui(String[] args) {
+        eu.oschl.gui.Session.launchGui(args);
+    }
+}

+ 2 - 20
src/main/java/eu/oschl/schmorn/Main.java

@@ -1,7 +1,5 @@
 package eu.oschl.schmorn;
 
-import eu.oschl.textadventure.Game;
-
 /**
  * Serves as an entry point, handles starting either CLI or GUI version of the game.
  *
@@ -9,23 +7,7 @@ import eu.oschl.textadventure.Game;
  */
 class Main {
     static void main(String[] args) {
-        var useCli = args.length > 0 && args[0].equalsIgnoreCase("--cli");
-
-        var game = Setup.createGame();
-
-        if (useCli) {
-            startCli(game);
-        } else {
-            startGui(game, args);
-        }
-    }
-
-    private static void startCli(Game game) {
-        var session = new eu.oschl.cli.Session(game);
-        session.start();
-    }
-
-    private static void startGui(Game game, String[] args) {
-        eu.oschl.gui.Session.launchGui(game, args);
+        Launcher.setArgs(args);
+        Launcher.startGame();
     }
 }

+ 1 - 1
src/main/java/eu/oschl/schmorn/Setup.java

@@ -14,7 +14,7 @@ import eu.oschl.textadventure.objects.Weapon;
  *
  * @author Ondřej Schlaichert
  */
-class Setup {
+public class Setup {
     public static Game createGame() {
         // Base game setup
         var prologue = """

+ 2 - 0
src/main/java/module-info.java

@@ -4,4 +4,6 @@ module eu.oschl.gui {
 
     opens eu.oschl.gui to javafx.fxml;
     exports eu.oschl.gui;
+    exports eu.oschl.schmorn;
+    opens eu.oschl.schmorn to javafx.fxml;
 }

+ 21 - 12
src/main/resources/eu/oschl/gui/css/style.css

@@ -108,32 +108,28 @@
 }
 
 .inventory-item {
-	/* Base style: No background, no border, transparent */
 	-fx-background-color: transparent;
 	-fx-border-color: transparent;
-	-fx-border-width: 1px; /* Keep border-width for smooth hover transition */
+	-fx-border-width: 1px;
 	-fx-background-radius: 0;
 	-fx-border-radius: 0;
-	-fx-padding: 6px 12px; /* Inherit or define padding */
+	-fx-padding: 6px 12px;
 }
 
 .inventory-item:hover {
-	/* Hover state: Opaque border (select box) */
-	-fx-background-color: transparent; /* No background change */
-	-fx-border-color: #e0e0e0; /* Opaque white/light-gray border */
-	-fx-text-fill: #ffffff; /* Slightly brighter text */
+	-fx-background-color: transparent;
+	-fx-border-color: #e0e0e0;
+	-fx-text-fill: #ffffff;
 }
 
 .inventory-item:focused {
-	/* Focused state: Retain a visible focus indicator */
-	-fx-border-color: #aaaaaa; /* A clear, visible focus color */
+	-fx-border-color: #aaaaaa;
 	-fx-border-width: 1px;
 	-fx-background-color: transparent;
 }
 
 .inventory-item:pressed {
-	/* Pressed state: Slight visual feedback */
-	-fx-background-color: rgba(224, 224, 224, 0.1); /* Very subtle dark overlay */
+	-fx-background-color: rgba(224, 224, 224, 0.1);
 	-fx-border-color: #e0e0e0;
 }
 
@@ -146,9 +142,22 @@
 	-fx-opacity: 1 !important;
 }
 
-.button:hover { -fx-cursor: hand; }
+.button:hover {
+	-fx-cursor: hand;
+}
 
 .button:disabled,
 .button:disabled:hover {
 	-fx-cursor: default;
+}
+
+.dialog-pane {
+	-fx-background-color: #000000;
+	-fx-border-color: #555555;
+	-fx-border-width: 1px;
+}
+
+.dialog-pane .header-panel,
+.dialog-pane .button-bar {
+	-fx-background-color: transparent;
 }