From 6cb2c9c9e7bc95ad12a6bc3cc5beb6e86e28de2a Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Mon, 6 Jun 2022 20:20:53 +0200 Subject: [PATCH 1/2] feat: implement item-required with inventory! --- .../io/rpg/model/actions/ActionEngine.java | 2 +- .../actions/condition/ConditionEngine.java | 14 +++++--------- src/main/java/io/rpg/model/data/Inventory.java | 18 ++++++++++++++++++ .../io/rpg/view/InventoryGameObjectView.java | 5 ++++- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/rpg/model/actions/ActionEngine.java b/src/main/java/io/rpg/model/actions/ActionEngine.java index b4d578c9..decf893f 100644 --- a/src/main/java/io/rpg/model/actions/ActionEngine.java +++ b/src/main/java/io/rpg/model/actions/ActionEngine.java @@ -127,7 +127,7 @@ public void onAction(CollectAction action) { controller.getPopupController().openTextImagePopup("Picked up an item!", new Image(GameObjectView.resolvePathToJFXFormat(action.getAssetPath())), controller.getWindowCenterX(), controller.getWindowCenterY()); controller.getPlayerController().getPlayer().getInventory() - .add(new InventoryGameObjectView(action.getAssetPath(), action.getDescription())); + .add(new InventoryGameObjectView(action.getAssetPath(), action.getDescription(), action.getOwner().getTag())); controller.removeObjectFromModel(action.getOwner()); }); } diff --git a/src/main/java/io/rpg/model/actions/condition/ConditionEngine.java b/src/main/java/io/rpg/model/actions/condition/ConditionEngine.java index cc1521d2..e8640de3 100644 --- a/src/main/java/io/rpg/model/actions/condition/ConditionEngine.java +++ b/src/main/java/io/rpg/model/actions/condition/ConditionEngine.java @@ -18,15 +18,11 @@ private Controller controller() throws IllegalStateException { } public boolean evaluate(ItemRequiredCondition condition) { - // TODO: Implement this when the inventory gets implemented. - // Scheme: -// return controller() -// .getPlayerController() -// .getPlayer() -// .getInventory() -// .getItems() -// .containsItemForTag(condition.getItemTag()); - return true; + return controller() + .getPlayerController() + .getPlayer() + .getInventory() + .containsItemForTag(condition.getItemTag()); } public boolean evaluate(DefeatOpponentCondition condition) { diff --git a/src/main/java/io/rpg/model/data/Inventory.java b/src/main/java/io/rpg/model/data/Inventory.java index 60b93f3f..a2d1061e 100644 --- a/src/main/java/io/rpg/model/data/Inventory.java +++ b/src/main/java/io/rpg/model/data/Inventory.java @@ -17,4 +17,22 @@ public Inventory() { public void add(InventoryGameObjectView object) { items.add(object); } + + public List getItems() { + return items; + } + + + public boolean containsItemForTag(String itemTag) { + if (itemTag == null) { + return false; + } + + for (InventoryGameObjectView item : items) { + if (itemTag.equals(item.tag)) { + return true; + } + } + return false; + } } diff --git a/src/main/java/io/rpg/view/InventoryGameObjectView.java b/src/main/java/io/rpg/view/InventoryGameObjectView.java index ffb0cc49..a7b68dea 100644 --- a/src/main/java/io/rpg/view/InventoryGameObjectView.java +++ b/src/main/java/io/rpg/view/InventoryGameObjectView.java @@ -7,10 +7,13 @@ public class InventoryGameObjectView extends ImageView { public final String description; - public InventoryGameObjectView(String assetPath, String description) { + public final String tag; + + public InventoryGameObjectView(String assetPath, String description, String tag) { Image image = new Image(GameObjectView.resolvePathToJFXFormat(assetPath)); setImage(image); this.description = description; + this.tag = tag; } } From 254a17a64c2b7b5ace1fb01f0effb37e8058b115 Mon Sep 17 00:00:00 2001 From: Kacper Kafara Date: Mon, 6 Jun 2022 20:26:21 +0200 Subject: [PATCH 2/2] fix: NPE in Controller --- src/main/java/io/rpg/controller/Controller.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/rpg/controller/Controller.java b/src/main/java/io/rpg/controller/Controller.java index 2622ca1f..ea1356c9 100644 --- a/src/main/java/io/rpg/controller/Controller.java +++ b/src/main/java/io/rpg/controller/Controller.java @@ -76,7 +76,9 @@ public void setMainStage(Stage mainStage) { } }); - mainStage.getScene().getWindow().addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, (event) -> popupController.hidePopup()); + if (mainStage.getScene() != null) { + mainStage.getScene().getWindow().addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, (event) -> popupController.hidePopup()); + } } public void setCurrentModel(@NotNull LocationModel model) {