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
@@ -1,8 +1,8 @@
{
"tag": "location-1",
"objects": [
{ "tag": "object-1", "position": { "row": 0, "col": 5 }, "type": "collectible" },
{ "tag": "object-2", "position": { "row": 1, "col": 3 }, "type": "interactive" }
{ "tag": "object-1", "position": { "row": 0, "col": 5 }, "type": "collectible", "assetPath": "assets/someDude.png" },
{ "tag": "object-2", "position": { "row": 1, "col": 3 }, "type": "dialog", "assetPath": "assets/someDude.png" }
],
"backgroundPath": "file:assets/map.png"
"backgroundPath": "assets/map.png"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"tag": "object-1",
"assetPath": "/path/to/the/asset",
"type": ""
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"tag": "location-2",
"objects": [
{ "tag": "object-1", "position": { "row": 0, "col": 0 }, "type": "collectible" },
{ "tag": "object-3", "position": { "row": 0, "col": 1 }, "type": "navigable" }
{ "tag": "object-1", "position": { "row": 0, "col": 5 }, "type": "collectible", "assetPath": "assets/someDude.png" },
{ "tag": "object-2", "position": { "row": 1, "col": 3 }, "type": "dialog", "assetPath": "assets/someDude.png" }
],
"backgroundPath": "file:assets/map.png"
}
"backgroundPath": "assets/map.png"
}
59 changes: 41 additions & 18 deletions src/main/java/io/rpg/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import io.rpg.model.location.LocationModel;
import io.rpg.model.object.GameObject;
import io.rpg.config.model.GameObjectConfig;
import io.rpg.util.GameObjectFactory;
import io.rpg.util.GameObjectViewFactory;
import io.rpg.util.Result;
import io.rpg.view.GameObjectView;
import io.rpg.view.LocationView;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
Expand All @@ -17,14 +20,13 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;

