Selaa lähdekoodia

Implemented more actions

Ondřej Schlaichert 8 kuukautta sitten
vanhempi
sitoutus
77a1baf4b4

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

@@ -1,8 +1,6 @@
 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.actions.*;
 import eu.oschl.gui.exceptions.InvalidActionId;
 import eu.oschl.textadventure.Game;
 
@@ -29,6 +27,8 @@ public class ActionProcessor extends Observable {
     public static ActionProcessor create(Game game) {
         ArrayList<Action> actions = new ArrayList<>(List.of(
             new Explore(game),
+            new Slay(game),
+            new PressButton(game),
             new Enter(game)
         ));
 

+ 35 - 5
src/main/java/eu/oschl/gui/GameController.java

@@ -2,6 +2,8 @@ package eu.oschl.gui;
 
 import eu.oschl.gui.actions.Action;
 import eu.oschl.gui.actions.Enter;
+import eu.oschl.gui.actions.PressButton;
+import eu.oschl.gui.actions.Slay;
 import eu.oschl.gui.exceptions.InvalidActionId;
 import javafx.event.ActionEvent;
 import javafx.fxml.FXML;
@@ -37,6 +39,11 @@ public class GameController implements Observer {
         actionProcessor.executeAction(buttonData.actionId(), buttonData.arguments());
     }
 
+    @Override
+    public void update() {
+        renderActionButtons();
+    }
+
     private void renderActionButtons() {
         inputButtonContainer.getChildren().clear();
 
@@ -54,6 +61,34 @@ public class GameController implements Observer {
 
                     button.getStyleClass().add("enter-button");
 
+                    newButtons.add(button);
+                }
+            } else if (action instanceof PressButton) {
+                var objects = Session.getGame().getCurrentRoom().getObjects();
+
+                for (var object : objects) {
+                    if (object instanceof eu.oschl.textadventure.objects.Button) {
+                        var label = action.getName() + " " + object.getName();
+                        var buttonData = new ButtonData(action.getId(), object.getName());
+
+                        var button = createActionButton(label, buttonData);
+
+                        button.getStyleClass().add("press-button");
+
+                        newButtons.add(button);
+                    }
+                }
+            } else if (action instanceof Slay) {
+                var enemy = Session.getGame().getCurrentRoom().getEnemy();
+
+                if (enemy.isPresent() && enemy.get().isAlive()) {
+                    var label = action.getName() + " " + enemy.get().getName();
+                    var buttonData = new ButtonData(action.getId(), enemy.get().getName());
+
+                    var button = createActionButton(label, buttonData);
+
+                    button.getStyleClass().add("slay-button");
+
                     newButtons.add(button);
                 }
             } else {
@@ -64,11 +99,6 @@ public class GameController implements Observer {
         inputButtonContainer.getChildren().addAll(newButtons);
     }
 
-    @Override
-    public void update() {
-        renderActionButtons();
-    }
-
     private Button createActionButton(String label, ButtonData buttonData) {
         Button actionButton = new Button(label);
 

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

@@ -76,7 +76,7 @@ public class Enter implements Action {
         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(game.getCurrentRoom().getName(), Color.LIGHTBLUE);
         Output.print(".");
 
         if (game.getCurrentRoom().isBlockedByEnemy()) {

+ 1 - 1
src/main/java/eu/oschl/gui/actions/Explore.java

@@ -19,7 +19,7 @@ public class Explore implements Action {
 
     @Override
     public String getName() {
-        return "Explore";
+        return "Look around";
     }
 
     @Override

+ 56 - 0
src/main/java/eu/oschl/gui/actions/PressButton.java

@@ -0,0 +1,56 @@
+package eu.oschl.gui.actions;
+
+import eu.oschl.gui.Output;
+import eu.oschl.textadventure.Game;
+import eu.oschl.textadventure.objects.Button;
+import javafx.scene.paint.Color;
+
+public class PressButton implements Action {
+    private final Game game;
+
+    public PressButton(Game game) {
+        this.game = game;
+    }
+
+    @Override
+    public String getId() {
+        return "press";
+    }
+
+    @Override
+    public String getName() {
+        return "Press";
+    }
+
+    @Override
+    public String getDescription() {
+        return "press a button";
+    }
+
+    @Override
+    public void execute(String[] args) {
+        for (var object : this.game.getCurrentRoom().getObjects()) {
+            if (object.getName().equalsIgnoreCase(String.join(" ", args))) {
+                if (object instanceof Button button) {
+                    var result = button.press();
+
+                    if (result) {
+                        Output.print(button.getPressText(), Color.MAGENTA);
+                    } else {
+                        Output.print("Button ", Color.RED);
+                        Output.print(button.getName(), Color.MAGENTA);
+                        Output.print(" has already been pressed.", Color.RED);
+                    }
+
+                } else {
+                    Output.print("It's impossible to press ", Color.RED);
+                    Output.print(object.getName(), Color.MAGENTA);
+                    Output.print(". It's not a button.", Color.RED);
+                }
+                return;
+            }
+        }
+
+        Output.print("That item is not here.", Color.RED);
+    }
+}

+ 73 - 0
src/main/java/eu/oschl/gui/actions/Slay.java

@@ -0,0 +1,73 @@
+package eu.oschl.gui.actions;
+
+import eu.oschl.gui.Output;
+import eu.oschl.textadventure.Game;
+import javafx.scene.paint.Color;
+
+public class Slay implements Action {
+    private final Game game;
+
+    public Slay(Game game) {
+        this.game = game;
+    }
+
+    @Override
+    public String getId() {
+        return "slay";
+    }
+
+    @Override
+    public String getName() {
+        return "Slay";
+    }
+
+    @Override
+    public String getDescription() {
+        return "fight an enemy";
+    }
+
+    @Override
+    public void execute(String[] args) {
+        if (
+                this.game.getCurrentRoom().getEnemy().isEmpty() ||
+                !this.game.getCurrentRoom().getEnemy().get().getName().equalsIgnoreCase(String.join(" ", args))
+        ) {
+            Output.print("This enemy is not here.", Color.RED);
+            return;
+        }
+
+        var enemy = this.game.getCurrentRoom().getEnemy().get();
+
+        if (enemy.strength > 0 && game.getInventory().getWeapon().isEmpty()) {
+            Output.print(enemy.getName() + " is too strong. A weapon is required.", Color.RED);
+            return;
+        }
+
+        if (enemy.strength > 0 && enemy.strength > game.getInventory().getWeapon().get().getDamage()) {
+            Output.print(enemy.getName() + " is too strong. A stronger weapon is required.", Color.RED);
+            return;
+        }
+
+        var result = enemy.kill();
+
+        if (result) {
+            if (game.getInventory().getWeapon().isPresent()) {
+                Output.print(game.getInventory().getWeapon().get().getAttackText());
+            }
+
+            if (enemy.getKillText().isPresent()) {
+                Output.printLine();
+                Output.print(enemy.getKillText().get());
+                Output.printLine();
+            }
+
+            Output.printLine();
+            Output.print(enemy.getName(), Color.RED);
+            Output.print(" lies murdered on the floor.");
+        } else {
+            Output.print("It's impossible to kill ", Color.RED);
+            Output.print(enemy.getName(), Color.RED);
+            Output.print(".", Color.RED);
+        }
+    }
+}

+ 8 - 0
src/main/resources/eu/oschl/gui/css/style.css

@@ -46,6 +46,14 @@
 	-fx-background-color: #14262b !important;
 }
 
+.press-button {
+	-fx-background-color: #14182b !important;
+}
+
+.slay-button {
+	-fx-background-color: #2b1416 !important;
+}
+
 .button:hover {
 	-fx-background-color: #333333;
 	-fx-border-color: #777777;