dice button now works and update dice images accordingly

This commit is contained in:
Johannes Schmelz 2024-11-26 14:59:48 +01:00
parent d6ce859fcd
commit 27a0ab52e6
11 changed files with 96 additions and 27 deletions

View File

@ -1,8 +1,6 @@
package pp.monopoly.client.gui; package pp.monopoly.client.gui;
import java.util.concurrent.Executors; import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.math.ColorRGBA; import com.jme3.math.ColorRGBA;
@ -21,6 +19,7 @@ public class TestWorld {
private final MonopolyApp app; private final MonopolyApp app;
private CameraController cameraController; // Steuert die Kamera private CameraController cameraController; // Steuert die Kamera
private Toolbar toolbar;
/** /**
* Konstruktor für TestWorld. * Konstruktor für TestWorld.
@ -51,7 +50,8 @@ public class TestWorld {
); );
// Füge die Toolbar hinzu // Füge die Toolbar hinzu
new Toolbar(app).open(); toolbar = new Toolbar(app);
toolbar.open();
cameraController.setPosition(0); cameraController.setPosition(0);
} }

View File

@ -1,6 +1,7 @@
package pp.monopoly.client.gui; package pp.monopoly.client.gui;
import java.util.List;
import java.util.Random; import java.util.Random;
import com.jme3.font.BitmapText; import com.jme3.font.BitmapText;
@ -18,20 +19,23 @@ import pp.dialog.Dialog;
import pp.monopoly.client.MonopolyApp; import pp.monopoly.client.MonopolyApp;
import pp.monopoly.game.server.Player; import pp.monopoly.game.server.Player;
import pp.monopoly.game.server.PlayerHandler; import pp.monopoly.game.server.PlayerHandler;
import pp.monopoly.message.client.RollDice;
import pp.monopoly.notification.DiceRollEvent;
import pp.monopoly.notification.GameEventListener;
import pp.monopoly.notification.Sound; import pp.monopoly.notification.Sound;
/** /**
* Toolbar Klasse, die am unteren Rand der Szene angezeigt wird. * Toolbar Klasse, die am unteren Rand der Szene angezeigt wird.
* Die Buttons bewegen den Würfel auf dem Spielfeld. * Die Buttons bewegen den Würfel auf dem Spielfeld.
*/ */
public class Toolbar extends Dialog { public class Toolbar extends Dialog implements GameEventListener{
private final MonopolyApp app; private final MonopolyApp app;
private final Container toolbarContainer; private final Container toolbarContainer;
private final BitmapText positionText; // Anzeige für die aktuelle Position private final BitmapText positionText; // Anzeige für die aktuelle Position
private int currentPosition = 0; // Aktuelle Position auf dem Spielfeld private int currentPosition = 0; // Aktuelle Position auf dem Spielfeld
private String diceOneImage = "Pictures/dice/one.png"; private Label imageLabel;
private String diceTwoImage = "Pictures/dice/two.png"; private Label imageLabel2;
/** /**
@ -43,6 +47,8 @@ public class Toolbar extends Dialog {
super(app.getDialogManager()); super(app.getDialogManager());
this.app = app; this.app = app;
app.getGameLogic().addListener(this);
// Erstelle die Toolbar // Erstelle die Toolbar
toolbarContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y), "toolbar"); toolbarContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y), "toolbar");
@ -99,13 +105,13 @@ public class Toolbar extends Dialog {
Container leftContainer = new Container(); Container leftContainer = new Container();
leftContainer.setPreferredSize(new Vector3f(100, 150, 0)); // Adjust size as needed leftContainer.setPreferredSize(new Vector3f(100, 150, 0)); // Adjust size as needed
Label imageLabel = new Label(""); imageLabel = new Label("");
IconComponent icon = new IconComponent(diceOneImage); // Icon mit Textur erstellen IconComponent icon = new IconComponent("Pictures/dice/one.png"); // Icon mit Textur erstellen
icon.setIconSize(new Vector2f(100,100)); // Skalierung des Bildes icon.setIconSize(new Vector2f(100,100)); // Skalierung des Bildes
imageLabel.setIcon(icon); imageLabel.setIcon(icon);
Label imageLabel2 = new Label(""); imageLabel2 = new Label("");
IconComponent icon2 = new IconComponent(diceTwoImage); // Icon mit Textur erstellen IconComponent icon2 = new IconComponent("Pictures/dice/two.png"); // Icon mit Textur erstellen
icon2.setIconSize(new Vector2f(100,100)); // Skalierung des Bildes icon2.setIconSize(new Vector2f(100,100)); // Skalierung des Bildes
imageLabel2.setIcon(icon2); imageLabel2.setIcon(icon2);
@ -138,7 +144,7 @@ public class Toolbar extends Dialog {
Button diceButton = new Button("Würfeln"); Button diceButton = new Button("Würfeln");
diceButton.setPreferredSize(new Vector3f(200, 50, 0)); // Full width for Würfeln button diceButton.setPreferredSize(new Vector3f(200, 50, 0)); // Full width for Würfeln button
diceButton.addClickCommands(s -> ifTopDialog(() -> { diceButton.addClickCommands(s -> ifTopDialog(() -> {
//TODO dice roll logic app.getGameLogic().send(new RollDice());
app.getGameLogic().playSound(Sound.BUTTON); app.getGameLogic().playSound(Sound.BUTTON);
})); }));
diceContainer.addChild(diceButton); diceContainer.addChild(diceButton);
@ -223,4 +229,46 @@ public class Toolbar extends Dialog {
public void escape() { public void escape() {
new SettingsMenu(app).open(); new SettingsMenu(app).open();
} }
@Override
public void receivedEvent(DiceRollEvent event) {
updateDiceImages(event.a(), event.b());
}
private void updateDiceImages(int a, int b) {
IconComponent icon1 = new IconComponent(diceToString(a));
IconComponent icon2 = new IconComponent(diceToString(b));
icon1.setIconSize(new Vector2f(100, 100));
icon2.setIconSize(new Vector2f(100, 100));
imageLabel.setIcon(icon1);
imageLabel2.setIcon(icon2);
System.out.println("Dice images set");
}
private String diceToString(int i) {
switch (i) {
case 1:
return "Pictures/dice/one.png";
case 2:
return "Pictures/dice/two.png";
case 3:
return "Pictures/dice/three.png";
case 4:
return "Pictures/dice/four.png";
case 5:
return "Pictures/dice/five.png";
case 6:
return "Pictures/dice/six.png";
default:
throw new IllegalArgumentException("Invalid dice number: " + i);
}
}
} }

View File

@ -25,6 +25,7 @@ import pp.monopoly.model.Board;
import pp.monopoly.model.IntPoint; import pp.monopoly.model.IntPoint;
import pp.monopoly.model.fields.BoardManager; import pp.monopoly.model.fields.BoardManager;
import pp.monopoly.notification.ClientStateEvent; import pp.monopoly.notification.ClientStateEvent;
import pp.monopoly.notification.DiceRollEvent;
import pp.monopoly.notification.GameEvent; import pp.monopoly.notification.GameEvent;
import pp.monopoly.notification.GameEventBroker; import pp.monopoly.notification.GameEventBroker;
import pp.monopoly.notification.GameEventListener; import pp.monopoly.notification.GameEventListener;
@ -214,9 +215,9 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker {
*/ */
@Override @Override
public void received(DiceResult msg) { public void received(DiceResult msg) {
//Set the dice images
playSound(Sound.DICE_ROLL); playSound(Sound.DICE_ROLL);
System.out.println("Message kam an");
notifyListeners(new DiceRollEvent(msg.getRollResult().get(0), msg.getRollResult().get(1)));
} }
/** /**

View File

@ -395,6 +395,7 @@ public class Player implements FieldVisitor<Void>{
* @return the result of a dice roll (1 to 6) * @return the result of a dice roll (1 to 6)
*/ */
private static int rollDice() { private static int rollDice() {
System.out.println("Gewuerfelt");
return random.nextInt(6) + 1; return random.nextInt(6) + 1;
} }
} }
@ -439,7 +440,7 @@ public class Player implements FieldVisitor<Void>{
@Override @Override
public DiceResult rollDice() { public DiceResult rollDice() {
List<Integer> roll = List.of(Dice.rollDice(), Dice.rollDice()); List<Integer> roll = List.of(Dice.rollDice(), Dice.rollDice());
rollResult = new DiceResult(roll); rollResult = new DiceResult(roll.get(0), roll.get(1));
return rollResult; return rollResult;
} }
@ -491,7 +492,7 @@ public class Player implements FieldVisitor<Void>{
@Override @Override
public DiceResult rollDice() { public DiceResult rollDice() {
List<Integer> roll = List.of(Dice.rollDice(), Dice.rollDice()); List<Integer> roll = List.of(Dice.rollDice(), Dice.rollDice());
rollResult = new DiceResult(roll); rollResult = new DiceResult(roll.get(0), roll.get(1));
if (rollResult.isDoublets()) { if (rollResult.isDoublets()) {
state = new ActiveState(); state = new ActiveState();
} else if (DoubletsCounter == 0) { } else if (DoubletsCounter == 0) {

View File

@ -166,7 +166,6 @@ public class PlayerHandler {
* @return the player with the required id * @return the player with the required id
*/ */
public Player getPlayerById(int id) { public Player getPlayerById(int id) {
System.out.println("TEST");
for (Player player : players) { for (Player player : players) {
if (player.getId() == id) return player; if (player.getId() == id) return player;
System.out.println(player.getId()); System.out.println(player.getId());

View File

@ -220,7 +220,8 @@ public class ServerGameLogic implements ClientInterpreter {
@Override @Override
public void received(RollDice msg, int from) { public void received(RollDice msg, int from) {
Player player = playerHandler.getPlayerById(from); Player player = playerHandler.getPlayerById(from);
if (player != null && state == ServerState.INGAME) { if (player != null) {
System.out.println("Ergebniss gesendet");
send(player, player.rollDice()); send(player, player.rollDice());
} }
} }

View File

@ -11,7 +11,7 @@ public class RollDice extends ClientMessage{
/** /**
* Default constructor for serialization purposes. * Default constructor for serialization purposes.
*/ */
private RollDice() { /* empty */ } public RollDice() { /* empty */ }
@Override @Override
public void accept(ClientInterpreter interpreter, int from) { public void accept(ClientInterpreter interpreter, int from) {

View File

@ -7,19 +7,24 @@ import com.jme3.network.serializing.Serializable;
@Serializable @Serializable
public class DiceResult extends ServerMessage{ public class DiceResult extends ServerMessage{
private List<Integer> rollResult; private final int a;
private final int b;
/** /**
* Default constructor for serialization purposes. * Default constructor for serialization purposes.
*/ */
private DiceResult() { /* empty */ } private DiceResult() {
a = 1;
b = 1;
}
public DiceResult(List<Integer> rollResult) { public DiceResult(int a, int b) {
this.rollResult = rollResult; this.a = a;
this.b = b;
} }
public List<Integer> getRollResult() { public List<Integer> getRollResult() {
return rollResult; return List.of(a,b);
} }
@Override @Override
@ -34,10 +39,10 @@ public class DiceResult extends ServerMessage{
} }
public boolean isDoublets() { public boolean isDoublets() {
return rollResult.get(0) == rollResult.get(1); return a == b;
} }
public int calcTotal() { public int calcTotal() {
return rollResult.get(0)+rollResult.get(1); return a+b;
} }
} }

View File

@ -0,0 +1,10 @@
package pp.monopoly.notification;
public record DiceRollEvent(int a, int b) implements GameEvent{
@Override
public void notifyListener(GameEventListener listener) {
listener.receivedEvent(this);
}
}

View File

@ -45,4 +45,6 @@ public interface GameEventListener {
* @param event the received event * @param event the received event
*/ */
default void receivedEvent(ClientStateEvent event) { /* do nothing */ } default void receivedEvent(ClientStateEvent event) { /* do nothing */ }
default void receivedEvent(DiceRollEvent event) { /*Do nothing */}
} }

View File

@ -37,6 +37,7 @@ import pp.monopoly.message.client.RollDice;
import pp.monopoly.message.client.TradeOffer; import pp.monopoly.message.client.TradeOffer;
import pp.monopoly.message.client.TradeResponse; import pp.monopoly.message.client.TradeResponse;
import pp.monopoly.message.client.ViewAssetsRequest; import pp.monopoly.message.client.ViewAssetsRequest;
import pp.monopoly.message.server.DiceResult;
import pp.monopoly.message.server.GameStart; import pp.monopoly.message.server.GameStart;
import pp.monopoly.message.server.NextPlayerTurn; import pp.monopoly.message.server.NextPlayerTurn;
import pp.monopoly.message.server.ServerMessage; import pp.monopoly.message.server.ServerMessage;
@ -130,6 +131,7 @@ public class MonopolyServer implements MessageListener<HostedConnection>, Connec
Serializer.registerClass(Player.class); Serializer.registerClass(Player.class);
Serializer.registerClass(Figure.class); Serializer.registerClass(Figure.class);
Serializer.registerClass(PlayerHandler.class); Serializer.registerClass(PlayerHandler.class);
Serializer.registerClass(DiceResult.class);
} }
private void registerListeners() { private void registerListeners() {