From 9107a08011132411f9ef68daa5141b83dd86dd80 Mon Sep 17 00:00:00 2001 From: Johannes Schmelz Date: Tue, 26 Nov 2024 20:00:50 +0100 Subject: [PATCH] dynamically set players in ChoosePatner --- .../pp/monopoly/client/gui/ChoosePartner.java | 90 +++++++++++++++++-- .../java/pp/monopoly/model/TradeHandler.java | 58 ++++++++++-- 2 files changed, 134 insertions(+), 14 deletions(-) diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/ChoosePartner.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/ChoosePartner.java index 09b5774..4d89b01 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/ChoosePartner.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/ChoosePartner.java @@ -1,5 +1,9 @@ package pp.monopoly.client.gui; +import java.util.Set; + +import com.jme3.app.Application; +import com.jme3.app.state.BaseAppState; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; @@ -14,9 +18,12 @@ import com.simsilica.lemur.Selector; import com.simsilica.lemur.component.QuadBackgroundComponent; import com.simsilica.lemur.component.SpringGridLayout; import com.simsilica.lemur.core.VersionedList; +import com.simsilica.lemur.core.VersionedReference; import com.simsilica.lemur.style.ElementId; import pp.dialog.Dialog; import pp.monopoly.client.MonopolyApp; +import pp.monopoly.game.server.Player; +import pp.monopoly.model.TradeHandler; import pp.monopoly.notification.Sound; public class ChoosePartner extends Dialog { @@ -28,6 +35,7 @@ public class ChoosePartner extends Dialog { private Container lowerLeftMenu; private Container lowerRightMenu; private Geometry background; + private TradeHandler tradeHandler; QuadBackgroundComponent translucentWhiteBackground = new QuadBackgroundComponent(new ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f)); @@ -39,7 +47,7 @@ public class ChoosePartner extends Dialog { public ChoosePartner(MonopolyApp app) { super(app.getDialogManager()); this.app = app; - + tradeHandler = new TradeHandler(app.getGameLogic().getPlayerHandler().getPlayerById(app.getId())); // Background Image addBackgroundImage(); @@ -60,6 +68,8 @@ public class ChoosePartner extends Dialog { // Add buttons mainContainer.addChild(createButtonContainer()); + addSelectionActionListener(playerSelector, this::onDropdownSelectionChanged); + // Attach main container to GUI node app.getGuiNode().attachChild(mainContainer); mainContainer.setLocalTranslation( @@ -80,10 +90,12 @@ public class ChoosePartner extends Dialog { dropdownContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black))); VersionedList playerOptions = new VersionedList<>(); - playerOptions.add("Spieler 1"); - playerOptions.add("Spieler 2"); - playerOptions.add("Spieler 3"); - playerOptions.add("Spieler 4"); + + for (Player player : app.getGameLogic().getPlayerHandler().getPlayers()) { + if (player.getId() != app.getId()) { + playerOptions.add(player.getName() + " (ID: "+player.getId()+")"); + } + } playerSelector = new Selector<>(playerOptions, "glass"); dropdownContainer.addChild(playerSelector); @@ -126,8 +138,6 @@ public class ChoosePartner extends Dialog { confirmButton.setFontSize(30); confirmButton.addClickCommands(s -> ifTopDialog(() -> { app.getGameLogic().playSound(Sound.BUTTON); - String selectedPlayer = playerSelector.getSelectedItem(); - System.out.println("Selected player: " + selectedPlayer); close(); new TradeMenu(app).open(); })); @@ -182,6 +192,72 @@ public class ChoosePartner extends Dialog { app.getGuiNode().detachChild(background); super.close(); } + + /** + * Adds a custom action listener to the Selector. + */ + private void addSelectionActionListener(Selector selector, SelectionActionListener listener) { + VersionedReference> selectionRef = selector.getSelectionModel().createReference(); + + app.getStateManager().attach(new BaseAppState() { + @Override + public void update(float tpf) { + if (selectionRef.update()) { + String selected = selectionRef.get().toString(); + System.out.println(selected); + listener.onSelectionChanged(selected); + } + } + + @Override + protected void initialize(Application app) { + } + + @Override + protected void cleanup(Application app) { + } + + @Override + protected void onEnable() { + + } + + @Override + protected void onDisable() { + + } + }); + } + + /** + * Callback for when the dropdown selection changes. + */ + private void onDropdownSelectionChanged(String selected) { + System.out.println("Selected: " + selected); + app.getGameLogic().playSound(Sound.BUTTON); + int idStart = selected.indexOf("(ID: ") + 5; // Find start of the ID + int idEnd = selected.indexOf(")", idStart); // Find end of the ID + String idStr = selected.substring(idStart, idEnd); // Extract the ID as a string + int playerId = Integer.parseInt(idStr); // Convert the ID to an integer + + // Find the player by ID + Player selectedPlayer = app.getGameLogic().getPlayerHandler().getPlayerById(playerId); + + if (selectedPlayer != null) { + tradeHandler.setReceiver(selectedPlayer); // Set the receiver in TradeHandler + System.out.println("Receiver set to: " + selectedPlayer.getName() + " (ID: " + selectedPlayer.getId() + ")"); + } else { + System.err.println("Player with ID " + playerId + " not found."); + } + } + + /** + * Functional interface for a selection action listener. + */ + @FunctionalInterface + private interface SelectionActionListener { + void onSelectionChanged(T selection); + } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/TradeHandler.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/TradeHandler.java index 6f63d8e..6e6a89e 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/model/TradeHandler.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/model/TradeHandler.java @@ -12,13 +12,13 @@ import java.util.List; public class TradeHandler { private final Player sender; - private final Player receiver; - private final int offeredAmount; - private final List offeredProperties; - private final int offeredJailCards; - private final int requestedAmount; - private final List requestedProperties; - private final int requestedJailCards; + private Player receiver; + private int offeredAmount; + private List offeredProperties; + private int offeredJailCards; + private int requestedAmount; + private List requestedProperties; + private int requestedJailCards; private Boolean status = null; /** @@ -45,6 +45,22 @@ public class TradeHandler { this.requestedJailCards = requestedJailCards; } + /** + * Constructs a TradeHandler for a single trade instance. + * + * @param sender the Player initiating the trade + * @param receiver the Player receiving the trade offer + * @param offeredAmount the amount of money offered by the sender + * @param offeredProperties the properties offered by the sender + * @param offeredJailCards the jail cards offered by the sender + * @param requestedAmount the amount of money requested from the receiver + * @param requestedProperties the properties requested from the receiver + * @param requestedJailCards the jail cards requested from the receiver + */ + public TradeHandler(Player sender) { + this.sender = sender; + } + public int getOfferedAmount() { return offeredAmount; } @@ -209,4 +225,32 @@ public class TradeHandler { } System.out.println(numCards + " jail card(s) transferred from " + from.getName() + " to " + to.getName()); } + + public void setOfferedAmount(int offeredAmount) { + this.offeredAmount = offeredAmount; + } + + public void setOfferedJailCards(int offeredJailCards) { + this.offeredJailCards = offeredJailCards; + } + + public void setOfferedProperties(List offeredProperties) { + this.offeredProperties = offeredProperties; + } + + public void setReceiver(Player receiver) { + this.receiver = receiver; + } + + public void setRequestedAmount(int requestedAmount) { + this.requestedAmount = requestedAmount; + } + + public void setRequestedJailCards(int requestedJailCards) { + this.requestedJailCards = requestedJailCards; + } + + public void setRequestedProperties(List requestedProperties) { + this.requestedProperties = requestedProperties; + } }