clean up and doc

This commit is contained in:
Johannes Schmelz 2024-12-08 18:38:06 +01:00
parent ba8791d9fe
commit 4839ddd497

View File

@ -7,8 +7,15 @@ import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import com.jme3.texture.Texture;
import com.simsilica.lemur.*;
import com.simsilica.lemur.component.*;
import com.simsilica.lemur.Axis;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.Container;
import com.simsilica.lemur.HAlignment;
import com.simsilica.lemur.Label;
import com.simsilica.lemur.VAlignment;
import com.simsilica.lemur.component.IconComponent;
import com.simsilica.lemur.component.QuadBackgroundComponent;
import com.simsilica.lemur.component.SpringGridLayout;
import com.simsilica.lemur.event.MouseEventControl;
import com.simsilica.lemur.event.MouseListener;
import com.simsilica.lemur.style.ElementId;
@ -19,7 +26,11 @@ import pp.monopoly.game.server.Player;
import pp.monopoly.game.server.PlayerHandler;
import pp.monopoly.message.client.EndTurn;
import pp.monopoly.message.client.RollDice;
import pp.monopoly.notification.*;
import pp.monopoly.notification.ButtonStatusEvent;
import pp.monopoly.notification.DiceRollEvent;
import pp.monopoly.notification.GameEventListener;
import pp.monopoly.notification.Sound;
import pp.monopoly.notification.UpdatePlayerView;
/**
* Represents the toolbar interface in the Monopoly application.
@ -27,20 +38,38 @@ import pp.monopoly.notification.*;
*/
public class Toolbar extends Dialog implements GameEventListener {
/** The Monopoly application instance*/
private final MonopolyApp app;
/** The container representing the toolbar interface */
private final Container toolbarContainer;
/** The container representing the player overview information */
private Container overviewContainer;
/** The container representing the player account information */
private Container accountContainer;
/** The player handler instance */
private PlayerHandler playerHandler;
/** The label representing the left dice */
private Label imageLabel;
/** The label representing the right dice */
private Label imageLabel2;
/** The flag to check if the dice can be rolled */
private boolean canRollDice = false;
/** The trade button */
private Button tradeButton;
/** The property menu button */
private Button propertyMenuButton;
/** The end turn button */
private Button endTurnButton;
/** The latest incoming Dice Roll Event */
private DiceRollEvent latestDiceRollEvent = null;
/** The flag to check if the bankrupt pop up is already shown */
private boolean bankruptPopUp = false;
/**
* Constructs a new {@code Toolbar} for the given {@code MonopolyApp}.
*
* @param app The {@code MonopolyApp} instance to create the toolbar for.
*/
public Toolbar(MonopolyApp app) {
super(app.getDialogManager());
this.app = app;
@ -52,6 +81,11 @@ public class Toolbar extends Dialog implements GameEventListener {
app.getGuiNode().attachChild(toolbarContainer);
}
/**
* Sets up the toolbar interface with the game controls, player information, and event handling.
*
* @return The container representing the toolbar interface.
*/
private Container setupToolbar() {
Container container = new Container(new SpringGridLayout(Axis.X, Axis.Y), "toolbar");
container.setLocalTranslation(0, 200, 0);
@ -70,6 +104,11 @@ public class Toolbar extends Dialog implements GameEventListener {
return container;
}
/**
* Sets up the borders for the toolbar interface.
*
* @param container The container representing the toolbar interface.
*/
private void setupBorders(Container container) {
addBorder(0, 205, app.getCamera().getWidth(), 5, ColorRGBA.DarkGray); // Top
addBorder(0, 5, app.getCamera().getWidth(), 10, ColorRGBA.DarkGray); // Bottom
@ -77,6 +116,15 @@ public class Toolbar extends Dialog implements GameEventListener {
addBorder(app.getCamera().getWidth() - 5, 200, 5, 210, ColorRGBA.DarkGray); // Right
}
/**
* Adds a border to the toolbar interface with the specified dimensions and color.
*
* @param x The x-coordinate of the border.
* @param y The y-coordinate of the border.
* @param width The width of the border.
* @param height The height of the border.
* @param color The color of the border.
*/
private void addBorder(float x, float y, float width, float height, ColorRGBA color) {
Container border = new Container();
border.setPreferredSize(new Vector3f(width, height, 0));
@ -85,6 +133,11 @@ public class Toolbar extends Dialog implements GameEventListener {
app.getGuiNode().attachChild(border);
}
/**
* Sets up the player information section of the toolbar interface.
*
* @param parentContainer The container representing the toolbar interface.
*/
private void setupPlayerInfoSection(Container parentContainer) {
Container playerInfoSection = parentContainer.addChild(new Container(new SpringGridLayout(Axis.X, Axis.Y)));
playerInfoSection.setPreferredSize(new Vector3f(600, 300, 0)); // Adjust size for both containers
@ -104,12 +157,22 @@ public class Toolbar extends Dialog implements GameEventListener {
refreshPlayerView();
}
/**
* Sets up the dice section of the toolbar interface.
*
* @param container The container representing the toolbar interface.
*/
private void setupDiceSection(Container container) {
Container diceContainer = container.addChild(new Container(new SpringGridLayout(Axis.X, Axis.Y)));
diceContainer.addChild(createDiceDisplay());
diceContainer.setBackground(null);
}
/**
* Sets up the action menu of the toolbar interface.
*
* @param container The container representing the toolbar interface.
*/
private void setupActionMenu(Container container) {
Container menuContainer = container.addChild(new Container());
menuContainer.addChild(createTradeButton(getCurrentPlayerColor()));
@ -118,11 +181,21 @@ public class Toolbar extends Dialog implements GameEventListener {
menuContainer.setBackground(null);
}
/**
* Returns the color of the current player.
*
* @return The color of the current player.
*/
private ColorRGBA getCurrentPlayerColor() {
Player currentPlayer = playerHandler.getPlayerById(app.getId());
return Player.getColor(currentPlayer.getId()).getColor();
}
/**
* Creates the dice display section of the toolbar interface.
*
* @return The container representing the dice display section.
*/
private Container createDiceDisplay() {
Container horizontalContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y));
horizontalContainer.setPreferredSize(new Vector3f(200, 150, 0));
@ -163,6 +236,12 @@ public class Toolbar extends Dialog implements GameEventListener {
return horizontalContainer;
}
/**
* Creates a dice label with the specified icon path.
*
* @param iconPath The path to the icon image.
* @return The label representing the dice.
*/
private Label createDiceLabel(String iconPath) {
Label label = new Label("");
IconComponent icon = new IconComponent(iconPath);
@ -171,6 +250,12 @@ public class Toolbar extends Dialog implements GameEventListener {
return label;
}
/**
* Creates a dice container with the specified label.
*
* @param label The label representing the dice.
* @return The container representing the dice.
*/
private Container createDiceContainer(Label label) {
Container container = new Container();
container.setBackground(null);
@ -179,6 +264,9 @@ public class Toolbar extends Dialog implements GameEventListener {
return container;
}
/**
* Handles the dice roll event.
*/
private void handleDiceRoll() {
ifTopDialog(() -> {
if (!canRollDice) return;
@ -190,18 +278,45 @@ public class Toolbar extends Dialog implements GameEventListener {
});
}
/**
* Creates a trade button with the specified player color.
*
* @param playerColor The color of the player.
* @return The button representing the trade action.
*/
private Button createTradeButton(ColorRGBA playerColor) {
return createActionButton(playerColor, "icons/icon-handeln.png", 100, () -> new ChoosePartner(app).open());
}
/**
* Creates a property menu button with the specified player color.
*
* @param playerColor The color of the player.
* @return The button representing the property menu action.
*/
private Button createPropertyMenuButton(ColorRGBA playerColor) {
return createActionButton(playerColor, "icons/icon-gebaude.png", 75, () -> new BuildingAdminMenu(app).open());
}
/**
* Creates an end turn button with the specified player color.
*
* @param playerColor The color of the player.
* @return The button representing the end turn action.
*/
private Button createEndTurnButton(ColorRGBA playerColor) {
return createActionButton(playerColor, "icons/icon-zugbeenden.png", 75, () -> handleEndTurn());
}
/**
* Creates an action button with the specified color, icon path, icon size, and action.
*
* @param color The color of the button.
* @param iconPath The path to the icon image.
* @param iconSize The size of the icon.
* @param action The action to perform when the button is clicked.
* @return The button representing the action.
*/
private Button createActionButton(ColorRGBA color, String iconPath, int iconSize, Runnable action) {
Button button = new Button("", new ElementId("button-toolbar2"));
button.setPreferredSize(new Vector3f(150, 50, 0));
@ -217,6 +332,12 @@ public class Toolbar extends Dialog implements GameEventListener {
return button;
}
/**
* Creates a background with the specified color.
*
* @param color The color of the background.
* @return The background component.
*/
private QuadBackgroundComponent createButtonBackground(ColorRGBA color) {
QuadBackgroundComponent background = new QuadBackgroundComponent(color);
Texture gradient = app.getAssetManager().loadTexture("Textures/gradient.png");
@ -224,6 +345,9 @@ public class Toolbar extends Dialog implements GameEventListener {
return background;
}
/**
* Handles the end turn event.
*/
private void handleEndTurn() {
Player currentPlayer = playerHandler.getPlayerById(app.getId());
if (currentPlayer.getAccountBalance() < 0 && !bankruptPopUp) {
@ -236,6 +360,9 @@ public class Toolbar extends Dialog implements GameEventListener {
}
}
/**
* Starts the dice animation.
*/
private void startDiceAnimation() {
long startTime = System.currentTimeMillis();
new Thread(() -> {
@ -250,6 +377,12 @@ public class Toolbar extends Dialog implements GameEventListener {
}).start();
}
/**
* Animates the dice roll.
*
* @param startTime The start time of the animation.
* @throws InterruptedException If the animation is interrupted.
*/
private void animateDice(long startTime) throws InterruptedException {
int[] currentFace = {1};
while (System.currentTimeMillis() - startTime < 2000) {
@ -259,6 +392,11 @@ public class Toolbar extends Dialog implements GameEventListener {
}
}
/**
* Updates the dice icons with the specified face.
*
* @param face The face of the dice.
*/
private void updateDiceIcons(int face) {
app.enqueue(() -> {
setDiceIcon(imageLabel, diceToString(face));
@ -266,6 +404,11 @@ public class Toolbar extends Dialog implements GameEventListener {
});
}
/**
* Shows the final dice result.
*
* @param event The dice roll event.
*/
private void showFinalDiceResult(DiceRollEvent event) {
app.enqueue(() -> {
setDiceIcon(imageLabel, diceToString(event.a()));
@ -273,12 +416,24 @@ public class Toolbar extends Dialog implements GameEventListener {
});
}
/**
* Sets the dice icon with the specified image path.
*
* @param label The label representing the dice.
* @param imagePath The path to the icon image.
*/
private void setDiceIcon(Label label, String imagePath) {
IconComponent icon = new IconComponent(imagePath);
icon.setIconSize(new Vector2f(80, 80));
label.setIcon(icon);
}
/**
* Converts the dice number to a string representation.
*
* @param i The dice number.
* @return The string representation of the dice number.
*/
private String diceToString(int i) {
return "Pictures/dice/" + switch (i) {
case 1 -> "one";
@ -302,6 +457,9 @@ public class Toolbar extends Dialog implements GameEventListener {
refreshPlayerView();
}
/**
* Refreshes the player view.
*/
private void refreshPlayerView() {
accountContainer.clearChildren();
overviewContainer.clearChildren();
@ -313,6 +471,9 @@ public class Toolbar extends Dialog implements GameEventListener {
overviewContainer.setBackground(null);
}
/**
* Adds the account details to the player view.
*/
private void addAccountDetails() {
Player currentPlayer = playerHandler.getPlayerById(app.getId());
accountContainer.addChild(new Label("Kontostand", new ElementId("label-toolbar")));
@ -321,6 +482,9 @@ public class Toolbar extends Dialog implements GameEventListener {
accountContainer.addChild(new Label(String.valueOf(currentPlayer.getNumJailCard()), new ElementId("label-account")));
}
/**
* Adds the overview details to the player view.
*/
private void addOverviewDetails() {
overviewContainer.addChild(new Label("Übersicht", new ElementId("label-toolbar")));
for (Player player : playerHandler.getPlayers()) {