Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@zamojtel/rpg 116 equipment #38

Merged
merged 10 commits into from
May 24, 2022
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"tag": "object-2",
"onLeftClick": {"tag": "dialogue-action", "type": "dialogue", "statements": ["A key.,A, keyA, key.A, key.A, key.A, key.A, key.A, key.A key."], "assetPath": "assets/key.png"}
"onLeftClick": {"tag": "dialogue-action", "type": "collect"}
}
4 changes: 4 additions & 0 deletions src/main/java/io/rpg/config/model/ActionConfigBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,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
20 changes: 16 additions & 4 deletions src/main/java/io/rpg/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,30 @@
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.util.Result;
import io.rpg.view.GameEndView;
import io.rpg.view.GameObjectView;
import io.rpg.view.InventoryPopup;
import io.rpg.view.LocationView;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;

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 @@ -36,10 +43,8 @@ public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Obs
private PlayerController playerController;
private Stage mainStage;


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

tagToLocationModelMap = new LinkedHashMap<>();
tagToLocationViewMap = new LinkedHashMap<>();
}
Expand Down Expand Up @@ -168,7 +173,13 @@ private void onAction(BattleAction action) {
}
popupController.openTextPopup(result.getMessage(), getWindowCenterX(), getWindowCenterY());
}


private void onAction(CollectAction action) {
popupController.openTextImagePopup("Picked up an item!", new Image("file:assets/key.png"), getWindowCenterX(), getWindowCenterY());
// objectView.setVisible(false);
playerController.getPlayer().getInventory().add(action.getOwner());
}

public Scene getView() {
return currentView;
}
Expand Down Expand Up @@ -215,6 +226,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)));
case U -> consumeAction(new GameEndAction("You have pressed the forbidden button"));
case E -> popupController.openInventoryPopup(playerController.getPlayer().getInventory(), getWindowCenterX(), getWindowCenterY());
}
}
// } else if (payload.getEventType() == KeyEvent.KEY_RELEASED) {
Expand All @@ -236,7 +248,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 Down
15 changes: 15 additions & 0 deletions src/main/java/io/rpg/controller/PopupController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.rpg.model.object.Question;
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.scene.image.Image;
Expand Down Expand Up @@ -50,6 +52,19 @@ public void openTextImagePopup(String text, Image image, int x, int y){
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) {
// InventoryPopup inventoryPopup=new InventoryPopup();
// final Stage popupStage = new Stage(StageStyle.TRANSPARENT);

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

popupStage.show();

popupStage.setX(x - popupScene.getWidth() / 2);
popupStage.setY(y - popupScene.getHeight() / 2);

}

