From 912879165d3a7335c13c35575bb4d1611dcd38b2 Mon Sep 17 00:00:00 2001 From: Zamojtel Bartek Date: Tue, 26 Apr 2022 00:14:11 +0200 Subject: [PATCH 1/2] done movement --- src/main/java/io/rpg/Game.java | 4 +++ src/main/java/io/rpg/Initializer.java | 2 ++ src/main/java/io/rpg/Main.java | 24 +++++++++++++++++ .../java/io/rpg/controller/Controller.java | 5 +++- .../io/rpg/model/location/LocationModel.java | 18 ++++++++++--- .../java/io/rpg/model/object/GameObject.java | 8 ++++++ src/main/java/io/rpg/model/object/Player.java | 27 ++++++++++++++++--- .../java/io/rpg/util/GameObjectFactory.java | 2 +- src/main/java/io/rpg/view/LocationView.java | 13 +++++++++ .../io/rpg/viewmodel/LocationViewModel.java | 4 +++ 10 files changed, 97 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/rpg/Game.java b/src/main/java/io/rpg/Game.java index ecd9f532..14f5dd67 100644 --- a/src/main/java/io/rpg/Game.java +++ b/src/main/java/io/rpg/Game.java @@ -41,4 +41,8 @@ public Builder setController(Controller controller) { return this; } } + + public Controller getController() { + return controller; + } } diff --git a/src/main/java/io/rpg/Initializer.java b/src/main/java/io/rpg/Initializer.java index 1add120d..46365655 100644 --- a/src/main/java/io/rpg/Initializer.java +++ b/src/main/java/io/rpg/Initializer.java @@ -90,6 +90,8 @@ public Result initialize() { .addViewForTag(locationConfig.getTag(), view) .addModelForTag(locationConfig.getTag(), model) .registerToViews(gameObjectViews); + + view.createViewsForObjects(model); } // Player is created separately diff --git a/src/main/java/io/rpg/Main.java b/src/main/java/io/rpg/Main.java index d41e5106..faff9a7f 100644 --- a/src/main/java/io/rpg/Main.java +++ b/src/main/java/io/rpg/Main.java @@ -1,6 +1,8 @@ package io.rpg; +import io.rpg.model.object.Player; import io.rpg.util.Result; +import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.stage.Stage; import org.apache.logging.log4j.Level; @@ -40,6 +42,28 @@ public void start(Stage stage) throws IOException { stage.setScene(game.getWorldView()); stage.show(); + + AnimationTimer animationTimer=new AnimationTimer() { + long lastUpdate=-1; + @Override + public void handle(long now) { + 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.render(); + } + } + lastUpdate=now; + } + }; + + animationTimer.start(); + } public static void main(String[] args) { diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index d269f70b..a6c68d2a 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -101,7 +101,7 @@ else if (currentModel == null) public void onKeyboardEvent(KeyboardEvent event) { // TODO: implement event handling logger.info("Controller notified on key pressed from " + event.source()); - + //TODO: call Player::set...Pressed depending on keyCode and whether the key was pressed or released switch (event.payload().getCode()) { case F -> popupController.openPointsPopup(5, getWindowCenterX(), getWindowCenterY()); } @@ -211,4 +211,7 @@ public Builder setPlayer(Player gameObject) { return this; } } + public LocationModel getCurrentModel() { + return currentModel; + } } diff --git a/src/main/java/io/rpg/model/location/LocationModel.java b/src/main/java/io/rpg/model/location/LocationModel.java index 1b55aa8f..97b227eb 100644 --- a/src/main/java/io/rpg/model/location/LocationModel.java +++ b/src/main/java/io/rpg/model/location/LocationModel.java @@ -63,10 +63,10 @@ private void setTag(String tag) { this.tag = tag; } - @UnmodifiableView - public List getGameObjects() { - return Collections.unmodifiableList(gameObjects); - } +// @UnmodifiableView +// public List getGameObjects() { +// return Collections.unmodifiableList(gameObjects); +// } /** * Private setter for Builder usage only. Notice that ownership of {@link GameObject}s is not @@ -135,4 +135,14 @@ public LocationModel build() { return locationModel; } } + public List getGameObjects() { + return gameObjects; + } + + public void update(float elapsed){ + if(player!=null){ + player.update(elapsed); + } + } + } diff --git a/src/main/java/io/rpg/model/object/GameObject.java b/src/main/java/io/rpg/model/object/GameObject.java index f8a4eddb..315f58d9 100644 --- a/src/main/java/io/rpg/model/object/GameObject.java +++ b/src/main/java/io/rpg/model/object/GameObject.java @@ -3,6 +3,9 @@ import io.rpg.config.model.GameObjectConfig; import io.rpg.model.data.GameObjectStateChange; import io.rpg.model.data.Position; +import io.rpg.model.data.Vector; +import io.rpg.view.GameObjectView; +import javafx.scene.image.ImageView; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -16,6 +19,11 @@ * objects appearing in the game. */ public class GameObject implements GameObjectStateChange.Emitter { + +// protected Vector currentPosition; + +// public GameObjectView view; + /** * Position of game object in model's representation of location. */ diff --git a/src/main/java/io/rpg/model/object/Player.java b/src/main/java/io/rpg/model/object/Player.java index 457c8087..43a943b4 100644 --- a/src/main/java/io/rpg/model/object/Player.java +++ b/src/main/java/io/rpg/model/object/Player.java @@ -2,12 +2,14 @@ import io.rpg.model.data.Position; import io.rpg.model.data.Vector; +import io.rpg.view.GameObjectView; import io.rpg.model.object.GameObject; import javafx.scene.image.Image; import org.jetbrains.annotations.NotNull; public class Player extends GameObject { + Vector currentPosition; int strength; float speed; Vector direction; @@ -15,11 +17,17 @@ public class Player extends GameObject { boolean leftPressed; boolean upPressed; boolean downPressed; - + GameObjectView gameObjectView; private Vector pixelPosition; - public Player(@NotNull String tag, @NotNull Position position) { - super(tag, position); + // public GameObject(@NotNull String tag, @NotNull Position position, @NotNull String assetPath) { +// this.tag = tag; +// this.position = position; +// this.assetPath = assetPath; +// } + public Player(@NotNull String tag, @NotNull Position position, @NotNull String assetPath) { + super(tag, position, assetPath); + this.currentPosition=new Vector(position.col, position.row); this.speed = 5f; this.direction = new Vector(0, 0); this.rightPressed = false; @@ -58,7 +66,7 @@ public void update(float elapsed) { if (rightPressed) x += 1; - this.pixelPosition = new Vector(this.pixelPosition.x + speed * x * elapsed / 1000, this.pixelPosition.y + speed * y * elapsed / 1000); + this.currentPosition = new Vector(this.currentPosition.x + speed * x * elapsed / 1000, this.currentPosition.y + speed * y * elapsed / 1000); } public void setRightPressed(boolean rightPressed) { @@ -80,4 +88,15 @@ public void setDownPressed(boolean downPressed) { public void setStrength(int strength) { this.strength = strength; } + + public void setGameObjectView(GameObjectView gameObjectView) { + this.gameObjectView = gameObjectView; + } + + public void render(){ + if(gameObjectView!=null){ + gameObjectView.setX(this.currentPosition.x); + gameObjectView.setY(this.currentPosition.y); + } + } } diff --git a/src/main/java/io/rpg/util/GameObjectFactory.java b/src/main/java/io/rpg/util/GameObjectFactory.java index e30371cf..2fd84ead 100644 --- a/src/main/java/io/rpg/util/GameObjectFactory.java +++ b/src/main/java/io/rpg/util/GameObjectFactory.java @@ -24,7 +24,7 @@ public static GameObject fromConfig(GameObjectConfig config) { switch (GameObject.Type.valueOf(config.getTypeString().toUpperCase())) { case COLLECTIBLE -> { return new CollectibleGameObject(config.getTag(), config.getPosition()); } case DIALOG -> { return new DialogGameObject(config.getTag(), config.getPosition()); } - case PLAYER -> { return new Player(config.getTag(), config.getPosition()); } + case PLAYER -> { return new Player(config.getTag(), config.getPosition(),config.getAssetPath()); } case NAVIGABLE -> { return new NavigationalGameObject(config.getTag(), config.getPosition()); } default -> throw new RuntimeException("Unknown GameObject type. This should not happen!"); } diff --git a/src/main/java/io/rpg/view/LocationView.java b/src/main/java/io/rpg/view/LocationView.java index 6b033db0..e9c4fbcc 100644 --- a/src/main/java/io/rpg/view/LocationView.java +++ b/src/main/java/io/rpg/view/LocationView.java @@ -100,4 +100,17 @@ public void onLocationModelStateChange(LocationModelStateChange event) { // most likely here we watn to pass this event to LocationViewModel or even // make LocationViewModel implement LocationModelStateChange.Observer } + + + List gameObjectViews=new ArrayList<>(); + + public void createViewsForObjects(LocationModel locationModel){ + for(GameObject g: locationModel.getGameObjects()){ + GameObjectView gameObjectView=new GameObjectView(Path.of(g.getAssetPath()),g.getPosition()); + gameObjectViews.add(gameObjectView); +// g.view=gameObjectView; + viewModel.getForegroundPane().getChildren().add(gameObjectView); + } + } + } diff --git a/src/main/java/io/rpg/viewmodel/LocationViewModel.java b/src/main/java/io/rpg/viewmodel/LocationViewModel.java index 98af8df8..451c5753 100644 --- a/src/main/java/io/rpg/viewmodel/LocationViewModel.java +++ b/src/main/java/io/rpg/viewmodel/LocationViewModel.java @@ -55,4 +55,8 @@ public void initialize(URL location, ResourceBundle resources) { public void addChild(ImageView child) { contentPane.getChildren().add(child); } + + public Pane getForegroundPane() { + return foregroundPane; + } } From 307ad7b3179d8180ba779375463e7d48489d45a6 Mon Sep 17 00:00:00 2001 From: mhawryluk Date: Tue, 26 Apr 2022 02:47:30 +0200 Subject: [PATCH 2/2] feat: Made player movement work. Desperately needs refactoring. --- src/main/java/io/rpg/Initializer.java | 3 +-- .../java/io/rpg/controller/Controller.java | 21 +++++++++++++++++-- src/main/java/io/rpg/model/object/Player.java | 8 +++---- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/rpg/Initializer.java b/src/main/java/io/rpg/Initializer.java index 46365655..2886a802 100644 --- a/src/main/java/io/rpg/Initializer.java +++ b/src/main/java/io/rpg/Initializer.java @@ -100,13 +100,12 @@ public Result initialize() { GameObjectView playerView = GameObjectViewFactory.fromConfig(gameWorldConfig.getPlayerConfig()); player.addGameObjectStateChangeObserver(playerView); controllerBuilder.setPlayer(player); + player.setGameObjectView(playerView); Controller controller = controllerBuilder.build(); // TODO: this is a temporary solution controller.setPlayerView(playerView); - - Game.Builder gameBuilder = new Game.Builder(); gameBuilder.setController(controller); diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index a6c68d2a..30d974b6 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -11,6 +11,7 @@ import io.rpg.view.GameObjectView; import io.rpg.view.LocationView; import javafx.scene.Scene; +import javafx.scene.input.KeyEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; @@ -102,8 +103,24 @@ public void onKeyboardEvent(KeyboardEvent event) { // TODO: implement event handling logger.info("Controller notified on key pressed from " + event.source()); //TODO: call Player::set...Pressed depending on keyCode and whether the key was pressed or released - switch (event.payload().getCode()) { - case F -> popupController.openPointsPopup(5, getWindowCenterX(), getWindowCenterY()); + + KeyEvent payload = event.payload(); + + if (payload.getEventType() == KeyEvent.KEY_PRESSED){ + switch (payload.getCode()) { + case F -> popupController.openPointsPopup(5, 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); + } } } diff --git a/src/main/java/io/rpg/model/object/Player.java b/src/main/java/io/rpg/model/object/Player.java index 43a943b4..4ef5acc7 100644 --- a/src/main/java/io/rpg/model/object/Player.java +++ b/src/main/java/io/rpg/model/object/Player.java @@ -28,7 +28,7 @@ public class Player extends GameObject { public Player(@NotNull String tag, @NotNull Position position, @NotNull String assetPath) { super(tag, position, assetPath); this.currentPosition=new Vector(position.col, position.row); - this.speed = 5f; + this.speed = 100f; this.direction = new Vector(0, 0); this.rightPressed = false; this.leftPressed = false; @@ -66,7 +66,7 @@ public void update(float elapsed) { if (rightPressed) x += 1; - this.currentPosition = new Vector(this.currentPosition.x + speed * x * elapsed / 1000, this.currentPosition.y + speed * y * elapsed / 1000); + this.pixelPosition = new Vector(this.pixelPosition.x + speed * x * elapsed / 1000, this.pixelPosition.y + speed * y * elapsed / 1000); } public void setRightPressed(boolean rightPressed) { @@ -95,8 +95,8 @@ public void setGameObjectView(GameObjectView gameObjectView) { public void render(){ if(gameObjectView!=null){ - gameObjectView.setX(this.currentPosition.x); - gameObjectView.setY(this.currentPosition.y); + gameObjectView.setX(this.pixelPosition.x); + gameObjectView.setY(this.pixelPosition.y); } } }