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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"tag": "location-2",
"objects": [
{ "tag": "object-1", "position": { "row": 1, "col": 8 }, "type": "collectible", "assetPath": "assets/diamond.png" },
{ "tag": "object-2", "position": { "row": 6, "col": 3 }, "type": "collectible", "assetPath": "assets/key.png" },
{ "tag": "object-3", "position": { "row": 1, "col": 1 }, "type": "dialog", "assetPath": "assets/zombie.png" },
{ "tag": "object-4", "position": { "row": 3, "col": 2 }, "type": "dialog", "assetPath": "assets/zombie.png" },
{ "tag": "object-5", "position": { "row": 8, "col": 8 }, "type": "dialog", "assetPath": "assets/chest.png" }
],
Comment on lines +4 to +9
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some objects don't fit on the map. map size is currently dependent on the asset dimensions, it should probably be specified in config instead, but that's a problem for another task ;)

"backgroundPath": "assets/map10x10.png"
}
3 changes: 2 additions & 1 deletion configurations/demo-config-1/root.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"tag": "GameByConfig1",
"locationTags": [
"location-1"
"location-1",
"location-2"
],
"rootLocation": "location-1",
"player": {
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/io/rpg/Game.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package io.rpg;

import io.rpg.controller.Controller;
import io.rpg.model.actions.Action;
import javafx.scene.Scene;
import org.jetbrains.annotations.NotNull;
import javafx.stage.Stage;

public class Game {
private Controller controller;
private Action onStart;

private Game() {

}

public void setLocationControllerForLocationTag(String tag) {
// tutaj przyda sie hashmapa: nazwa lokacji -> kontroller
}

public Scene getWorldView() {
return controller.getView();
}

public void setController(Controller controller) {
this.controller = controller;
}

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

public static class Builder {
private final Game game;

Expand All @@ -40,6 +40,11 @@ public Builder setController(Controller controller) {
game.setController(controller);
return this;
}

public Builder setOnStartAction(Action onStart) {
game.onStart = onStart;
return this;
}
}

public Controller getController() {
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/io/rpg/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import io.rpg.controller.Controller;
import io.rpg.config.model.GameWorldConfig;
import io.rpg.config.model.LocationConfig;
import io.rpg.controller.PlayerController;
import io.rpg.model.actions.LocationChangeAction;
import io.rpg.model.data.Position;
import io.rpg.model.location.LocationModel;
import io.rpg.model.object.GameObject;
import io.rpg.config.model.GameObjectConfig;
Expand Down Expand Up @@ -54,6 +57,7 @@ public Result<Game, Exception> initialize() {

GameWorldConfig gameWorldConfig = gameWorldConfigLoadResult.getOkValue();


Controller.Builder controllerBuilder = new Controller.Builder();

assert gameWorldConfig.getLocationConfigs() != null;
Expand All @@ -74,15 +78,7 @@ public Result<Game, Exception> initialize() {

assert view != null;

gameObjectViews.forEach(view_ -> {
view.getViewModel().addChild(view_);
});

if (locationConfig.getTag().equals(gameWorldConfig.getRootLocation())) {
controllerBuilder
.setModel(model)
.setView(view);
}
gameObjectViews.forEach(view::addChild);

model.addOnLocationModelStateChangeObserver(view);

Expand All @@ -98,18 +94,22 @@ public Result<Game, Exception> initialize() {
// TODO: consider moving it to separate method
Player player = (Player) GameObjectFactory.fromConfig(gameWorldConfig.getPlayerConfig());
GameObjectView playerView = GameObjectViewFactory.fromConfig(gameWorldConfig.getPlayerConfig());
player.addGameObjectStateChangeObserver(playerView);
controllerBuilder.setPlayer(player);
player.setGameObjectView(playerView);
PlayerController playerController = new PlayerController(player, playerView);

controllerBuilder.setPlayerController(playerController);


Controller controller = controllerBuilder.build();
// TODO: this is a temporary solution
controller.setPlayerView(playerView);
// // TODO: this is a temporary solution
// controller.setPlayerView(playerView);

Game.Builder gameBuilder = new Game.Builder();
gameBuilder.setController(controller);
Game game = gameBuilder
.setController(controller)
.setOnStartAction(new LocationChangeAction(gameWorldConfig.getRootLocation(), player.getPosition()))
.build();

return Result.ok(gameBuilder.build());
return Result.ok(game);
}

public static List<GameObject> loadGameObjectsForLocation(LocationConfig config) {
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/io/rpg/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void start(Stage stage) throws IOException {
Logger logger = LogManager.getLogger(Main.class);

Initializer worldInitializer = new Initializer("configurations/demo-config-1", stage);
Result<Game, Exception> initializationResult = worldInitializer.initialize();
Result<Game, Exception> initializationResult = worldInitializer.initialize();

if (initializationResult.isError()) {
logger.error("Initialization error");
Expand All @@ -37,28 +37,25 @@ 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.getOkValue();
game.start(stage);

stage.setScene(game.getWorldView());
AnimationTimer animationTimer = new AnimationTimer() {
long lastUpdate = -1;

stage.show();

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

game.getController().getCurrentModel().update(difference);
// locationModel.update(difference);
Player player=game.getController().getCurrentModel().getPlayer();
if(player!=null){
// game.getController().getCurrentModel().getPlayer().render();
Player player = game.getController().getCurrentModel().getPlayer();
if (player != null) {
player.render();
}
}
lastUpdate=now;
lastUpdate = now;
}
};

Expand Down
116 changes: 50 additions & 66 deletions src/main/java/io/rpg/controller/Controller.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.rpg.controller;

import io.rpg.model.actions.Action;
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.data.Vector;
import io.rpg.model.location.LocationModel;
import io.rpg.model.object.CollectibleGameObject;
Expand All @@ -11,8 +14,11 @@
import io.rpg.util.Result;
import io.rpg.view.GameObjectView;
import io.rpg.view.LocationView;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
Expand All @@ -27,6 +33,8 @@ public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Obs
private LinkedHashMap<String, LocationView> tagToLocationViewMap;
private Logger logger;
private final PopupController popupController = new PopupController();
private PlayerController playerController;
private Stage mainStage;


public Controller() {
Expand All @@ -49,6 +57,10 @@ public Controller(LinkedHashMap<String, LocationModel> tagToLocationModelMap,
this.currentView = this.tagToLocationViewMap.get(rootTag);
}

public void setMainStage(Stage mainStage) {
this.mainStage = mainStage;
}

public void setModel(@NotNull LocationModel model) {
this.tagToLocationModelMap.put(model.getTag(), model);
currentModel = model;
Expand All @@ -58,10 +70,38 @@ public void setView(Scene currentView) {
this.currentView = currentView;
}

public void registerToViews(List<GameObjectView> views) {
for (GameObjectView view : views) {
view.addOnClickedObserver(this);
public void onAction(Action action) {
Class<?>[] args = {action.getClass()};
Method onSpecificAction;

try {
onSpecificAction = this.getClass().getDeclaredMethod("onAction", args);
} catch (NoSuchMethodException e) {
logger.error("onAction for " + action.getClass().getSimpleName() + "is not implemented");
return;
}

try {
onSpecificAction.invoke(this, action);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

private void onAction(LocationChangeAction action) {
if (!this.tagToLocationModelMap.containsKey(action.destinationLocationTag)) {
logger.error("Unknown location tag");
return;
}

LocationView nextView = this.tagToLocationViewMap.get(action.destinationLocationTag);
LocationModel nextModel = this.tagToLocationModelMap.get(action.destinationLocationTag);

playerController.teleportTo(nextModel, nextView, action.playerPosition);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could also check here if the location is unlocked, for example whether the player has enough points or a specific item in his equipment


this.currentModel = nextModel;
this.currentView = nextView;
mainStage.setScene(nextView);
}

public Scene getView() {
Expand Down Expand Up @@ -91,10 +131,6 @@ else if (tagToLocationViewMap.size() == 0)
return Result.error(new Exception("Empty tag to location view map!"));
else if (tagToLocationViewMap.size() != tagToLocationModelMap.size())
return Result.error(new Exception("Mismatched sizes of maps!"));
else if (currentView == null)
return Result.error(new Exception("No current view set!"));
else if (currentModel == null)
return Result.error(new Exception("No current model set!"));
else
return Result.ok(this);
}
Expand All @@ -107,23 +143,16 @@ public void onKeyboardEvent(KeyboardEvent event) {

KeyEvent payload = event.payload();

if (payload.getEventType() == KeyEvent.KEY_PRESSED){
if (payload.getEventType() == KeyEvent.KEY_PRESSED) {
switch (payload.getCode()) {
case F -> popupController.openPointsPopup(5, getWindowCenterX(), getWindowCenterY());
case G -> popupController.openTextPopup("Hello!", getWindowCenterX(), getWindowCenterY());
case A -> currentModel.getPlayer().setLeftPressed(true);
case D -> currentModel.getPlayer().setRightPressed(true);
case S -> currentModel.getPlayer().setDownPressed(true);
case W -> currentModel.getPlayer().setUpPressed(true);
}
} else if (payload.getEventType() == KeyEvent.KEY_RELEASED) {
switch (payload.getCode()) {
case A -> currentModel.getPlayer().setLeftPressed(false);
case D -> currentModel.getPlayer().setRightPressed(false);
case S -> currentModel.getPlayer().setDownPressed(false);
case W -> currentModel.getPlayer().setUpPressed(false);
case L -> onAction((Action) new LocationChangeAction("location-2", new Position(1, 2)));
}
}
// } else if (payload.getEventType() == KeyEvent.KEY_RELEASED) {
//
// }
}

private int getWindowCenterX() {
Expand Down Expand Up @@ -153,58 +182,14 @@ public void onMouseClickedEvent(MouseClickedEvent event) {
logger.info("Controller notified on click from " + event.source());
}

private void setPlayer(Player gameObject) {
currentModel.setPlayer(gameObject);
}

// TODO: temporary solution
public void setPlayerView(GameObjectView playerView) {
((LocationView) currentView).getViewModel().addChild(playerView);
}

public static class Builder {
private final Controller controller;
private boolean isViewSet = false;
private boolean isModelSet = false;

private Player player;

public Builder() {
controller = new Controller();
}

public Builder setTagToLocationModelMap(LinkedHashMap<String, LocationModel> tagToLocationModelMap) {
controller.setTagToLocationModelMap(tagToLocationModelMap);
return this;
}

public Builder setTagToLocationViewMap(LinkedHashMap<String, LocationView> tagToLocationViewMap) {
controller.setTagToLocationViewMap(tagToLocationViewMap);
return this;
}

public Builder setModel(@NotNull LocationModel model) {
if (!isModelSet) {
isModelSet = true;
controller.setModel(model);
return this;
} else {
throw new IllegalStateException("Attempt to set model for the second time!");
}
}

public Builder setView(Scene currentView) {
if (!isViewSet) {
isViewSet = true;
controller.setView(currentView);
return this;
} else {
throw new IllegalStateException("Attempt to set view for the second time!");
}
}

public Controller build() {
controller.setPlayer(player);
Result<Controller, Exception> validationResult = controller.validate();
if (validationResult.isError()) {
throw new IllegalStateException(validationResult.getErrorValue());
Expand All @@ -231,9 +216,8 @@ public Builder addModelForTag(String tag, LocationModel model) {
return this;
}

public Builder setPlayer(Player gameObject) {
player = gameObject;
return this;
public void setPlayerController(PlayerController playerController) {
controller.playerController = playerController;
}
}
public LocationModel getCurrentModel() {
Expand Down
Loading