diff --git a/configurations/demo-config-1/locations/location-1/location-1.json b/configurations/demo-config-1/locations/location-1/location-1.json index fd831daa..1d5abd3b 100644 --- a/configurations/demo-config-1/locations/location-1/location-1.json +++ b/configurations/demo-config-1/locations/location-1/location-1.json @@ -49,7 +49,10 @@ { "tag": "object-6", "position": { "row": 9, "col": 9 }, "assetPath": "assets/bush.png" } ], "backgroundPath": "assets/map.png", - "north": "location-2", + "directionToLocationMap": { + "NORTH": "location-2", + "SOUTH": "location-3" + }, "width": 10, "height": 10 } diff --git a/configurations/demo-config-1/locations/location-2/location-2.json b/configurations/demo-config-1/locations/location-2/location-2.json index 185e6463..b6c78256 100644 --- a/configurations/demo-config-1/locations/location-2/location-2.json +++ b/configurations/demo-config-1/locations/location-2/location-2.json @@ -42,6 +42,9 @@ { "tag": "object-8", "position": { "row": 8, "col": 8 }, "assetPath": "assets/coin.png" } ], "backgroundPath": "assets/map.png", + "directionToLocationMap": { + "SOUTH": "location-1" + }, "north": "location-3", "width": 10, "height": 10 diff --git a/configurations/demo-config-1/locations/location-3/location-3.json b/configurations/demo-config-1/locations/location-3/location-3.json index 2e259e82..dd6c6a08 100644 --- a/configurations/demo-config-1/locations/location-3/location-3.json +++ b/configurations/demo-config-1/locations/location-3/location-3.json @@ -48,6 +48,9 @@ { "tag": "object-3", "position": { "row": 1, "col": 1 }, "assetPath": "assets/zombie.png" } ], "backgroundPath": "assets/map.png", + "directionToLocationMap": { + "NORTH": "location-1" + }, "width": 10, "height": 10 } \ No newline at end of file diff --git a/src/main/java/io/rpg/Initializer.java b/src/main/java/io/rpg/Initializer.java index 37521c44..52673716 100644 --- a/src/main/java/io/rpg/Initializer.java +++ b/src/main/java/io/rpg/Initializer.java @@ -7,6 +7,7 @@ import io.rpg.config.model.LocationConfig; import io.rpg.controller.PlayerController; import io.rpg.model.actions.LocationChangeAction; +import io.rpg.model.data.MapDirection; import io.rpg.model.location.LocationModel; import io.rpg.model.object.GameObject; import io.rpg.model.object.Player; @@ -27,6 +28,7 @@ import java.io.IOException; import java.nio.file.Path; +import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -80,11 +82,11 @@ public Result initialize() { List gameObjectViews = loadGameObjectViewsForLocation(locationConfig); registerGameObjectViewsToModel(gameObjects, gameObjectViews); - LocationModel model = new LocationModel.Builder() .setTag(locationConfig.getTag()) .setBounds(new Point2D(locationConfig.getWidth(), locationConfig.getHeight())) .setGameObjects(gameObjects) + .setDirectionToLocationMap(locationConfig.getDirectionToLocationMap()) .build(); LocationView view = loadLocationViewFromConfig(locationConfig); diff --git a/src/main/java/io/rpg/config/model/LocationConfig.java b/src/main/java/io/rpg/config/model/LocationConfig.java index 3dee41c5..a7983cd0 100644 --- a/src/main/java/io/rpg/config/model/LocationConfig.java +++ b/src/main/java/io/rpg/config/model/LocationConfig.java @@ -2,15 +2,14 @@ import com.kkafara.rt.Result; +import io.rpg.model.data.MapDirection; import io.rpg.util.ErrorMessageBuilder; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.nio.file.Files; import java.nio.file.Path; -import java.util.LinkedHashSet; -import java.util.Optional; -import java.util.Set; +import java.util.*; public class LocationConfig { public static final int MAX_HEIGHT = 10; @@ -21,6 +20,9 @@ public class LocationConfig { @NotNull private Set objects; + @NotNull + private HashMap directionToLocationMap; + @Nullable private Path path; @@ -56,6 +58,15 @@ public Set getObjects() { return objects; } + @NotNull + public HashMap getDirectionToLocationMap() { + // same as objects in getObjects the direction map might be null if loaded via Gson + if (directionToLocationMap == null) { + directionToLocationMap = new HashMap<>(); + } + return directionToLocationMap; + } + public void addObjectConfig(@NotNull GameObjectConfig config) { objects.add(config); } diff --git a/src/main/java/io/rpg/model/location/LocationModel.java b/src/main/java/io/rpg/model/location/LocationModel.java index 3957962b..0806e10e 100644 --- a/src/main/java/io/rpg/model/location/LocationModel.java +++ b/src/main/java/io/rpg/model/location/LocationModel.java @@ -9,13 +9,8 @@ import io.rpg.model.data.Position; import io.rpg.model.object.GameObject; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; +import java.util.*; + import javafx.beans.value.ChangeListener; import javafx.geometry.Point2D; import org.apache.logging.log4j.LogManager; @@ -30,7 +25,7 @@ public class LocationModel extends BaseActionEmitter implements LocationModelSta private List gameObjects; private final HashMap> positionListeners; private final HashMap positionGameObjectMap; - private final HashMap directionToLocationMap; + private HashMap directionToLocationMap; public Point2D bounds; private final Set locationModelStateChangeObservers; @@ -46,8 +41,6 @@ private LocationModel() { this.positionListeners = new HashMap<>(); this.positionGameObjectMap = new HashMap<>(); this.directionToLocationMap = new HashMap<>(); - - directionToLocationMap.put(MapDirection.EAST, "location-1"); } public String getTag() { @@ -245,6 +238,11 @@ public Builder setTag(@NotNull String tag) { return this; } + public Builder setDirectionToLocationMap(@NotNull HashMap directionToLocationMap) { + locationModel.directionToLocationMap = directionToLocationMap; + return this; + } + public LocationModel build() { Result result = locationModel.validate();