-
Notifications
You must be signed in to change notification settings - Fork 0
@co012/rpg 108 teleport action #31
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
192ae89
chore: pass stage to Controller
co012 ca34ce7
feat: change location
co012 a206683
chore: create PlayerController
co012 72ddb25
chore: made generic Action
co012 13781e3
chore: refactor game start
co012 8ddda7d
chore: teleport player
co012 350e775
chore: fix for concurrent modification exception
co012 734ac91
chore: change player position on teleport
co012 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
configurations/demo-config-1/locations/location-2/location-2.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "tag": "location-2", | ||
| "objects": [ | ||
| { "tag": "object-1", "position": { "row": 1, "col": 8 }, "type": "collectible", "assetPath": "assets/diamond.png" }, | ||
| { "tag": "object-2", "position": { "row": 6, "col": 3 }, "type": "collectible", "assetPath": "assets/key.png" }, | ||
| { "tag": "object-3", "position": { "row": 1, "col": 1 }, "type": "dialog", "assetPath": "assets/zombie.png" }, | ||
| { "tag": "object-4", "position": { "row": 3, "col": 2 }, "type": "dialog", "assetPath": "assets/zombie.png" }, | ||
| { "tag": "object-5", "position": { "row": 8, "col": 8 }, "type": "dialog", "assetPath": "assets/chest.png" } | ||
| ], | ||
| "backgroundPath": "assets/map10x10.png" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| package io.rpg.controller; | ||
|
|
||
| import io.rpg.model.actions.Action; | ||
| import io.rpg.model.actions.LocationChangeAction; | ||
| import io.rpg.model.data.KeyboardEvent; | ||
| import io.rpg.model.data.MouseClickedEvent; | ||
| import io.rpg.model.data.Position; | ||
| import io.rpg.model.data.Vector; | ||
| import io.rpg.model.location.LocationModel; | ||
| import io.rpg.model.object.CollectibleGameObject; | ||
|
|
@@ -11,8 +14,11 @@ | |
| import io.rpg.util.Result; | ||
| import io.rpg.view.GameObjectView; | ||
| import io.rpg.view.LocationView; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import javafx.scene.Scene; | ||
| import javafx.scene.input.KeyEvent; | ||
| import javafx.stage.Stage; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
@@ -27,6 +33,8 @@ public class Controller implements KeyboardEvent.Observer, MouseClickedEvent.Obs | |
| private LinkedHashMap<String, LocationView> tagToLocationViewMap; | ||
| private Logger logger; | ||
| private final PopupController popupController = new PopupController(); | ||
| private PlayerController playerController; | ||
| private Stage mainStage; | ||
|
|
||
|
|
||
| public Controller() { | ||
|
|
@@ -49,6 +57,10 @@ public Controller(LinkedHashMap<String, LocationModel> tagToLocationModelMap, | |
| this.currentView = this.tagToLocationViewMap.get(rootTag); | ||
| } | ||
|
|
||
| public void setMainStage(Stage mainStage) { | ||
| this.mainStage = mainStage; | ||
| } | ||
|
|
||
| public void setModel(@NotNull LocationModel model) { | ||
| this.tagToLocationModelMap.put(model.getTag(), model); | ||
| currentModel = model; | ||
|
|
@@ -58,10 +70,38 @@ public void setView(Scene currentView) { | |
| this.currentView = currentView; | ||
| } | ||
|
|
||
| public void registerToViews(List<GameObjectView> views) { | ||
| for (GameObjectView view : views) { | ||
| view.addOnClickedObserver(this); | ||
| public void onAction(Action action) { | ||
| Class<?>[] args = {action.getClass()}; | ||
| Method onSpecificAction; | ||
|
|
||
| try { | ||
| onSpecificAction = this.getClass().getDeclaredMethod("onAction", args); | ||
| } catch (NoSuchMethodException e) { | ||
| logger.error("onAction for " + action.getClass().getSimpleName() + "is not implemented"); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| onSpecificAction.invoke(this, action); | ||
| } catch (IllegalAccessException | InvocationTargetException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private void onAction(LocationChangeAction action) { | ||
| if (!this.tagToLocationModelMap.containsKey(action.destinationLocationTag)) { | ||
| logger.error("Unknown location tag"); | ||
| return; | ||
| } | ||
|
|
||
| LocationView nextView = this.tagToLocationViewMap.get(action.destinationLocationTag); | ||
| LocationModel nextModel = this.tagToLocationModelMap.get(action.destinationLocationTag); | ||
|
|
||
| playerController.teleportTo(nextModel, nextView, action.playerPosition); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could also check here if the location is unlocked, for example whether the player has enough points or a specific item in his equipment |
||
|
|
||
| this.currentModel = nextModel; | ||
| this.currentView = nextView; | ||
| mainStage.setScene(nextView); | ||
| } | ||
|
|
||
| public Scene getView() { | ||
|
|
@@ -91,10 +131,6 @@ else if (tagToLocationViewMap.size() == 0) | |
| return Result.error(new Exception("Empty tag to location view map!")); | ||
| else if (tagToLocationViewMap.size() != tagToLocationModelMap.size()) | ||
| return Result.error(new Exception("Mismatched sizes of maps!")); | ||
| else if (currentView == null) | ||
| return Result.error(new Exception("No current view set!")); | ||
| else if (currentModel == null) | ||
| return Result.error(new Exception("No current model set!")); | ||
| else | ||
| return Result.ok(this); | ||
| } | ||
|
|
@@ -107,23 +143,16 @@ public void onKeyboardEvent(KeyboardEvent event) { | |
|
|
||
| KeyEvent payload = event.payload(); | ||
|
|
||
| if (payload.getEventType() == KeyEvent.KEY_PRESSED){ | ||
| if (payload.getEventType() == KeyEvent.KEY_PRESSED) { | ||
| switch (payload.getCode()) { | ||
| case F -> popupController.openPointsPopup(5, getWindowCenterX(), getWindowCenterY()); | ||
| case G -> popupController.openTextPopup("Hello!", getWindowCenterX(), getWindowCenterY()); | ||
| case A -> currentModel.getPlayer().setLeftPressed(true); | ||
| case D -> currentModel.getPlayer().setRightPressed(true); | ||
| case S -> currentModel.getPlayer().setDownPressed(true); | ||
| case W -> currentModel.getPlayer().setUpPressed(true); | ||
| } | ||
| } else if (payload.getEventType() == KeyEvent.KEY_RELEASED) { | ||
| switch (payload.getCode()) { | ||
| case A -> currentModel.getPlayer().setLeftPressed(false); | ||
| case D -> currentModel.getPlayer().setRightPressed(false); | ||
| case S -> currentModel.getPlayer().setDownPressed(false); | ||
| case W -> currentModel.getPlayer().setUpPressed(false); | ||
| case L -> onAction((Action) new LocationChangeAction("location-2", new Position(1, 2))); | ||
| } | ||
| } | ||
| // } else if (payload.getEventType() == KeyEvent.KEY_RELEASED) { | ||
| // | ||
| // } | ||
| } | ||
|
|
||
| private int getWindowCenterX() { | ||
|
|
@@ -153,58 +182,14 @@ public void onMouseClickedEvent(MouseClickedEvent event) { | |
| logger.info("Controller notified on click from " + event.source()); | ||
| } | ||
|
|
||
| private void setPlayer(Player gameObject) { | ||
| currentModel.setPlayer(gameObject); | ||
| } | ||
|
|
||
| // TODO: temporary solution | ||
| public void setPlayerView(GameObjectView playerView) { | ||
| ((LocationView) currentView).getViewModel().addChild(playerView); | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private final Controller controller; | ||
| private boolean isViewSet = false; | ||
| private boolean isModelSet = false; | ||
|
|
||
| private Player player; | ||
|
|
||
| public Builder() { | ||
| controller = new Controller(); | ||
| } | ||
|
|
||
| public Builder setTagToLocationModelMap(LinkedHashMap<String, LocationModel> tagToLocationModelMap) { | ||
| controller.setTagToLocationModelMap(tagToLocationModelMap); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setTagToLocationViewMap(LinkedHashMap<String, LocationView> tagToLocationViewMap) { | ||
| controller.setTagToLocationViewMap(tagToLocationViewMap); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setModel(@NotNull LocationModel model) { | ||
| if (!isModelSet) { | ||
| isModelSet = true; | ||
| controller.setModel(model); | ||
| return this; | ||
| } else { | ||
| throw new IllegalStateException("Attempt to set model for the second time!"); | ||
| } | ||
| } | ||
|
|
||
| public Builder setView(Scene currentView) { | ||
| if (!isViewSet) { | ||
| isViewSet = true; | ||
| controller.setView(currentView); | ||
| return this; | ||
| } else { | ||
| throw new IllegalStateException("Attempt to set view for the second time!"); | ||
| } | ||
| } | ||
|
|
||
| public Controller build() { | ||
| controller.setPlayer(player); | ||
| Result<Controller, Exception> validationResult = controller.validate(); | ||
| if (validationResult.isError()) { | ||
| throw new IllegalStateException(validationResult.getErrorValue()); | ||
|
|
@@ -231,9 +216,8 @@ public Builder addModelForTag(String tag, LocationModel model) { | |
| return this; | ||
| } | ||
|
|
||
| public Builder setPlayer(Player gameObject) { | ||
| player = gameObject; | ||
| return this; | ||
| public void setPlayerController(PlayerController playerController) { | ||
| controller.playerController = playerController; | ||
| } | ||
| } | ||
| public LocationModel getCurrentModel() { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some objects don't fit on the map. map size is currently dependent on the asset dimensions, it should probably be specified in config instead, but that's a problem for another task ;)