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
4 changes: 4 additions & 0 deletions src/main/java/io/rpg/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public Builder setController(Controller controller) {
return this;
}
}

public Controller getController() {
return controller;
}
}
5 changes: 3 additions & 2 deletions src/main/java/io/rpg/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public Result<Game, Exception> initialize() {
.addViewForTag(locationConfig.getTag(), view)
.addModelForTag(locationConfig.getTag(), model)
.registerToViews(gameObjectViews);

view.createViewsForObjects(model);
}

// Player is created separately
Expand All @@ -98,13 +100,12 @@ public Result<Game, Exception> 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);

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/io/rpg/Main.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/io/rpg/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -211,4 +228,7 @@ public Builder setPlayer(Player gameObject) {
return this;
}
}
public LocationModel getCurrentModel() {
return currentModel;
}
}
18 changes: 14 additions & 4 deletions src/main/java/io/rpg/model/location/LocationModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ private void setTag(String tag) {
this.tag = tag;
}

@UnmodifiableView
public List<GameObject> getGameObjects() {
return Collections.unmodifiableList(gameObjects);
}
// @UnmodifiableView
// public List<GameObject> getGameObjects() {
// return Collections.unmodifiableList(gameObjects);
// }

/**
* Private setter for Builder usage only. Notice that ownership of {@link GameObject}s is not
Expand Down Expand Up @@ -135,4 +135,14 @@ public LocationModel build() {
return locationModel;
}
}
public List<GameObject> getGameObjects() {
return gameObjects;
}

public void update(float elapsed){
if(player!=null){
player.update(elapsed);
}
}

}
8 changes: 8 additions & 0 deletions src/main/java/io/rpg/model/object/GameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*/
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/io/rpg/model/object/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@

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;
boolean rightPressed;
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;
Expand Down Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/rpg/util/GameObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/rpg/view/LocationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<GameObjectView> 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);
}
}

}
4 changes: 4 additions & 0 deletions src/main/java/io/rpg/viewmodel/LocationViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}