Skip to content

Commit

Permalink
@zamojtel/in progress (#82)
Browse files Browse the repository at this point in the history
* feat: add equipment

* chore: add simple equipment system for the player

Collected collectibles are now displayed if we click on a non-collectible item

* feat: Improve equipment system

* add InventoryPopup

* Update src/main/java/io/rpg/controller/PopupController.java

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

* Update src/main/java/io/rpg/model/data/Inventory.java

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

* Update src/main/java/io/rpg/model/object/CollectibleGameObject.java

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

* Update src/main/java/io/rpg/view/InventoryPopup.java

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

* Update src/main/java/io/rpg/viewmodel/InventoryPopupViewModel.java

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

* Update src/main/java/io/rpg/view/InventoryGameObjectView.java

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

* fix: make equipment work on current arch

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

* add minor improvements

(Proper)Gathering items is now available, collectibles are no longer visible on the ground after picking them up

* fix: proper items are displayed in the equipment

* chore: add battle logic implementation

* chore: add BattlePopup

* changes

* fix: make things work

KLEJONE GRUBĄ WARSTWĄ KLEJU

Co-authored-by: Marcin Hawryluk <70582973+mhawryluk@users.noreply.github.com>
Co-authored-by: Kacper Kafara <kacperkafara@gmail.com>
  • Loading branch information
3 people authored Jun 5, 2022
1 parent 8c1ca50 commit fea281c
Show file tree
Hide file tree
Showing 23 changed files with 548 additions and 8 deletions.
Binary file added assets/sword6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tag": "object-2",
"position": { "row": 5, "col": 5 },
"assetPath": "assets/key.png",
"onLeftClick": {
"tag": "dialogue-action",
"type": "collect",
"assetPath": "assets/coin.png"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
],
"assetPath": "assets/goblin.png"
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/rpg/config/model/ActionConfigBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class ActionConfigBundle implements ConfigWithValidation {
/**
* {@link io.rpg.model.actions.DialogueAction} <br>
* {@link io.rpg.model.actions.ShowDescriptionAction} <br>
* {@link io.rpg.model.actions.CollectAction} <br>
* TODO
*/
@Nullable
Expand Down Expand Up @@ -263,6 +264,10 @@ Result<Void, Exception> validateForBattle() {
return Result.ok();
}

Result<Void, Exception> validateForCollectAction() {
return Result.ok();
}

Result<Void, Exception> validateBasic() {
ErrorMessageBuilder builder = new ErrorMessageBuilder();
if (tag == null) {
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/io/rpg/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
import io.rpg.model.data.MouseClickedEvent;
import io.rpg.model.data.Position;
import io.rpg.model.location.LocationModel;
import io.rpg.model.object.CollectibleGameObject;
import io.rpg.model.object.GameObject;
import io.rpg.model.object.Player;
import io.rpg.model.object.Question;
import io.rpg.util.BattleResult;
import io.rpg.view.GameEndView;
import io.rpg.view.GameObjectView;
import io.rpg.view.InventoryPopup;
import io.rpg.view.LocationView;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import io.rpg.view.popups.TextPopup;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
Expand All @@ -41,10 +46,8 @@ public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Obs
private final ConditionEngine conditionEngine;
private final ActionEngine actionEngine;


public Controller() {
logger = LogManager.getLogger(Controller.class);

tagToLocationModelMap = new LinkedHashMap<>();
tagToLocationViewMap = new LinkedHashMap<>();

Expand Down Expand Up @@ -158,6 +161,7 @@ public void onKeyboardEvent(KeyboardEvent event) {
case Q -> popupController.openQuestionPopup(new Question("How many bits are there in one byte?", new String[]{"1/8", "1024", "8", "256"}, 'C'), getWindowCenterX(), getWindowCenterY());
case L -> consumeAction(new LocationChangeAction("location-2", new Position(1, 2), null));
case U -> consumeAction(new GameEndAction("You have pressed the forbidden button", null));
case E -> popupController.openInventoryPopup(playerController.getPlayer().getInventory(), getWindowCenterX(), getWindowCenterY(), playerController.getPlayer());
}
}
}
Expand All @@ -176,7 +180,7 @@ public void onMouseClickedEvent(MouseClickedEvent event) {
GameObjectView objectView = event.source();
Position position = new Position(objectView.getPosition());
GameObject object = currentModel.getObject(position)
.orElseThrow(() -> new RuntimeException("No object present at position " + position));
.orElseThrow(() -> new RuntimeException("No object present at position " + position));

double distance = playerPos.distance(objectView.getPosition());

Expand All @@ -192,6 +196,13 @@ public void onMouseClickedEvent(MouseClickedEvent event) {
logger.info("Controller notified on click from " + event.source());
}

public void removeObjectFromModel(GameObject object) {
String tag = currentModel.getTag();
LocationView currentLocationView = tagToLocationViewMap.get(tag);
currentLocationView.removeViewBoundToObject(object);
currentModel.removeGameObject(object);
}

public PlayerController getPlayerController() {
return playerController;
}
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/io/rpg/controller/PopupController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package io.rpg.controller;

import io.rpg.model.object.Player;
import io.rpg.model.object.Question;
import io.rpg.view.BattlePopup;
import io.rpg.view.popups.DialoguePopup;
import io.rpg.view.popups.QuestionPopup;
import io.rpg.model.data.Inventory;
import io.rpg.view.InventoryPopup;
import io.rpg.view.popups.TextImagePopup;
import io.rpg.view.popups.TextPopup;
import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
Expand Down Expand Up @@ -33,6 +38,7 @@ public void openTextPopup(String text, int x, int y) {
popupStage.setX(x - popupScene.getWidth() / 2);
popupStage.setY(y - popupScene.getHeight() / 2);
});

if (!popupStage.isShowing()) {
popupStage.showAndWait();
}
Expand All @@ -56,6 +62,34 @@ public void openPointsPopup(int pointsCount, int x, int y) {
openTextImagePopup("You earned " + pointsCount + " points!", coinImage, x, y);
}

public void openInventoryPopup(Inventory inventory, int x, int y, Player player) {
// InventoryPopup inventoryPopup=new InventoryPopup();
// final Stage popupStage = new Stage(StageStyle.TRANSPARENT);

InventoryPopup popupScene = new InventoryPopup(inventory,player);
popupStage.setScene(popupScene);

popupStage.onShownProperty().setValue(event -> {
popupStage.setX(x - popupScene.getWidth() / 2);
popupStage.setY(y - popupScene.getHeight() / 2);
});

if (!popupStage.isShowing()) {
popupStage.showAndWait();
}
}

public void openBattlePopup(Player player,int x,int y){
BattlePopup battlePopup = new BattlePopup(player);
this.popupStage.setScene(battlePopup);
popupStage.show();
popupStage.setX(x - battlePopup.getWidth() / 2);
popupStage.setY(y - battlePopup.getHeight() / 2);
battlePopup.setCloseButtonActionListener((event)->{
popupStage.close();
});
}

public void openQuestionPopup(Question question, int x, int y, Runnable successCallback, Runnable failureCallback) {
QuestionPopup popupScene = new QuestionPopup(question);
popupScene.setSuccessCallback(successCallback);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/io/rpg/model/actions/ActionEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import io.rpg.model.object.GameObject;
import io.rpg.model.object.Player;
import io.rpg.util.BattleResult;
import io.rpg.view.InventoryGameObjectView;
import io.rpg.view.LocationView;
import javafx.application.Platform;
import javafx.scene.image.Image;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -119,6 +121,17 @@ public void onAction(BattleAction action) {
});
}

public void onAction(CollectAction action) {
actionGuard(action, () -> {
var controller = controller();
controller.getPopupController().openTextImagePopup("Picked up an item!", new Image("file:" + action.getAssetPath()),
controller.getWindowCenterX(), controller.getWindowCenterY());
controller.getPlayerController().getPlayer().getInventory()
.add(new InventoryGameObjectView(action.getOwner(), action.getAssetPath()));
controller.removeObjectFromModel(action.getOwner());
});
}

private void actionGuard(BaseAction action, Runnable actionLogic) {
if (action.getCondition() != null && !action.getCondition().acceptEngine(controller().getConditionEngine())) {
logger.info("Action not executed due to condition being not satisfied");
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/io/rpg/model/actions/ActionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public enum ActionType {
Dialogue("dialogue"),

@SerializedName("battle")
Battle("battle");
Battle("battle"),

@SerializedName("collect")
Collect("collect");

private final String asString;

Expand All @@ -41,6 +44,7 @@ public static Optional<ActionType> fromString(String action) {
case "show-description" -> { return Optional.of(ShowDescription); }
case "dialogue" -> { return Optional.of(Dialogue); }
case "battle" -> { return Optional.of(Battle); }
case "collect" -> { return Optional.of(Collect); }
default -> { return Optional.empty(); }
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/io/rpg/model/actions/CollectAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.rpg.model.actions;

import io.rpg.model.actions.condition.Condition;
import io.rpg.model.object.GameObject;
import org.jetbrains.annotations.Nullable;

public class CollectAction extends BaseAction {
private final String assetPath;

public CollectAction(String assetPath, @Nullable Condition condition) {
super(condition);
this.assetPath = assetPath;
}

public GameObject getOwner() {
return getEmitter();
}

@Override
public void acceptActionEngine(ActionEngine engine) {
engine.onAction(this);
}

public String getAssetPath() {
return assetPath;
}
}

20 changes: 20 additions & 0 deletions src/main/java/io/rpg/model/data/Inventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.rpg.model.data;

import io.rpg.model.object.GameObject;
import io.rpg.view.InventoryGameObjectView;

import java.util.ArrayList;
import java.util.List;

public class Inventory {

public List<InventoryGameObjectView> items;

public Inventory() {
items = new ArrayList<>();
}

public void add(InventoryGameObjectView object) {
items.add(object);
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/rpg/model/object/CollectibleGameObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.rpg.model.object;

import io.rpg.model.data.Position;
import org.jetbrains.annotations.NotNull;

public final class CollectibleGameObject extends InteractiveGameObject {
public CollectibleGameObject(@NotNull String tag, @NotNull Position position, String assetPath) {
super(tag, position, assetPath);
}

@Override
public void onAction() {
System.out.println("Collectible object action");
}
}
1 change: 1 addition & 0 deletions src/main/java/io/rpg/model/object/GameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.rpg.model.data.Position;
import io.rpg.util.DataObjectDescriptionProvider;
import javafx.application.Platform;
import io.rpg.view.GameObjectView;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Point2D;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/rpg/model/object/InteractiveGameObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.rpg.model.object;

import io.rpg.model.data.Position;
import org.jetbrains.annotations.NotNull;

public abstract class InteractiveGameObject extends GameObject {
public InteractiveGameObject(@NotNull String tag, @NotNull Position position, String assetPath) {
super(tag, position);
}
public InteractiveGameObject(@NotNull String tag, @NotNull Position position) {
super(tag, position);
}

abstract public void onAction();
}
11 changes: 11 additions & 0 deletions src/main/java/io/rpg/model/object/Player.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.rpg.model.object;

import io.rpg.model.data.Inventory;
import io.rpg.model.data.Position;
import io.rpg.view.GameObjectView;
import javafx.geometry.Point2D;
Expand All @@ -20,6 +21,7 @@ public class Player extends GameObject {
private boolean downPressed;
private GameObjectView gameObjectView;
private int points;
private final Inventory inventory;

private final Set<String> defeatedOpponents;

Expand All @@ -33,6 +35,7 @@ public Player(@NotNull String tag, @NotNull Position position, @NotNull String a
this.downPressed = false;
this.strength = 0;
this.defeatedOpponents = new LinkedHashSet<>();
this.inventory = new Inventory();
}

public void updateStrength(int value) {
Expand Down Expand Up @@ -99,8 +102,16 @@ public void addPoints(int value) {
points += value;
}

public void removePoints(int value) {
points += value;
}

public int getStrength() {
return strength;
}

public Inventory getInventory() {
return inventory;
}

public void addDefeatedOpponent(@NotNull String tag) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/rpg/util/ActionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ private static Action actionByType(ActionConfigBundle config) {
case Battle -> {
return battleActionFromConfig(config);
}
case Collect -> {
return collectActionFromConfig(config);
}
default -> {
throw new IllegalArgumentException("Unexpected action type!");
}
Expand Down Expand Up @@ -92,4 +95,8 @@ private static void initBeforeAndAfterActions(Action action, ActionConfigBundle
action.setAfterAction(actionByType(config.getAfterAction()));
}
}

private static CollectAction collectActionFromConfig(ActionConfigBundle config) {
return new CollectAction(config.getAssetPath(), ConditionFactory.fromConfig(config.getCondition()));
}
}
1 change: 1 addition & 0 deletions src/main/java/io/rpg/util/GameObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.rpg.config.model.PlayerConfig;
import io.rpg.model.actions.Action;
import io.rpg.model.actions.BattleAction;
import io.rpg.model.actions.CollectAction;
import io.rpg.model.actions.ShowDescriptionAction;
import io.rpg.model.object.GameObject;
import io.rpg.model.object.Player;
Expand Down
Loading

0 comments on commit fea281c

Please sign in to comment.