diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/popups/BuyHouse.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/popups/BuyHouse.java index 2030f1a..67b36be 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/popups/BuyHouse.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/popups/BuyHouse.java @@ -6,8 +6,8 @@ import com.simsilica.lemur.Axis; import com.simsilica.lemur.Button; import com.simsilica.lemur.Container; import com.simsilica.lemur.Label; -import com.simsilica.lemur.ListBox; import com.simsilica.lemur.Selector; +import com.simsilica.lemur.TextField; import com.simsilica.lemur.component.QuadBackgroundComponent; import com.simsilica.lemur.component.SpringGridLayout; import com.simsilica.lemur.core.VersionedList; @@ -17,11 +17,12 @@ import pp.dialog.Dialog; import pp.monopoly.client.MonopolyApp; import pp.monopoly.client.gui.SettingsMenu; import pp.monopoly.game.server.Player; +import pp.monopoly.message.client.AlterProperty; import pp.monopoly.model.fields.BoardManager; import pp.monopoly.model.fields.BuildingProperty; -import pp.monopoly.model.fields.PropertyField; import pp.monopoly.notification.Sound; +import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -33,9 +34,12 @@ public class BuyHouse extends Dialog { private final MonopolyApp app; private final Container buyHouseContainer; private final Container backgroundContainer; + private TextField selectionDisplay; // TextField to display selections private VersionedReference> selectionRef; private Selector propertySelector; - private BuildingProperty selectedProperty; + private Set selectedProperties = new HashSet<>(); + + private Label cost = new Label("0", new ElementId("label-Text")); public BuyHouse(MonopolyApp app) { super(app.getDialogManager()); @@ -76,7 +80,7 @@ public class BuyHouse extends Dialog { downContainer.addChild(new Label("", new ElementId("label-Text"))); // Empty line downContainer.addChild(new Label("Kosten:", new ElementId("label-Text"))); // Label for cost - downContainer.addChild(new Label("Hier die tatsächlichen Kosten", new ElementId("label-Text"))); // Cost details + downContainer.addChild(cost); // Cost details downContainer.setBackground(new QuadBackgroundComponent(new ColorRGBA(0.4657f, 0.4735f, 0.4892f, 1.0f))); // Cancel button @@ -92,11 +96,9 @@ public class BuyHouse extends Dialog { confirmButton.setFontSize(32); confirmButton.addClickCommands(s -> ifTopDialog(() -> { app.getGameLogic().playSound(Sound.BUTTON); - if (selectedProperty != null) { - System.out.println("Confirmed property: " + selectedProperty.getName()); - // Send the "alter building" message to the server - // app.getGameLogic().sendMessage(new AlterBuildingMessage(selectedProperty.getId(), false)); // TODO - } + AlterProperty msg = new AlterProperty("BuyHouse"); + msg.setProperties(selectedProperties.stream().map(p -> app.getGameLogic().getBoardManager().getFieldByName(p).getId()).map(p -> (Integer) p).collect(Collectors.toSet())); + app.getGameLogic().send(msg); })); // Center the popup @@ -130,22 +132,20 @@ public class BuyHouse extends Dialog { // Populate the dropdown with property names for (BuildingProperty property : playerProperties) { - propertyOptions.add(property.getName() + " (ID: " + property.getId() + ")"); + propertyOptions.add(property.getName()); } propertySelector = new Selector<>(propertyOptions, "glass"); dropdownContainer.setBackground(new QuadBackgroundComponent(ColorRGBA.Orange)); dropdownContainer.addChild(propertySelector); - // Set initial selection - if (!propertyOptions.isEmpty()) { - onDropdownSelectionChanged(propertyOptions.get(0), playerProperties); - } - // Track selection changes selectionRef = propertySelector.getSelectionModel().createReference(); - + // Initialize the selection display here + selectionDisplay = new TextField(""); // Create TextField for displaying selections + selectionDisplay.setPreferredSize(new Vector3f(300, 30, 0)); + dropdownContainer.addChild(selectionDisplay); // Add it to the dropdown container return dropdownContainer; } @@ -156,36 +156,43 @@ public class BuyHouse extends Dialog { * @return List of BuildingProperty objects owned by the player. */ private List getPlayerProperties() { - Player currentPlayer = app.getGameLogic().getPlayerHandler().getPlayerById(app.getId()); + Player self = app.getGameLogic().getPlayerHandler().getPlayerById(app.getId()); BoardManager boardManager = app.getGameLogic().getBoardManager(); - return boardManager.getPropertyFields(currentPlayer.getProperties()).stream() + return boardManager.getPropertyFields(self.getProperties()).stream() .filter(property -> property instanceof BuildingProperty) .map(property -> (BuildingProperty) property) .collect(Collectors.toList()); } + @Override + public void update(float delta) { + if(selectionRef.update()) { + onDropdownSelectionChanged(propertySelector); + } + } + /** * Handles property selection changes. */ - private void onDropdownSelectionChanged(String selected, List playerProperties) { + private void onDropdownSelectionChanged(Selector playerProperties) { + String selected = playerProperties.getSelectedItem(); app.getGameLogic().playSound(Sound.BUTTON); - - // Extract the ID from the selected string - int idStart = selected.indexOf("(ID: ") + 5; - int idEnd = selected.indexOf(")", idStart); - String idStr = selected.substring(idStart, idEnd); - int propertyId = Integer.parseInt(idStr); - - // Find the selected property - selectedProperty = playerProperties.stream() - .filter(property -> property.getId() == propertyId) - .findFirst() - .orElse(null); - - if (selectedProperty != null) { - System.out.println("Selected property: " + selectedProperty.getName()); + if (selectedProperties.contains(selected)) { + selectedProperties.remove(selected); + } else { + selectedProperties.add(selected); } + + int cost = 0; + for (String s : selectedProperties) { + cost += ((BuildingProperty) app.getGameLogic().getBoardManager().getFieldByName(s)).getHousePrice(); + } + + String display = String.join(" | ", selectedProperties); + selectionDisplay.setText(display); + + this.cost.setText(cost+""); } @Override