public void openQuestionPopup(Question question, int x, int y, Runnable successCallback, Runnable failureCallback) {
QuestionPopup popupScene = new QuestionPopup(question);
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
16 changes: 16 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,16 @@
package io.rpg.model.actions;

import io.rpg.model.object.GameObject;

public class CollectAction implements Action {
private GameObject owner;

public void setOwner(GameObject owner) {
this.owner = owner;
}

public GameObject getOwner() {
return owner;
}
}

19 changes: 19 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,19 @@
package io.rpg.model.data;

import io.rpg.model.object.GameObject;

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

public class Inventory {

public List<GameObject> items;

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

public void add(GameObject object) {
items.add(object);
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/rpg/model/object/CollectibleGameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.jetbrains.annotations.NotNull;

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

@Override
Expand Down
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 @@ -130,4 +130,5 @@ public void onLeftClick() {
public int getStrength() {
return strength;
}

}
3 changes: 3 additions & 0 deletions src/main/java/io/rpg/model/object/InteractiveGameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
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);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/rpg/model/object/Player.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class Player extends GameObject {

private int strength;
Expand All @@ -15,6 +18,7 @@ public class Player extends GameObject {
private boolean downPressed;
private GameObjectView gameObjectView;
private int points;
Inventory inventory;


public Player(@NotNull String tag, @NotNull Position position, @NotNull String assetPath) {
Expand All @@ -25,6 +29,7 @@ public Player(@NotNull String tag, @NotNull Position position, @NotNull String a
this.upPressed = false;
this.downPressed = false;
this.strength = 0;
this.inventory = new Inventory();
}

public void updateStrength(int value) {
Expand Down Expand Up @@ -93,5 +98,9 @@ public void addPoints(int value) {

public int getStrength() {
return strength;
}

public Inventory getInventory() {
return inventory;
}
}
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 @@ -34,6 +34,9 @@ public static Action fromConfig(ActionConfigBundle config) {
case Battle -> {
return battleActionFromConfig(config);
}
case Collect -> {
return collectActionFromConfig(config);
}
default -> {
throw new IllegalArgumentException("Unexpected action type!");
}
Expand Down Expand Up @@ -66,4 +69,8 @@ private static ShowDescriptionAction showDescriptionActionFromConfig(ActionConfi
private static BattleAction battleActionFromConfig(ActionConfigBundle config) {
return new BattleAction(config.getRewardPoints());
}

private static CollectAction collectActionFromConfig(ActionConfigBundle config) {
return new CollectAction();
}
}
7 changes: 7 additions & 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 Expand Up @@ -56,9 +57,15 @@ public static GameObject fromConfig(GameObjectConfig config) {
if (onLeftClickAction instanceof BattleAction) {
((BattleAction) onLeftClickAction).setOpponent(gameObject);
}
if (onLeftClickAction instanceof CollectAction) {
((CollectAction) onLeftClickAction).setOwner(gameObject);
}
if (onRightClickAction instanceof BattleAction) {
((BattleAction) onRightClickAction).setOpponent(gameObject);
}
if (onRightClickAction instanceof CollectAction) {
((CollectAction) onRightClickAction).setOwner(gameObject);
}

// TODO: Create ActionFactory & inflate the actions
return gameObject;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/io/rpg/view/InventoryGameObjectView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.rpg.view;

import io.rpg.model.object.CollectibleGameObject;
import io.rpg.model.object.GameObject;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class InventoryGameObjectView extends ImageView {
GameObject collectibleGameObject;

public InventoryGameObjectView(GameObject collectibleGameObject) {
this.collectibleGameObject = collectibleGameObject;
Image image = new Image(GameObjectView.resolvePathToJFXFormat("assets/key.png"));
setImage(image);
}
}
78 changes: 78 additions & 0 deletions src/main/java/io/rpg/view/InventoryPopup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.rpg.view;

import io.rpg.model.data.Inventory;
import io.rpg.model.data.Position;
import io.rpg.model.object.Player;
import io.rpg.viewmodel.InventoryPopupViewModel;
import io.rpg.viewmodel.TextPopupViewModel;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class InventoryPopup extends Scene {
final int PADDING_LEFT = 25;
final int PADDING_TOP = 20;
@FXML
private Label label;

public InventoryPopup(Inventory inventory) {

super(new Group(), Color.TRANSPARENT);
Group group = new Group();
//TODO: load asset path from config
ImageView imageView = new ImageView(GameObjectView.resolvePathToJFXFormat("assets/popup-background.png"));
imageView.setX(0);
imageView.setY(0);
label = new Label();
label.setLayoutX(300);
label.setLayoutY(100);
label.setStyle("-fx-font-family: Monospaced; -fx-text-fill: white; -fx-font-weight: bold; -fx-font-size: " + 18);
group.getChildren().add(imageView);
group.getChildren().add(label);
for (int i = 0; i < inventory.items.size(); i++) {
// String assetPath=inventory.items.get(i).getAssetPath();
// to display objects in the menu
// wrapperClass to store information about object which we display
InventoryGameObjectView imageGameObjectView = new InventoryGameObjectView(inventory.items.get(i));

imageGameObjectView.setX(i * 50 + PADDING_LEFT);
imageGameObjectView.setY(0 + PADDING_TOP);
imageGameObjectView.setOnMouseEntered(event -> {
InventoryGameObjectView src = (InventoryGameObjectView) event.getSource();
// System.out.println("over the item "+src.collectibleGameObject.getAssetPath());
label.setText("stub description");
});

imageGameObjectView.setOnMouseExited(event -> {
label.setText("");
});

imageGameObjectView.setOnMouseClicked(event -> {

System.out.println("Object clicked");
InventoryGameObjectView src = (InventoryGameObjectView) event.getSource();
System.out.println();
});
group.getChildren().add(imageGameObjectView);

}
this.setRoot(group);
this.setFill(Color.TRANSPARENT);
}

}
2 changes: 1 addition & 1 deletion src/main/java/io/rpg/view/LocationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.rpg.model.data.KeyboardEvent;
import io.rpg.model.data.LocationModelStateChange;
import io.rpg.model.location.LocationModel;
import io.rpg.model.object.CollectibleGameObject;
import io.rpg.model.object.GameObject;
import io.rpg.viewmodel.LocationViewModel;
import io.rpg.config.model.LocationConfig;
Expand Down Expand Up @@ -101,7 +102,6 @@ public void onLocationModelStateChange(LocationModelStateChange event) {

List<GameObjectView> gameObjectViews = new ArrayList<>();


public void removeChild(GameObjectView view) {
viewModel.getForegroundPane().getChildren().remove(view);
}
Expand Down
Loading