lobby uebergibt farbe und startgeld

This commit is contained in:
Yvonne Schmidt 2024-11-24 18:07:39 +01:00
parent 437114704a
commit 9a6ce27fe1
3 changed files with 98 additions and 13 deletions

View File

@ -23,6 +23,10 @@ import com.simsilica.lemur.core.VersionedList;
import com.simsilica.lemur.core.VersionedReference;
import com.simsilica.lemur.style.ElementId;
import pp.monopoly.client.MonopolyApp;
import pp.monopoly.game.client.ClientGameLogic;
import pp.monopoly.game.server.Player;
import pp.monopoly.game.server.PlayerColor;
import pp.monopoly.game.server.PlayerHandler;
import java.util.Set;
@ -34,10 +38,18 @@ public class LobbyMenu {
private Geometry circle;
private Container lowerLeftMenu;
private Container lowerRightMenu;
private ColorRGBA playerColor= ColorRGBA.Gray;
private PlayerHandler playerHandler; // Reference to PlayerHandler
private TextField startingCapital; // Reference to the starting capital input field
public LobbyMenu(MonopolyApp app) {
this.app = app;
this.playerHandler = ClientGameLogic.getPlayerHandler(); // Initialize PlayerHandler
int playerID = app.getNetworkSupport().getId(); // Retrieve the player ID dynamically
assignPlayerColor(playerID);
// Entfernt das CreateGameMenu (inklusive Hintergrund)
app.getGuiNode().detachAllChildren();
@ -102,6 +114,7 @@ public class LobbyMenu {
// Figur Dropdown
Container figureDropdownContainer = dropdownContainer.addChild(new Container(new SpringGridLayout(Axis.Y, Axis.X)));
figureDropdownContainer.setPreferredSize(new Vector3f(150, 80, 0));
figureDropdownContainer.addChild(new Label("Figur:"));
figureDropdownContainer.setBackground(null);
@ -115,7 +128,11 @@ public class LobbyMenu {
Selector<String> figureDropdown = new Selector<>(figures, "glass");
figureDropdown.setBackground(new QuadBackgroundComponent(ColorRGBA.DarkGray));
figureDropdown.setPreferredSize(new Vector3f(100, 20, 0));
figureDropdown.setPreferredSize(new Vector3f(150, 140, 0));
Vector3f dimens = figureDropdownContainer.getPreferredSize();
Vector3f dimens2 = figureDropdown.getPopupContainer().getPreferredSize();
dimens2.setX( dimens.getX() );
figureDropdown.getPopupContainer().setPreferredSize( dimens2 );
figureDropdownContainer.addChild(figureDropdown);
addSelectionActionListener(figureDropdown, this::onDropdownSelectionChanged);
@ -143,6 +160,7 @@ public class LobbyMenu {
readyButton.setFontSize(18); // Adjust font size
readyButton.setBackground(new QuadBackgroundComponent(ColorRGBA.Green)); // Add color to match the style
readyButton.addClickCommands(source -> toggleReady(null));// Add functionality
readyButton.addClickCommands(source -> applyStartingCapital(playerID));
lowerRightMenu.addChild(readyButton);
// Position the container near the bottom-right corner
@ -150,7 +168,7 @@ public class LobbyMenu {
app.getGuiNode().attachChild(lowerRightMenu);
// Add a colored circle between the input field and the dropdown menu
circle = createCircle( ColorRGBA.Red); // 50 is the diameter, Red is the color
circle = createCircle(); // 50 is the diameter, Red is the color
circle.setLocalTranslation(new Vector3f(
(app.getCamera().getWidth()) / 2, // Center horizontally
(app.getCamera().getHeight() / 2) - 90, // Adjust Y position
@ -167,7 +185,29 @@ public class LobbyMenu {
app.getGuiNode().attachChild(menuContainer);
}
/**
* Apply the starting capital only if the current player is the host.
*/
private void applyStartingCapital(int playerID) {
Player currentPlayer = playerHandler.getPlayerById(playerID);
// Check if the current player is the host
if (currentPlayer.equals(playerHandler.getHostPlayer())) {
try {
// Parse and validate starting capital
int startBalance = Integer.parseInt(startingCapital.getText().replaceAll("[^\\d]", ""));
if (startBalance < 0) throw new NumberFormatException("Starting capital must be positive.");
// Apply the starting balance to all players
playerHandler.setStartBalance(startBalance);
System.out.println("Starting balance set to: " + startBalance);
} catch (NumberFormatException e) {
System.err.println("Invalid starting capital: " + e.getMessage());
}
} else {
System.out.println("Only the host can set the starting balance.");
}
}
/**
* Lädt das Hintergrundbild und fügt es als geometrische Ebene hinzu.
@ -184,18 +224,57 @@ public class LobbyMenu {
app.getGuiNode().attachChild(background);
}
private Geometry createCircle(ColorRGBA color) {
private Geometry createCircle() {
Sphere sphere = new Sphere(90,90,60.0f);
Geometry circleGeometry = new Geometry("Circle", sphere);
// Create a material with a solid color
Material material = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
material.setColor("Color", color); // Set the desired color
material.setColor("Color", playerColor); // Set the desired color
circleGeometry.setMaterial(material);
return circleGeometry;
}
public void setPlayerColor(ColorRGBA newColor) {
this.playerColor = newColor;
// Update the circle's color
if (circle != null) {
Material material = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
material.setColor("Color", playerColor);
circle.setMaterial(material);
}
}
/**
* Assigns a color to the player based on their ID.
*
* @param playerID the player's ID
*/
private void assignPlayerColor(int playerID) {
switch (playerID) {
case 0:
playerColor = PlayerColor.RED.getColor();
break;
case 1:
playerColor = PlayerColor.GREEN_LIGHT.getColor();
break;
case 2:
playerColor = PlayerColor.BLUE.getColor();
break;
case 3:
playerColor = PlayerColor.PINK.getColor();
break;
case 4:
playerColor = PlayerColor.GREEN_DARK.getColor();
break;
case 5:
playerColor = PlayerColor.YELLOW.getColor();
break;
default:
playerColor = ColorRGBA.White; // Default color if ID is unknown
}
}
/**
* Schaltet den "Bereit"-Status um.
@ -260,16 +339,22 @@ public class LobbyMenu {
System.out.println("Selected: " + selected);
switch (selected) {
case "[0]":
System.out.println("Alpha selected");
System.out.println("Laptop selected");
break;
case "[1]":
System.out.println("Beta selected");
System.out.println("Flugzeug selected");
break;
case "[2]":
System.out.println("Gamma selected");
System.out.println("Jägermeister selected");
break;
case "[3]":
goBackToCreateGame();
System.out.println("Katze selected");
break;
case "[4]":
System.out.println("OOP selected");
break;
case "[5]":
System.out.println("Handyholster selected");
break;
default:
System.out.println("Unknown selection");

View File

@ -51,7 +51,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
/** The current state of the client game logic. */
private ClientState state = new LobbyState(this);
private PlayerHandler playerHandler;
private static PlayerHandler playerHandler;
/**
* Constructs a ClientGameLogic with the specified sender object.
@ -83,7 +83,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
state.entry();
}
public PlayerHandler getPlayerHandler() {
public static PlayerHandler getPlayerHandler() {
return playerHandler;
}

View File

@ -157,7 +157,7 @@ public class PlayerHandler {
* @param id the id to be searched for
* @return the player with the required id
*/
Player getPlayerById(int id) {
public Player getPlayerById(int id) {
for (Player player : players) {
if (player.getId() == id) return player;
}
@ -176,7 +176,7 @@ public class PlayerHandler {
players.get(0).setActive();
}
void setStartBalance(int amount) {
public void setStartBalance(int amount) {
for (Player player : players) {
player.setAccountBalance(amount);
}