Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
testImplementation("org.junit.jupiter:junit-jupiter-params:${junitVersion}")
testImplementation("org.mockito:mockito-core:3.+")
testImplementation("org.mockito:mockito-core:4.5.1")
}

test {
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/io/rpg/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,56 @@

import io.rpg.controller.Controller;
import io.rpg.model.actions.Action;
import javafx.animation.AnimationTimer;
import javafx.stage.Stage;

public class Game {
private Controller controller;
private Action onStart;
private AnimationTimer timer;
private Runnable onEnd;

private Game() {

onEnd = () -> {};
}

public void setController(Controller controller) {
this.controller = controller;
controller.getGameEndController().setOnEnd(this::end);
}

public void start(Stage stage) {
stage.show();
controller.setMainStage(stage);
controller.consumeAction(onStart);
startAnimationTimer();
stage.show();
}

public void end() {
timer.stop();
onEnd.run();
}

public void setOnEnd(Runnable onEnd) {
this.onEnd = onEnd;
}

private void startAnimationTimer() {
timer = new AnimationTimer() {
long lastUpdate = -1;
@Override
public void handle(long now) {
if (lastUpdate != -1) {
float difference = (now - lastUpdate) / 1e6f;

getController().getPlayerController()
.getPlayer()
.update(difference);
}
lastUpdate = now;
}
};
timer.start();
}

public static class Builder {
Expand Down
18 changes: 6 additions & 12 deletions src/main/java/io/rpg/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import com.kkafara.rt.Result;
import io.rpg.config.ConfigLoader;
import io.rpg.controller.Controller;
import io.rpg.config.model.GameWorldConfig;
import io.rpg.config.model.LocationConfig;
import io.rpg.controller.Controller;
import io.rpg.controller.PlayerController;
import io.rpg.model.actions.LocationChangeAction;
import io.rpg.model.data.MapDirection;
import io.rpg.model.location.LocationModel;
import io.rpg.model.object.GameObject;
import io.rpg.model.object.Player;
Expand All @@ -19,29 +18,24 @@
import io.rpg.view.popups.QuestionPopup;
import io.rpg.view.popups.TextImagePopup;
import io.rpg.view.popups.TextPopup;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import javafx.geometry.Point2D;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class Initializer {
private Path pathToConfigDir;
private ConfigLoader configLoader;
private final Stage mainStage;

private final Logger logger;

public Initializer(@NotNull String pathToConfigDir, @NotNull Stage mainStage) {
public Initializer(@NotNull String pathToConfigDir) {
this.configLoader = new ConfigLoader(pathToConfigDir);
this.mainStage = mainStage;
this.logger = LogManager.getLogger(Initializer.class);
}

Expand Down
39 changes: 18 additions & 21 deletions src/main/java/io/rpg/Main.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
package io.rpg;

import com.kkafara.rt.Result;
import javafx.animation.AnimationTimer;
import io.rpg.wrapper.WrapperController;
import java.io.IOException;
import javafx.application.Application;
import javafx.stage.Stage;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;

import java.io.IOException;

/**
* Entry point of the app.
*/
public class Main extends Application {

private final Logger logger = LogManager.getLogger(Main.class);

@Override
public void start(Stage stage) throws IOException {
Configurator.setRootLevel(Level.DEBUG);
Logger logger = LogManager.getLogger(Main.class);
String path = getParameters().getNamed().get("config");
if (path == null) {
WrapperController wrapperController = WrapperController.load();
wrapperController.show(stage);
} else {
fastStart(path, stage);
}
}

Initializer worldInitializer = new Initializer("configurations/demo-config-1", stage);
private void fastStart(String path, Stage stage) {
Initializer worldInitializer = new Initializer(path);
Result<Game, Exception> initializationResult = worldInitializer.initialize();

if (initializationResult.isErr()) {
Expand All @@ -36,24 +49,8 @@ public void start(Stage stage) throws IOException {
return;
}

// TODO: 04.05.2022 Null check for game was already made but IDE still screams
Game game = initializationResult.getOk();
game.start(stage);

AnimationTimer animationTimer = new AnimationTimer() {
long lastUpdate = -1;

@Override
public void handle(long now) {
if (lastUpdate != -1) {
float difference = (now - lastUpdate) / 1e6f;

game.getController().getPlayerController().getPlayer().update(difference);
}
lastUpdate = now;
}
};
animationTimer.start();
}

public static void main(String[] args) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/io/rpg/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Obs
private Logger logger;
private final PopupController popupController = new PopupController();
private PlayerController playerController;
private GameEndController gameEndController;
private Stage mainStage;


Expand All @@ -42,6 +43,7 @@ public Controller() {

tagToLocationModelMap = new LinkedHashMap<>();
tagToLocationViewMap = new LinkedHashMap<>();
gameEndController = new GameEndController();
}

public Controller(LinkedHashMap<String, LocationModel> tagToLocationModelMap,
Expand Down Expand Up @@ -154,13 +156,8 @@ public void acceptQuizResult(boolean correct, int pointsCount) {
}

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);
gameEndController.showGameEnd(mainStage, action.description);

}

private void onAction(BattleAction action) {
Expand Down Expand Up @@ -266,6 +263,9 @@ public PlayerController getPlayerController() {
return playerController;
}

public GameEndController getGameEndController() {
return gameEndController;
}

public static class Builder {
private final Controller controller;
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/io/rpg/controller/GameEndController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.rpg.controller;

import io.rpg.view.GameEndView;
import javafx.event.EventType;
import javafx.scene.input.InputEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class GameEndController {
private final GameEndView view;
private Runnable onEnd;

public GameEndController() {
view = GameEndView.load();
onEnd = () -> {};
view.setOnClick(this::end);
}

public void showGameEnd(Stage stage, String description) {
view.setDescription(description);
double prevWidth = stage.getWidth();
double prevHeight = stage.getHeight();
stage.setScene(view);
stage.setWidth(prevWidth);
stage.setHeight(prevHeight);
}

public void setOnEnd(Runnable onEnd) {
this.onEnd = onEnd;
}

private void end() {
onEnd.run();
}
}
4 changes: 4 additions & 0 deletions src/main/java/io/rpg/view/GameEndView.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public static GameEndView load() {
public void setDescription(String description) {
viewModel.setDescription(description);
}

public void setOnClick(Runnable runnable) {
viewModel.getParent().setOnMouseClicked((e) -> runnable.run());
}
}
34 changes: 34 additions & 0 deletions src/main/java/io/rpg/wrapper/ConfigChooser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.rpg.wrapper;

import java.io.File;
import java.util.Optional;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class ConfigChooser {
private final FileChooser chooser;

public ConfigChooser() {
chooser = new FileChooser();
chooser.setTitle("Select root.json");
chooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("config", "root.json"),
new FileChooser.ExtensionFilter("json", "*.json")
);
}

/**
* Open file selection window.

* @param stage - main stage.
* @return Optional of selected file folder path.
*/
public Optional<String> open(Stage stage) {
File file = chooser.showOpenDialog(stage);
if (file == null) {
return Optional.empty();
}

return Optional.of(file.getParentFile().getAbsolutePath());
}
}
Loading