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..2886a802 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 @@ -98,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/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..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; @@ -101,9 +102,25 @@ 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()); + 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); + } } } @@ -211,4 +228,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..4ef5acc7 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,12 +17,18 @@ 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); - this.speed = 5f; + // 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 = 100f; this.direction = new Vector(0, 0); this.rightPressed = false; this.leftPressed = false; @@ -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.pixelPosition.x); + gameObjectView.setY(this.pixelPosition.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; + } }