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

Added room traversal

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

+ 3 - 1
src/main/java/eu/oschl/gui/ActionProcessor.java

@@ -1,6 +1,7 @@
 package eu.oschl.gui;
 
 import eu.oschl.gui.actions.Action;
+import eu.oschl.gui.actions.Enter;
 import eu.oschl.gui.actions.Explore;
 import eu.oschl.gui.exceptions.InvalidActionId;
 import eu.oschl.textadventure.Game;
@@ -27,7 +28,8 @@ public class ActionProcessor extends Observable {
      */
     public static ActionProcessor create(Game game) {
         ArrayList<Action> actions = new ArrayList<>(List.of(
-            new Explore(game)
+            new Explore(game),
+            new Enter(game)
         ));
 
         return new ActionProcessor(actions);

+ 24 - 8
src/main/java/eu/oschl/gui/GameController.java

@@ -1,6 +1,7 @@
 package eu.oschl.gui;
 
 import eu.oschl.gui.actions.Action;
+import eu.oschl.gui.actions.Enter;
 import eu.oschl.gui.exceptions.InvalidActionId;
 import javafx.event.ActionEvent;
 import javafx.fxml.FXML;
@@ -20,6 +21,7 @@ public class GameController implements Observer {
     @FXML
     public void initialize() {
         Output.setOutputElement(output);
+        Session.getActionProcessor().register(this);
         update();
 
         Output.print(Session.getGame().getPrologue());
@@ -41,14 +43,18 @@ public class GameController implements Observer {
         var newButtons = new ArrayList<Button>();
 
         for (Action action : Session.getActionProcessor().getActions()) {
-            var actionButton = new Button(action.getName());
-
-            actionButton.setUserData(new ButtonData(action.getId()));
-            actionButton.setOnAction(this::exectuteAction);
-
-            actionButton.setMaxWidth(Double.MAX_VALUE);
-
-            newButtons.add(actionButton);
+            if (action instanceof Enter) {
+                var passages = Session.getGame().getCurrentRoom().getPassages();
+
+                for (var passage : passages) {
+                    var label = action.getName() + " " + passage.getName() + " (>> " + passage.getOtherRoom(Session.getGame().getCurrentRoom()).getName() + ")";
+                    var buttonData = new ButtonData(action.getId(), passage.getName());
+
+                    newButtons.add(createActionButton(label, buttonData));
+                }
+            } else {
+                newButtons.add(createActionButton(action.getName(), new ButtonData(action.getId())));
+            }
         }
 
         inputButtonContainer.getChildren().addAll(newButtons);
@@ -58,4 +64,14 @@ public class GameController implements Observer {
     public void update() {
         renderActionButtons();
     }
+
+    private Button createActionButton(String label, ButtonData buttonData) {
+        Button actionButton = new Button(label);
+
+        actionButton.setUserData(buttonData);
+        actionButton.setOnAction(this::exectuteAction);
+        actionButton.setMaxWidth(Double.MAX_VALUE);
+
+        return actionButton;
+    }
 }

+ 96 - 0
src/main/java/eu/oschl/gui/actions/Enter.java

@@ -0,0 +1,96 @@
+package eu.oschl.gui.actions;
+
+import eu.oschl.gui.Output;
+import eu.oschl.textadventure.Game;
+import eu.oschl.textadventure.exceptions.InvalidGameState;
+import eu.oschl.textadventure.map.Passage;
+import javafx.scene.paint.Color;
+
+public class Enter implements Action {
+    private final Game game;
+
+    public Enter(Game game) {
+        this.game = game;
+    }
+
+    @Override
+    public String getId() {
+        return "enter";
+    }
+
+    @Override
+    public String getName() {
+        return "Enter";
+    }
+
+    @Override
+    public String getDescription() {
+        return "walk through a passage";
+    }
+
+    @Override
+    public void execute(String[] args) {
+        String input = String.join(" ", args);
+
+        var passages = this.game.getCurrentRoom().getPassages();
+        Passage passage = null;
+
+        for (Passage p : passages) {
+            if (p.getName().equalsIgnoreCase(input)) {
+                passage = p;
+                break;
+            }
+        }
+
+        var printEnterText = !passage.getOtherRoom(game.getCurrentRoom()).wasEntered();
+
+        var result = passage.passThrough(false);
+
+        if (!result) {
+            if (game.getCurrentRoom().getEnemy().isPresent()) {
+                Output.print(
+                        game.getCurrentRoom().getEnemy().get().getName() + " blocks the way. It is only possible to go back.",
+                        Color.RED
+                );
+            } else if (passage.getBlockage().isPresent()) {
+                Output.print(passage.getBlockage().get().getDescription(), Color.RED);
+            } else {
+                Output.print("The passage is blocked.", Color.RED);
+            }
+            return;
+        }
+
+        printSuccessfulPassage(printEnterText);
+    }
+
+    /**
+     * Prints a message indicating that the player has successfully passed through a passage.
+     *
+     * @param printEnterText whether to print the enter text of the current room
+     */
+    private void printSuccessfulPassage(boolean printEnterText) {
+        if (game.getLastPassage().isEmpty()) {
+            throw new InvalidGameState("Last passages stack is empty even though a passage was passed");
+        }
+
+        Output.print("Passed through the ");
+        Output.print(game.getLastPassage().get().getName(), Color.YELLOW);
+        Output.print(" and entered ");
+        Output.print(game.getCurrentRoom().getName(), Color.BLUE);
+        Output.print(".");
+
+        if (game.getCurrentRoom().isBlockedByEnemy()) {
+            Output.printLine();
+            Output.print("...", Color.WHITE);
+            Output.printLine();
+            Output.print("There is somebody in here.", Color.WHITE);
+        }
+
+        if (printEnterText && game.getCurrentRoom().getEnterText().isPresent()) {
+            Output.printLine();
+            Output.printLine();
+            Output.print(game.getCurrentRoom().getEnterText().get());
+            Output.printLine();
+        }
+    }
+}