Skip to content

Commit

Permalink
feat: implement item-required with inventory! (#88)
Browse files Browse the repository at this point in the history
* feat: implement item-required with inventory!

* fix: NPE in Controller
  • Loading branch information
kkafar authored Jun 6, 2022
1 parent 9191c50 commit e8273d1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/main/java/io/rpg/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/rpg/model/actions/ActionEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/rpg/model/data/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,22 @@ public Inventory() {
public void add(InventoryGameObjectView object) {
items.add(object);
}

public List<InventoryGameObjectView> 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;
}
}
5 changes: 4 additions & 1 deletion src/main/java/io/rpg/view/InventoryGameObjectView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit e8273d1

Please sign in to comment.