Skip to content

Commit

Permalink
@co012/rpg 78 game end action (#40)
Browse files Browse the repository at this point in the history
* feat: done, but ...

* chore: made it retain size
  • Loading branch information
co012 authored May 21, 2022
1 parent 9a46f38 commit 6480673
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/main/java/io/rpg/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, LocationModel> tagToLocationModelMap;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/rpg/model/actions/GameEndAction.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
30 changes: 30 additions & 0 deletions src/main/java/io/rpg/view/GameEndView.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
32 changes: 32 additions & 0 deletions src/main/java/io/rpg/viewmodel/GameEndViewModel.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
25 changes: 25 additions & 0 deletions src/main/resources/io/rpg/viewmodel/game-end-view.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>


<VBox fx:controller="io.rpg.viewmodel.GameEndViewModel" alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" style="-fx-background-color: #1c1c1c;" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label text="GAME OVER" textFill="WHITE">
<font>
<Font name="Sitka Small" size="72.0" />
</font>
</Label>
<Label fx:id="descriptionLabel" alignment="CENTER" maxWidth="300.0" text="No description given." textAlignment="CENTER" textFill="#848484" wrapText="true">
<font>
<Font name="Sitka Small" size="18.0" />
</font>
</Label>
</children>
<padding>
<Insets bottom="25.0" left="25.0" right="25.0" top="25.0" />
</padding>
</VBox>

0 comments on commit 6480673

Please sign in to comment.