Skip to content

Commit

Permalink
@co012/position and bounds (#33)
Browse files Browse the repository at this point in the history
* chore: deleted tag setter

* chore: change checkstyle.xml to allow method chaining

* chore: remove duplicate code

* chore: removed assetPath from GameObject and GameObjectConfig no longer extends GameObject

* feat: Made DiscretePane

* chore: got rid of SCALE

* chore: improve DiscretePane

* chore: replace Vector with build-in Point2D

* chore: preparation for event base position changing

* feat: Bounds are working

* chore: made position observable

* chore: bound Player and PlayerView positions

* chore: remove Plyer field fromm LocationModel

* chore: new constructor from Point2D

* chore: object collisions

* chore: better LocationModel.getObject function

* Update src/main/java/io/rpg/model/location/LocationModel.java

Co-authored-by: Marcin Hawryluk <70582973+mhawryluk@users.noreply.github.com>

* chore: better bounds

Co-authored-by: Marcin Hawryluk <70582973+mhawryluk@users.noreply.github.com>
  • Loading branch information
co012 and mhawryluk authored May 14, 2022
1 parent 7e71a20 commit 5d17086
Show file tree
Hide file tree
Showing 19 changed files with 305 additions and 206 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ test {
useJUnitPlatform()
}

compileJava {
options.compilerArgs += ["--add-exports=javafx.graphics/com.sun.javafx.scene=io.rpg"]
}

run {
jvmArgs = ['--add-exports=javafx.graphics/com.sun.javafx.scene=io.rpg']
}

jlink {
imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
Expand Down
5 changes: 0 additions & 5 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,6 @@
<property name="tokens" value="COMMA"/>
<property name="option" value="EOL"/>
</module>
<module name="SeparatorWrap">
<!-- ELLIPSIS is EOL until https://github.com/google/styleguide/issues/259 -->
<property name="id" value="SeparatorWrapEllipsis"/>
<property name="option" value="EOL"/>
</module>
<module name="SeparatorWrap">
<!-- ARRAY_DECLARATOR is EOL until https://github.com/google/styleguide/issues/258 -->
<property name="id" value="SeparatorWrapArrayDeclarator"/>
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/io/rpg/Initializer.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package io.rpg;

import io.rpg.config.ConfigLoader;
import io.rpg.config.model.PlayerConfig;
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;
import io.rpg.model.object.Player;
import io.rpg.util.GameObjectFactory;
import io.rpg.util.GameObjectViewFactory;
Expand Down Expand Up @@ -79,15 +76,13 @@ public Result<Game, Exception> initialize() {
assert view != null;

gameObjectViews.forEach(view::addChild);

model.addOnLocationModelStateChangeObserver(view);

controllerBuilder
.addViewForTag(locationConfig.getTag(), view)
.addModelForTag(locationConfig.getTag(), model)
.registerToViews(gameObjectViews);

view.createViewsForObjects(model);
}

// Player is created separately
Expand All @@ -100,8 +95,6 @@ public Result<Game, Exception> initialize() {


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

Game.Builder gameBuilder = new Game.Builder();
Game game = gameBuilder
Expand Down Expand Up @@ -135,6 +128,7 @@ public static void registerGameObjectViewsToModel(List<GameObject> gameObjects,

// registration
gameObject.addGameObjectStateChangeObserver(gameObjectView);
gameObjectView.bindToGameObject(gameObject);
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/io/rpg/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ public void handle(long now) {
if (lastUpdate != -1) {
float difference = (now - lastUpdate) / 1e6f;

game.getController().getCurrentModel().update(difference);
Player player = game.getController().getCurrentModel().getPlayer();
if (player != null) {
player.render();
}
game.getController().getPlayerController().getPlayer().update(difference);
}
lastUpdate = now;
}
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/io/rpg/config/model/GameObjectConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,28 @@
* Represents {@link io.rpg.model.object.GameObject} configuration provided by user
* in configuration files.
*/
public class GameObjectConfig extends GameObject {
public class GameObjectConfig {

private String type;
private String tag;
private Position position;
private String assetPath;

public GameObjectConfig(@NotNull String tag, @NotNull Position position) {
super(tag, position);
this.tag = tag;
this.position = position;
}

public String getTag() {
return tag;
}

public Position getPosition() {
return position;
}

public String getAssetPath() {
return assetPath;
}

public String getTypeString() {
Expand All @@ -39,15 +55,10 @@ public Result<GameObjectConfig, Exception> validate() {
}

public void updateFrom(GameObjectConfig gameObjectConfig) {
if (gameObjectConfig.getPosition() != null) {
this.position = gameObjectConfig.getPosition();
}
this.position = gameObjectConfig.position;
if (gameObjectConfig.getTypeString() != null) {
this.type = gameObjectConfig.getTypeString();
}
if (gameObjectConfig.getAssetPath() != null) {
this.assetPath = gameObjectConfig.assetPath;
}
}

public String getFieldDescription() {
Expand All @@ -68,9 +79,7 @@ public String getFieldDescription() {

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("\n{\n").append(super.getFieldDescription()).append(getFieldDescription());
return builder.append("}").toString();
return "\n{\n" + getFieldDescription() + "}";
}

}
22 changes: 16 additions & 6 deletions src/main/java/io/rpg/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
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.*;
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 java.util.function.Supplier;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
Expand Down Expand Up @@ -135,7 +136,7 @@ else if (tagToLocationViewMap.size() != tagToLocationModelMap.size())
@Override
public void onKeyboardEvent(KeyboardEvent event) {
// TODO: implement event handling
logger.info("Controller notified on key pressed from " + event.source());
logger.trace("Controller notified on key pressed from " + event.source());
//TODO: call Player::set...Pressed depending on keyCode and whether the key was pressed or released

KeyEvent payload = event.payload();
Expand Down Expand Up @@ -163,23 +164,32 @@ private int getWindowCenterY() {

@Override
public void onMouseClickedEvent(MouseClickedEvent event) {
int SCALE = 64;
Vector playerPos = currentModel.getPlayer().getPixelPosition();
Point2D playerPos = playerController.getPlayer().getExactPosition();
GameObjectView objectView = event.source();
GameObject object = currentModel.getObject((int) objectView.getY() / SCALE, (int) objectView.getX() / SCALE);
if (Math.abs(playerPos.x - objectView.getX()) / SCALE <= 1.5 && Math.abs(playerPos.y - objectView.getY()) / SCALE <= 1.5) {
Position position = new Position(objectView.getPosition());
GameObject object = currentModel.getObject(position)
.orElseThrow(() -> new RuntimeException("No object present at position " + position));

double distance = playerPos.distance(objectView.getPosition());
if (distance < 1.5) {
if (object instanceof InteractiveGameObject) {
((InteractiveGameObject) object).onAction();
}

if (object instanceof CollectibleGameObject) {
popupController.openTextImagePopup("Picked up an item!", objectView.getImage(), getWindowCenterX(), getWindowCenterY());
objectView.setVisible(false);
currentModel.removeGameObject(object);
}
}

logger.info("Controller notified on click from " + event.source());
}

public PlayerController getPlayerController() {
return playerController;
}

public static class Builder {
private final Controller controller;

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/io/rpg/controller/PlayerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class PlayerController implements KeyboardEvent.Observer {
public PlayerController(Player player, GameObjectView playerView) {
this.player = player;
this.playerView = playerView;
playerView.bindToGameObject(player);
this.onChangeLocation = () -> {};

player.addGameObjectStateChangeObserver(playerView);
Expand Down Expand Up @@ -54,7 +55,7 @@ public void teleportTo(LocationModel model, LocationView view, Position playerPo
updateOnChangeLocation(model, view);

player.setPosition(playerPosition);
model.setPlayer(player);
model.addGameObject(player);
view.addChild(playerView);
view.addKeyboardEventObserver(this);
}
Expand All @@ -63,6 +64,11 @@ private void updateOnChangeLocation(LocationModel model, LocationView view) {
this.onChangeLocation = () -> {
view.removeKeyboardEventObserver(this);
view.removeChild(this.playerView);
model.removeGameObject(player);
};
}

public Player getPlayer() {
return player;
}
}
8 changes: 6 additions & 2 deletions src/main/java/io/rpg/model/data/Position.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package io.rpg.model.data;

import java.util.Objects;
import javafx.geometry.Point2D;

/**
* Represents current position by holding row / col values.
* This class can NOT be record due to some issues with
* Gson library.
*/
public class Position {
public final class Position {
public final int row;

public final int col;

public Position(int row, int col) {
this.row = row;
this.col = col;
}

public Position(Point2D point2D) {
this((int) Math.round(point2D.getY()), (int) Math.round(point2D.getX()));
}

public int getRow() {
return row;
}
Expand Down
25 changes: 0 additions & 25 deletions src/main/java/io/rpg/model/data/Vector.java

This file was deleted.

Loading

0 comments on commit 5d17086

Please sign in to comment.