dynamically set players in ChoosePatner

This commit is contained in:
Johannes Schmelz 2024-11-26 20:00:50 +01:00
parent 174958c6b1
commit 9107a08011
2 changed files with 134 additions and 14 deletions

View File

@ -1,5 +1,9 @@
package pp.monopoly.client.gui; 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.material.Material;
import com.jme3.math.ColorRGBA; import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f; 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.QuadBackgroundComponent;
import com.simsilica.lemur.component.SpringGridLayout; import com.simsilica.lemur.component.SpringGridLayout;
import com.simsilica.lemur.core.VersionedList; import com.simsilica.lemur.core.VersionedList;
import com.simsilica.lemur.core.VersionedReference;
import com.simsilica.lemur.style.ElementId; import com.simsilica.lemur.style.ElementId;
import pp.dialog.Dialog; import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp; import pp.monopoly.client.MonopolyApp;
import pp.monopoly.game.server.Player;
import pp.monopoly.model.TradeHandler;
import pp.monopoly.notification.Sound; import pp.monopoly.notification.Sound;
public class ChoosePartner extends Dialog { public class ChoosePartner extends Dialog {
@ -28,6 +35,7 @@ public class ChoosePartner extends Dialog {
private Container lowerLeftMenu; private Container lowerLeftMenu;
private Container lowerRightMenu; private Container lowerRightMenu;
private Geometry background; private Geometry background;
private TradeHandler tradeHandler;
QuadBackgroundComponent translucentWhiteBackground = QuadBackgroundComponent translucentWhiteBackground =
new QuadBackgroundComponent(new ColorRGBA(1.0f, 1.0f, 1.0f, 0.5f)); 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) { public ChoosePartner(MonopolyApp app) {
super(app.getDialogManager()); super(app.getDialogManager());
this.app = app; this.app = app;
tradeHandler = new TradeHandler(app.getGameLogic().getPlayerHandler().getPlayerById(app.getId()));
// Background Image // Background Image
addBackgroundImage(); addBackgroundImage();
@ -60,6 +68,8 @@ public class ChoosePartner extends Dialog {
// Add buttons // Add buttons
mainContainer.addChild(createButtonContainer()); mainContainer.addChild(createButtonContainer());
addSelectionActionListener(playerSelector, this::onDropdownSelectionChanged);
// Attach main container to GUI node // Attach main container to GUI node
app.getGuiNode().attachChild(mainContainer); app.getGuiNode().attachChild(mainContainer);
mainContainer.setLocalTranslation( mainContainer.setLocalTranslation(
@ -80,10 +90,12 @@ public class ChoosePartner extends Dialog {
dropdownContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black))); dropdownContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(ColorRGBA.Black)));
VersionedList<String> playerOptions = new VersionedList<>(); VersionedList<String> playerOptions = new VersionedList<>();
playerOptions.add("Spieler 1");
playerOptions.add("Spieler 2"); for (Player player : app.getGameLogic().getPlayerHandler().getPlayers()) {
playerOptions.add("Spieler 3"); if (player.getId() != app.getId()) {
playerOptions.add("Spieler 4"); playerOptions.add(player.getName() + " (ID: "+player.getId()+")");
}
}
playerSelector = new Selector<>(playerOptions, "glass"); playerSelector = new Selector<>(playerOptions, "glass");
dropdownContainer.addChild(playerSelector); dropdownContainer.addChild(playerSelector);
@ -126,8 +138,6 @@ public class ChoosePartner extends Dialog {
confirmButton.setFontSize(30); confirmButton.setFontSize(30);
confirmButton.addClickCommands(s -> ifTopDialog(() -> { confirmButton.addClickCommands(s -> ifTopDialog(() -> {
app.getGameLogic().playSound(Sound.BUTTON); app.getGameLogic().playSound(Sound.BUTTON);
String selectedPlayer = playerSelector.getSelectedItem();
System.out.println("Selected player: " + selectedPlayer);
close(); close();
new TradeMenu(app).open(); new TradeMenu(app).open();
})); }));
@ -182,6 +192,72 @@ public class ChoosePartner extends Dialog {
app.getGuiNode().detachChild(background); app.getGuiNode().detachChild(background);
super.close(); super.close();
} }
/**
* Adds a custom action listener to the Selector.
*/
private void addSelectionActionListener(Selector<String> selector, SelectionActionListener<String> listener) {
VersionedReference<Set<Integer>> 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<T> {
void onSelectionChanged(T selection);
}
} }

View File

@ -12,13 +12,13 @@ import java.util.List;
public class TradeHandler { public class TradeHandler {
private final Player sender; private final Player sender;
private final Player receiver; private Player receiver;
private final int offeredAmount; private int offeredAmount;
private final List<PropertyField> offeredProperties; private List<PropertyField> offeredProperties;
private final int offeredJailCards; private int offeredJailCards;
private final int requestedAmount; private int requestedAmount;
private final List<PropertyField> requestedProperties; private List<PropertyField> requestedProperties;
private final int requestedJailCards; private int requestedJailCards;
private Boolean status = null; private Boolean status = null;
/** /**
@ -45,6 +45,22 @@ public class TradeHandler {
this.requestedJailCards = requestedJailCards; 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() { public int getOfferedAmount() {
return offeredAmount; return offeredAmount;
} }
@ -209,4 +225,32 @@ public class TradeHandler {
} }
System.out.println(numCards + " jail card(s) transferred from " + from.getName() + " to " + to.getName()); 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<PropertyField> 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<PropertyField> requestedProperties) {
this.requestedProperties = requestedProperties;
}
} }