Compare commits

...

2 Commits

Author SHA1 Message Date
Yvonne Schmidt
9a6ce27fe1 lobby uebergibt farbe und startgeld 2024-11-24 18:07:39 +01:00
Johannes Schmelz
437114704a get client id 2024-11-24 17:20:35 +01:00
4 changed files with 104 additions and 14 deletions

View File

@ -41,6 +41,11 @@ public class NetworkSupport implements MessageListener<Client>, ClientStateListe
return app; return app;
} }
public int getId() {
if (client == null) return 0;
return client.getId();
}
/** /**
* Checks if there is a connection to the game server. * Checks if there is a connection to the game server.
* *
@ -80,7 +85,7 @@ public class NetworkSupport implements MessageListener<Client>, ClientStateListe
* @param port The server's port. * @param port The server's port.
* @throws IOException If an I/O error occurs when creating the client. * @throws IOException If an I/O error occurs when creating the client.
*/ */
void initNetwork(String host, int port) throws IOException { public void initNetwork(String host, int port) throws IOException {
if (client != null) { if (client != null) {
throw new IllegalStateException("Already connected to the game server."); throw new IllegalStateException("Already connected to the game server.");
} }

View File

@ -23,6 +23,10 @@ import com.simsilica.lemur.core.VersionedList;
import com.simsilica.lemur.core.VersionedReference; import com.simsilica.lemur.core.VersionedReference;
import com.simsilica.lemur.style.ElementId; import com.simsilica.lemur.style.ElementId;
import pp.monopoly.client.MonopolyApp; 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; import java.util.Set;
@ -34,10 +38,18 @@ public class LobbyMenu {
private Geometry circle; private Geometry circle;
private Container lowerLeftMenu; private Container lowerLeftMenu;
private Container lowerRightMenu; 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) { public LobbyMenu(MonopolyApp app) {
this.app = 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) // Entfernt das CreateGameMenu (inklusive Hintergrund)
app.getGuiNode().detachAllChildren(); app.getGuiNode().detachAllChildren();
@ -102,6 +114,7 @@ public class LobbyMenu {
// Figur Dropdown // Figur Dropdown
Container figureDropdownContainer = dropdownContainer.addChild(new Container(new SpringGridLayout(Axis.Y, Axis.X))); 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.addChild(new Label("Figur:"));
figureDropdownContainer.setBackground(null); figureDropdownContainer.setBackground(null);
@ -115,7 +128,11 @@ public class LobbyMenu {
Selector<String> figureDropdown = new Selector<>(figures, "glass"); Selector<String> figureDropdown = new Selector<>(figures, "glass");
figureDropdown.setBackground(new QuadBackgroundComponent(ColorRGBA.DarkGray)); 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); figureDropdownContainer.addChild(figureDropdown);
addSelectionActionListener(figureDropdown, this::onDropdownSelectionChanged); addSelectionActionListener(figureDropdown, this::onDropdownSelectionChanged);
@ -142,7 +159,8 @@ public class LobbyMenu {
readyButton.setPreferredSize(new Vector3f(200, 60, 0)); // Set size to match the appearance in the image readyButton.setPreferredSize(new Vector3f(200, 60, 0)); // Set size to match the appearance in the image
readyButton.setFontSize(18); // Adjust font size readyButton.setFontSize(18); // Adjust font size
readyButton.setBackground(new QuadBackgroundComponent(ColorRGBA.Green)); // Add color to match the style readyButton.setBackground(new QuadBackgroundComponent(ColorRGBA.Green)); // Add color to match the style
readyButton.addClickCommands(source -> toggleReady(null)); // Add functionality readyButton.addClickCommands(source -> toggleReady(null));// Add functionality
readyButton.addClickCommands(source -> applyStartingCapital(playerID));
lowerRightMenu.addChild(readyButton); lowerRightMenu.addChild(readyButton);
// Position the container near the bottom-right corner // Position the container near the bottom-right corner
@ -150,7 +168,7 @@ public class LobbyMenu {
app.getGuiNode().attachChild(lowerRightMenu); app.getGuiNode().attachChild(lowerRightMenu);
// Add a colored circle between the input field and the dropdown menu // 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( circle.setLocalTranslation(new Vector3f(
(app.getCamera().getWidth()) / 2, // Center horizontally (app.getCamera().getWidth()) / 2, // Center horizontally
(app.getCamera().getHeight() / 2) - 90, // Adjust Y position (app.getCamera().getHeight() / 2) - 90, // Adjust Y position
@ -167,7 +185,29 @@ public class LobbyMenu {
app.getGuiNode().attachChild(menuContainer); 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. * Lädt das Hintergrundbild und fügt es als geometrische Ebene hinzu.
@ -184,18 +224,57 @@ public class LobbyMenu {
app.getGuiNode().attachChild(background); app.getGuiNode().attachChild(background);
} }
private Geometry createCircle(ColorRGBA color) { private Geometry createCircle() {
Sphere sphere = new Sphere(90,90,60.0f); Sphere sphere = new Sphere(90,90,60.0f);
Geometry circleGeometry = new Geometry("Circle", sphere); Geometry circleGeometry = new Geometry("Circle", sphere);
// Create a material with a solid color // Create a material with a solid color
Material material = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); 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); circleGeometry.setMaterial(material);
return circleGeometry; 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. * Schaltet den "Bereit"-Status um.
@ -260,16 +339,22 @@ public class LobbyMenu {
System.out.println("Selected: " + selected); System.out.println("Selected: " + selected);
switch (selected) { switch (selected) {
case "[0]": case "[0]":
System.out.println("Alpha selected"); System.out.println("Laptop selected");
break; break;
case "[1]": case "[1]":
System.out.println("Beta selected"); System.out.println("Flugzeug selected");
break; break;
case "[2]": case "[2]":
System.out.println("Gamma selected"); System.out.println("Jägermeister selected");
break; break;
case "[3]": 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; break;
default: default:
System.out.println("Unknown selection"); 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. */ /** The current state of the client game logic. */
private ClientState state = new LobbyState(this); private ClientState state = new LobbyState(this);
private PlayerHandler playerHandler; private static PlayerHandler playerHandler;
/** /**
* Constructs a ClientGameLogic with the specified sender object. * Constructs a ClientGameLogic with the specified sender object.
@ -83,7 +83,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
state.entry(); state.entry();
} }
public PlayerHandler getPlayerHandler() { public static PlayerHandler getPlayerHandler() {
return playerHandler; return playerHandler;
} }

View File

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