diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index df5dd70f..e06802a3 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -2,17 +2,22 @@ import io.rpg.model.actions.Action; import io.rpg.model.actions.ActionConsumer; +import io.rpg.model.actions.GameEndAction; import io.rpg.model.actions.LocationChangeAction; import io.rpg.model.data.KeyboardEvent; import io.rpg.model.data.MouseClickedEvent; import io.rpg.model.data.Position; import io.rpg.model.location.LocationModel; -import io.rpg.model.object.*; +import io.rpg.model.object.GameObject; +import io.rpg.model.object.Question; import io.rpg.util.Result; +import io.rpg.view.GameEndView; import io.rpg.view.GameObjectView; import io.rpg.view.LocationView; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.LinkedHashMap; +import java.util.List; import javafx.geometry.Point2D; import javafx.scene.Scene; import javafx.scene.input.KeyEvent; @@ -22,9 +27,6 @@ import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; -import java.util.LinkedHashMap; -import java.util.List; - public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Observer, ActionConsumer { private Scene currentView; private LinkedHashMap tagToLocationModelMap; @@ -104,6 +106,16 @@ private void onAction(LocationChangeAction action) { mainStage.setScene(nextView); } + private void onAction(GameEndAction action) { + GameEndView view = GameEndView.load(); + view.setDescription(action.description); + double prevWidth = mainStage.getWidth(); + double prevHeight = mainStage.getHeight(); + mainStage.setScene(view); + mainStage.setWidth(prevWidth); + mainStage.setHeight(prevHeight); + } + public Scene getView() { return currentView; } @@ -148,7 +160,8 @@ public void onKeyboardEvent(KeyboardEvent event) { case F -> popupController.openPointsPopup(5, getWindowCenterX(), getWindowCenterY()); case G -> popupController.openTextPopup("Hello!", getWindowCenterX(), getWindowCenterY()); case Q -> popupController.openQuestionPopup(new Question("How many bits are there in one byte?", new String[]{"1/8", "1024", "8", "256"}, 'C'), getWindowCenterX(), getWindowCenterY()); - case L -> consumeAction((Action) new LocationChangeAction("location-2", new Position(1, 2))); + case L -> consumeAction(new LocationChangeAction("location-2", new Position(1, 2))); + case U -> consumeAction(new GameEndAction("You have pressed the forbidden button")); } } // } else if (payload.getEventType() == KeyEvent.KEY_RELEASED) { diff --git a/src/main/java/io/rpg/model/actions/GameEndAction.java b/src/main/java/io/rpg/model/actions/GameEndAction.java new file mode 100644 index 00000000..b8a6fb26 --- /dev/null +++ b/src/main/java/io/rpg/model/actions/GameEndAction.java @@ -0,0 +1,11 @@ +package io.rpg.model.actions; + +public class GameEndAction implements Action { + public final String description; + + public GameEndAction(String description) { + this.description = description; + } + + +} diff --git a/src/main/java/io/rpg/view/GameEndView.java b/src/main/java/io/rpg/view/GameEndView.java new file mode 100644 index 00000000..ed68725d --- /dev/null +++ b/src/main/java/io/rpg/view/GameEndView.java @@ -0,0 +1,30 @@ +package io.rpg.view; + +import io.rpg.viewmodel.GameEndViewModel; +import java.io.IOException; +import javafx.scene.Parent; +import javafx.scene.Scene; + +public class GameEndView extends Scene { + private GameEndViewModel viewModel; + + private GameEndView(Parent parent) { + super(parent); + } + + public static GameEndView load() { + GameEndViewModel viewModel; + try { + viewModel = GameEndViewModel.load(); + } catch (IOException e) { + throw new RuntimeException(e); + } + GameEndView view = new GameEndView(viewModel.getParent()); + view.viewModel = viewModel; + return view; + } + + public void setDescription(String description) { + viewModel.setDescription(description); + } +} diff --git a/src/main/java/io/rpg/viewmodel/GameEndViewModel.java b/src/main/java/io/rpg/viewmodel/GameEndViewModel.java new file mode 100644 index 00000000..40ba380f --- /dev/null +++ b/src/main/java/io/rpg/viewmodel/GameEndViewModel.java @@ -0,0 +1,32 @@ +package io.rpg.viewmodel; + +import java.io.IOException; +import java.net.URL; +import java.util.Objects; +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.control.Label; + +public class GameEndViewModel { + public static final String GAME_END_VIEW_FXML = "game-end-view.fxml"; + private Parent parent; + @FXML private Label descriptionLabel; + + public static GameEndViewModel load() throws IOException { + URL url = Objects.requireNonNull(GameEndViewModel.class.getResource(GAME_END_VIEW_FXML)); + FXMLLoader loader = new FXMLLoader(url); + Parent root = loader.load(); + GameEndViewModel viewModel = loader.getController(); + viewModel.parent = root; + return viewModel; + } + + public Parent getParent() { + return parent; + } + + public void setDescription(String description) { + descriptionLabel.setText(description); + } +} diff --git a/src/main/resources/io/rpg/viewmodel/game-end-view.fxml b/src/main/resources/io/rpg/viewmodel/game-end-view.fxml new file mode 100644 index 00000000..a16a8125 --- /dev/null +++ b/src/main/resources/io/rpg/viewmodel/game-end-view.fxml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + +