Bladeren bron

Implemented action processor

Ondřej Schlaichert 8 maanden geleden
bovenliggende
commit
642b9ec570

+ 1 - 1
src/main/java/eu/oschl/cli/commands/UseItem.java

@@ -33,7 +33,7 @@ public class UseItem implements Command {
     @Override
     public void execute(String[] args) {
         if (args.length == 0) {
-            Output.print("What item?", ConsoleColor.RED);
+            Console.print("What item?", ConsoleColor.RED);
             return;
         }
 

+ 51 - 0
src/main/java/eu/oschl/gui/ActionProcessor.java

@@ -1,4 +1,55 @@
 package eu.oschl.gui;
 
+import eu.oschl.gui.actions.Action;
+import eu.oschl.gui.actions.Explore;
+import eu.oschl.gui.exceptions.InvalidActionId;
+import eu.oschl.textadventure.Game;
+
+import java.util.ArrayList;
+import java.util.List;
+
 public class ActionProcessor extends Observable {
+    private final ArrayList<Action> actions;
+
+    public ActionProcessor(ArrayList<Action> actions) {
+        this.actions = actions;
+    }
+
+    public ArrayList<Action> getActions() {
+        return actions;
+    }
+
+    /**
+     * Factory method to create a ActionProcessor with a predefined set of Actions.
+     *
+     * @param game the game instance to be used by the actions
+     * @return a new ActionProcessor instance with initialized actions
+     */
+    public static ActionProcessor create(Game game) {
+        ArrayList<Action> actions = new ArrayList<>(List.of(
+            new Explore(game)
+        ));
+
+        return new ActionProcessor(actions);
+    }
+
+    /**
+     * Executes the action of the provided ID and arguments.
+     *
+     * @param id the action ID
+     * @throws InvalidActionId if the action ID is not found or is invalid
+     */
+    public void executeAction(String id, String[] args) throws InvalidActionId {
+        for (Action action : actions) {
+            if (action.getId().equals(id)) {
+                action.execute(args);
+
+                sendEvent();
+
+                return;
+            }
+        }
+
+        throw new InvalidActionId("Invalid action ID");
+    }
 }

+ 5 - 1
src/main/java/eu/oschl/gui/Session.java

@@ -10,17 +10,21 @@ import java.io.IOException;
 
 public class Session extends Application {
     private static Game game;
+    private static ActionProcessor actionProcessor;
 
     public static void launchGui(Game game, String[] args)
     {
         Session.game = game;
+
+        Session.actionProcessor = ActionProcessor.create(game);
+
         Application.launch(Session.class, args);
     }
 
     @Override
     public void start(Stage stage) throws IOException {
         FXMLLoader fxmlLoader = new FXMLLoader(Session.class.getResource("game.fxml"));
-        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
+        Scene scene = new Scene(fxmlLoader.load(), 1280, 720);
         stage.setTitle("Schmorn");
         stage.setScene(scene);
         stage.show();

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

@@ -9,10 +9,17 @@ public interface Action {
     /**
      * Returns ID of the action.
      *
-     * @return a string ID/name
+     * @return a string ID
      */
     String getId();
 
+    /**
+     * Returns name of the action.
+     *
+     * @return a string name
+     */
+    String getName();
+
     /**
      * Returns a description of what the action does.
      *

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

@@ -17,6 +17,11 @@ public class Explore implements Action {
         return "explore";
     }
 
+    @Override
+    public String getName() {
+        return "Explore";
+    }
+
     @Override
     public String getDescription() {
         return "find out information about the current area";

+ 24 - 0
src/main/java/eu/oschl/gui/exceptions/InvalidActionId.java

@@ -0,0 +1,24 @@
+package eu.oschl.gui.exceptions;
+
+/**
+ * This exception is thrown when the action ID is invalid.
+ *
+ * @author Ondřej Schlaichert
+ */
+public class InvalidActionId extends Exception {
+    public InvalidActionId() {
+        super();
+    }
+
+    public InvalidActionId(String message) {
+        super(message);
+    }
+
+    public InvalidActionId(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public InvalidActionId(Throwable cause) {
+        super(cause);
+    }
+}