| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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 {
- Output.clear();
- for (Action action : actions) {
- if (action.getId().equals(id)) {
- action.execute(args);
- sendEvent();
- return;
- }
- }
- throw new InvalidActionId("Invalid action ID");
- }
- }
|