public class Initializer {
private Path pathToConfigDir;
private ConfigLoader configLoader;
private Stage mainStage;
private final Stage mainStage;

private final Logger logger;

Expand Down Expand Up @@ -56,7 +58,16 @@ public Result<Game, Exception> initialize() {
assert worldConfig.getLocationConfigs().size() > 0 : "There must be at least one location config specified";

for (LocationConfig locationConfig : worldConfig.getLocationConfigs()) {
LocationModel model = loadLocationModelFromConfig(locationConfig);

List<GameObject> gameObjects = loadGameObjectsForLocation(locationConfig);
List<GameObjectView> gameObjectViews = loadGameObjectViewsForLocation(locationConfig);

registerGameObjectViewsToModel(gameObjects, gameObjectViews);

LocationModel model = new LocationModel.Builder()
.setTag(locationConfig.getTag())
.setGameObjects(gameObjects).build();

LocationView view = loadLocationViewFromConfig(locationConfig);

assert view != null;
Expand All @@ -80,6 +91,32 @@ public Result<Game, Exception> initialize() {
return Result.ok(gameBuilder.build());
}

public static List<GameObject> loadGameObjectsForLocation(LocationConfig config) {
return GameObjectFactory.fromConfigList(config.getObjects());
}

public static List<GameObjectView> loadGameObjectViewsForLocation(LocationConfig config) {
return GameObjectViewFactory.fromConfigList(config.getObjects());
}

public static void registerGameObjectViewsToModel(List<GameObject> gameObjects,
List<GameObjectView> gameObjectViews) {
assert gameObjects.size() == gameObjectViews.size() : "Arrays must be of the same length!";

Iterator<GameObject> gameObjectIterator = gameObjects.iterator();
Iterator<GameObjectView> gameObjectViewIterator = gameObjectViews.iterator();

// we asserted earlier that both lists have the same length thus we don't
// need to check .hasNext() for both lists
while (gameObjectIterator.hasNext()) {
GameObject gameObject = gameObjectIterator.next();
GameObjectView gameObjectView = gameObjectViewIterator.next();

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

@Nullable
public static LocationView loadLocationViewFromConfig(LocationConfig config) {
try {
Expand All @@ -89,18 +126,4 @@ public static LocationView loadLocationViewFromConfig(LocationConfig config) {
}
return null;
}

public static LocationModel loadLocationModelFromConfig(LocationConfig config) {
List<GameObjectConfig> gameObjectConfigs = config.getObjects();
List<GameObject> gameObjects = new LinkedList<>();

for (GameObjectConfig goconfig : gameObjectConfigs) {
gameObjects.add(GameObject.fromConfig(goconfig));
}

return new LocationModel(
config.getTag(),
gameObjects
);
}
}
3 changes: 3 additions & 0 deletions src/main/java/io/rpg/config/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ LocationConfig loadLocationConfig(@NotNull String locationTag) throws FileNotFou
BufferedReader reader = new BufferedReader(new FileReader(locationConfigJson.toString()));
LocationConfig config = gson.fromJson(reader, LocationConfig.class);
config.setPath(locationDir);

logger.info("Path to background for location");
logger.info(config.getBackgroundPath());
return config;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/rpg/config/model/GameObjectConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
*/
public class GameObjectConfig extends GameObject {


private String type;

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

public String getType() {
public String getTypeString() {
assert type != null : "Attempt to access uninitialized \"type\" field!";
return type;
}

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/io/rpg/model/data/GameObjectStateChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

import io.rpg.model.object.GameObject;

public record GameObjectStateChange (
/**
* Event representing game object model change.
*/
public record GameObjectStateChange(
GameObject source,
Object payload // TODO: what kind of data do we want? What changed? What can change?
// or maybe implement separate methods for observer & emitter for different kinds
// of event
Object payload // TODO: what kind of data do we want? What changed? What can change?
// or maybe implement separate methods for observer & emitter for different kinds
// of events
) {
/**
* Interface for {@link GameObjectStateChange} observer.
*/
public interface Observer {
void onGameObjectStateChange(GameObjectStateChange event);
}

/**
* Interface for {@link GameObjectStateChange} event emitter.
*/
public interface Emitter {
void emitGameObjectStateChange(GameObjectStateChange event);

Expand Down
11 changes: 10 additions & 1 deletion src/main/java/io/rpg/model/data/KeyboardEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;

final public record KeyboardEvent(Scene source, KeyEvent payload) {
/**
* Represents a keyboard clicked event.
*/
public final record KeyboardEvent(Scene source, KeyEvent payload) {
/**
* Interface for {@link KeyboardEvent} observer.
*/
public interface Observer {
void onKeyboardEvent(KeyboardEvent event);
}

/**
* Interface for {@link KeyboardEvent} emitter.
*/
public interface Emitter {
void addKeyboardEventObserver(KeyboardEvent.Observer observer);

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/rpg/model/data/LocationModelStateChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

import io.rpg.model.location.LocationModel;

/**
* Event representing location model state change.
*/
public record LocationModelStateChange(
LocationModel source,
Object payload // TODO: Same considerations as for GameObjectStateChange
) {
/**
* Interface for {@link LocationModelStateChange} observer.
*/
public interface Observer {
void onLocationModelStateChange(LocationModelStateChange event);
}

/**
* Interface for {@link LocationModelStateChange} event emitter.
*/
public interface Emitter {
void addOnLocationModelStateChangeObserver(Observer observer);

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/rpg/model/data/MouseClickedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
import org.jetbrains.annotations.NotNull;

/**
*
* Represents mouse click event.
*/
final public record MouseClickedEvent(
public record MouseClickedEvent(
@NotNull GameObjectView source,
@NotNull MouseEvent payload
) {
/**
* Interface for {@link MouseClickedEvent} observer.
*/
public interface Observer {
void onMouseClickedEvent(MouseClickedEvent event);
}

/**
* Interface for {@link MouseClickedEvent} emitter.
*/
public interface Emitter {
void emitOnMouseClickedEvent(MouseClickedEvent event);

Expand Down
80 changes: 75 additions & 5 deletions src/main/java/io/rpg/model/location/LocationModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import io.rpg.model.data.LocationModelStateChange;
import io.rpg.model.object.Player;
import io.rpg.model.object.GameObject;
import io.rpg.util.Result;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

/**
* Represents single location in our game
* Represents single location in our game.
*/
public class LocationModel implements LocationModelStateChange.Emitter {
private String tag;
Expand All @@ -20,9 +22,12 @@ public class LocationModel implements LocationModelStateChange.Emitter {
private final Set<LocationModelStateChange.Observer> locationModelStateChangeObservers;

public LocationModel(@NotNull String tag, @NotNull List<GameObject> gameObjects) {
this();
this.tag = tag;
this.gameObjects = gameObjects;
this.player = null;
}

private LocationModel() {
this.locationModelStateChangeObservers = new LinkedHashSet<>();
}

Expand All @@ -34,6 +39,31 @@ public String getTag() {
return tag;
}

/**
* Private setter for Builder usage only.
*
* @param tag tag for the location
*/
private void setTag(String tag) {
this.tag = tag;
}

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

/**
* Private setter for Builder usage only. Notice that ownership of {@link GameObject}s is not
* transferred to LocationModel.
* TODO: Transfer ownership of objects to LoactionModel.
*
* @param gameObjects game object for location
*/
private void setGameObjects(List<GameObject> gameObjects) {
this.gameObjects = gameObjects;
}

@Override
public void addOnLocationModelStateChangeObserver(LocationModelStateChange.Observer observer) {
locationModelStateChangeObservers.add(observer);
Expand All @@ -50,4 +80,44 @@ public void emitLocationModelStateChange(LocationModelStateChange event) {
observer.onLocationModelStateChange(event);
});
}

public Result<Void, Void> validate() {
if (tag == null || gameObjects == null) {
return Result.error(null);
}
return Result.ok(null);
}

public static class Builder {
private final LocationModel locationModel;

private final Logger logger;

public Builder() {
logger = LogManager.getLogger(Builder.class);
locationModel = new LocationModel();
}

public Builder setGameObjects(@NotNull List<GameObject> gameObjects) {
locationModel.setGameObjects(gameObjects);
return this;
}

public Builder setTag(@NotNull String tag) {
assert tag != null : "Location tag must not be null!";
locationModel.setTag(tag);
return this;
}

public LocationModel build() {
Result<Void, Void> result = locationModel.validate();

// TODO(@kkafar): Handle this in better way. Consider returning the result.
if (result.isError()) {
logger.error("Error occurred while building LocationModel.");
}

return locationModel;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.rpg.model.data.Position;
import org.jetbrains.annotations.NotNull;

final public class CollectibleGameObject extends InteractiveGameObject {
public final class CollectibleGameObject extends InteractiveGameObject {
public CollectibleGameObject(@NotNull String tag, @NotNull Position position) {
super(tag, position);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/rpg/model/object/DialogGameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.rpg.model.data.Position;
import org.jetbrains.annotations.NotNull;

final public class DialogGameObject extends InteractiveGameObject {
public final class DialogGameObject extends InteractiveGameObject {
public DialogGameObject(@NotNull String tag, @NotNull Position position) {
super(tag, position);
}
Expand Down
Loading