diff --git a/src/main/java/io/rpg/controller/PopupController.java b/src/main/java/io/rpg/controller/PopupController.java index 9d5fb6d5..42bd4964 100644 --- a/src/main/java/io/rpg/controller/PopupController.java +++ b/src/main/java/io/rpg/controller/PopupController.java @@ -13,6 +13,7 @@ import javafx.stage.Stage; import javafx.stage.StageStyle; + public class PopupController { private final Stage popupStage = new Stage(StageStyle.TRANSPARENT); @@ -130,7 +131,6 @@ public void openDialoguePopup(String text, Image npcImage, int x, int y) { } } - public void hidePopup() { popupStage.hide(); } diff --git a/src/main/java/io/rpg/model/actions/ActionEngine.java b/src/main/java/io/rpg/model/actions/ActionEngine.java index decf893f..6728e27c 100644 --- a/src/main/java/io/rpg/model/actions/ActionEngine.java +++ b/src/main/java/io/rpg/model/actions/ActionEngine.java @@ -85,9 +85,10 @@ public void onAction(QuizAction action) { public void acceptQuizResult(boolean correct, int pointsCount) { var controller = controller(); if (correct) { - controller.getPlayerController().addPoints(pointsCount); - if (pointsCount > 0) + if (pointsCount > 0) { controller.getPopupController().openPointsPopup(pointsCount, controller.getWindowCenterX(), controller.getWindowCenterY()); + controller.getPlayerController().addPoints(pointsCount); + } } else { controller.getPopupController().hidePopup(); logger.info("Wrong answer provided"); @@ -107,8 +108,7 @@ public void onAction(BattleAction action) { GameObject opponent = action.getOpponent(); int reward = action.getReward(); BattleResult result; - if (player.getPoints() > opponent.getStrength()) { - player.addPoints(reward); + if (player.getStrength() > opponent.getStrength()) { player.addDefeatedOpponent(opponent.getTag()); result = new BattleResult(BattleResult.Result.VICTORY, reward); } else if (player.getStrength() < opponent.getStrength()) { @@ -118,9 +118,16 @@ public void onAction(BattleAction action) { } controller().getPopupController().openTextPopup(result.getMessage(), controller().getWindowCenterX(), controller().getWindowCenterY()); + if (player.getStrength() > opponent.getStrength()) + player.addPoints(reward); }); } + public void onAction(LevelUpAction action) { + controller().getPopupController().openTextPopup("Achieved level " + action.newLevel + "!", + controller().getWindowCenterX(), controller().getWindowCenterY()); + } + public void onAction(CollectAction action) { actionGuard(action, () -> { var controller = controller(); diff --git a/src/main/java/io/rpg/model/actions/LevelUpAction.java b/src/main/java/io/rpg/model/actions/LevelUpAction.java new file mode 100644 index 00000000..a4c14578 --- /dev/null +++ b/src/main/java/io/rpg/model/actions/LevelUpAction.java @@ -0,0 +1,20 @@ +package io.rpg.model.actions; + +import io.rpg.model.actions.condition.Condition; + +/** + * Class for storing local data to perform level up action + */ +public class LevelUpAction extends BaseAction { + public final int newLevel; + + public LevelUpAction(int newLevel, Condition condition) { + super(condition); + this.newLevel = newLevel; + } + + @Override + public void acceptActionEngine(ActionEngine engine) { + engine.onAction(this); + } +} diff --git a/src/main/java/io/rpg/model/object/Player.java b/src/main/java/io/rpg/model/object/Player.java index b0e0c587..c8bc51c1 100644 --- a/src/main/java/io/rpg/model/object/Player.java +++ b/src/main/java/io/rpg/model/object/Player.java @@ -1,5 +1,7 @@ package io.rpg.model.object; +import io.rpg.model.actions.Action; +import io.rpg.model.actions.LevelUpAction; import io.rpg.model.data.Inventory; import io.rpg.model.data.Position; import io.rpg.view.GameObjectView; @@ -21,6 +23,8 @@ public class Player extends GameObject { private boolean downPressed; private GameObjectView gameObjectView; private int points; + private int level; + public Action levelUpAction; private final Inventory inventory; private final Set defeatedOpponents; @@ -34,6 +38,7 @@ public Player(@NotNull String tag, @NotNull Position position, @NotNull String a this.upPressed = false; this.downPressed = false; this.strength = 0; + this.level = 1; this.defeatedOpponents = new LinkedHashSet<>(); this.inventory = new Inventory(); } @@ -100,6 +105,7 @@ public int getPoints() { public void addPoints(int value) { points += value; + updateLevel(); } public void removePoints(int value) { @@ -114,6 +120,21 @@ public Inventory getInventory() { return inventory; } + public int getLevel() { + return level; + } + + public void updateLevel() { //TODO: update different types of stats instead of just strength. Also do not use an arbitrary value + int levelDifference = points / 10; + points %= 10; + if (levelDifference != 0) { + level += levelDifference; + updateStrength(10); + levelUpAction = new LevelUpAction(level, null); + emitAction(levelUpAction); + } + } + public void addDefeatedOpponent(@NotNull String tag) { this.defeatedOpponents.add(tag); }