diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java index 41d35bb..34fc1c4 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/TestWorld.java @@ -1,8 +1,6 @@ package pp.monopoly.client.gui; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; +import java.util.List; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; @@ -21,6 +19,7 @@ public class TestWorld { private final MonopolyApp app; private CameraController cameraController; // Steuert die Kamera + private Toolbar toolbar; /** * Konstruktor für TestWorld. @@ -51,7 +50,8 @@ public class TestWorld { ); // Füge die Toolbar hinzu - new Toolbar(app).open(); + toolbar = new Toolbar(app); + toolbar.open(); cameraController.setPosition(0); } diff --git a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java index 989a44e..a2bd72a 100644 --- a/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java +++ b/Projekte/monopoly/client/src/main/java/pp/monopoly/client/gui/Toolbar.java @@ -1,6 +1,7 @@ package pp.monopoly.client.gui; +import java.util.List; import java.util.Random; import com.jme3.font.BitmapText; @@ -18,20 +19,23 @@ import pp.dialog.Dialog; import pp.monopoly.client.MonopolyApp; import pp.monopoly.game.server.Player; 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; /** * Toolbar Klasse, die am unteren Rand der Szene angezeigt wird. * 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 Container toolbarContainer; private final BitmapText positionText; // Anzeige für die aktuelle Position private int currentPosition = 0; // Aktuelle Position auf dem Spielfeld - private String diceOneImage = "Pictures/dice/one.png"; - private String diceTwoImage = "Pictures/dice/two.png"; + private Label imageLabel; + private Label imageLabel2; /** @@ -43,6 +47,8 @@ public class Toolbar extends Dialog { super(app.getDialogManager()); this.app = app; + app.getGameLogic().addListener(this); + // Erstelle die Toolbar toolbarContainer = new Container(new SpringGridLayout(Axis.X, Axis.Y), "toolbar"); @@ -99,13 +105,13 @@ public class Toolbar extends Dialog { Container leftContainer = new Container(); leftContainer.setPreferredSize(new Vector3f(100, 150, 0)); // Adjust size as needed - Label imageLabel = new Label(""); - IconComponent icon = new IconComponent(diceOneImage); // Icon mit Textur erstellen + imageLabel = new Label(""); + IconComponent icon = new IconComponent("Pictures/dice/one.png"); // Icon mit Textur erstellen icon.setIconSize(new Vector2f(100,100)); // Skalierung des Bildes imageLabel.setIcon(icon); - Label imageLabel2 = new Label(""); - IconComponent icon2 = new IconComponent(diceTwoImage); // Icon mit Textur erstellen + imageLabel2 = new Label(""); + IconComponent icon2 = new IconComponent("Pictures/dice/two.png"); // Icon mit Textur erstellen icon2.setIconSize(new Vector2f(100,100)); // Skalierung des Bildes imageLabel2.setIcon(icon2); @@ -138,7 +144,7 @@ public class Toolbar extends Dialog { Button diceButton = new Button("Würfeln"); diceButton.setPreferredSize(new Vector3f(200, 50, 0)); // Full width for Würfeln button diceButton.addClickCommands(s -> ifTopDialog(() -> { - //TODO dice roll logic + app.getGameLogic().send(new RollDice()); app.getGameLogic().playSound(Sound.BUTTON); })); diceContainer.addChild(diceButton); @@ -223,4 +229,46 @@ public class Toolbar extends Dialog { public void escape() { 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); + } + + } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java index 3a6df03..b432191 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/client/ClientGameLogic.java @@ -25,6 +25,7 @@ import pp.monopoly.model.Board; import pp.monopoly.model.IntPoint; import pp.monopoly.model.fields.BoardManager; import pp.monopoly.notification.ClientStateEvent; +import pp.monopoly.notification.DiceRollEvent; import pp.monopoly.notification.GameEvent; import pp.monopoly.notification.GameEventBroker; import pp.monopoly.notification.GameEventListener; @@ -214,9 +215,9 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { */ @Override public void received(DiceResult msg) { - - //Set the dice images playSound(Sound.DICE_ROLL); + System.out.println("Message kam an"); + notifyListeners(new DiceRollEvent(msg.getRollResult().get(0), msg.getRollResult().get(1))); } /** @@ -226,7 +227,7 @@ public class ClientGameLogic implements ServerInterpreter, GameEventBroker { */ @Override public void received(EventDrawCard msg) { - + // Kartenlogik playSound(Sound.EVENT_CARD); } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java index c350479..ac85ba2 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/Player.java @@ -395,6 +395,7 @@ public class Player implements FieldVisitor{ * @return the result of a dice roll (1 to 6) */ private static int rollDice() { + System.out.println("Gewuerfelt"); return random.nextInt(6) + 1; } } @@ -439,7 +440,7 @@ public class Player implements FieldVisitor{ @Override public DiceResult rollDice() { List roll = List.of(Dice.rollDice(), Dice.rollDice()); - rollResult = new DiceResult(roll); + rollResult = new DiceResult(roll.get(0), roll.get(1)); return rollResult; } @@ -491,7 +492,7 @@ public class Player implements FieldVisitor{ @Override public DiceResult rollDice() { List roll = List.of(Dice.rollDice(), Dice.rollDice()); - rollResult = new DiceResult(roll); + rollResult = new DiceResult(roll.get(0), roll.get(1)); if (rollResult.isDoublets()) { state = new ActiveState(); } else if (DoubletsCounter == 0) { diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java index c1f8793..c9c1bbd 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/PlayerHandler.java @@ -166,7 +166,6 @@ public class PlayerHandler { * @return the player with the required id */ public Player getPlayerById(int id) { - System.out.println("TEST"); for (Player player : players) { if (player.getId() == id) return player; System.out.println(player.getId()); diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java index bcfd8da..6751436 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/game/server/ServerGameLogic.java @@ -220,7 +220,8 @@ public class ServerGameLogic implements ClientInterpreter { @Override public void received(RollDice msg, int from) { Player player = playerHandler.getPlayerById(from); - if (player != null && state == ServerState.INGAME) { + if (player != null) { + System.out.println("Ergebniss gesendet"); send(player, player.rollDice()); } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/client/RollDice.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/client/RollDice.java index a4d5dfc..fe118d9 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/client/RollDice.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/client/RollDice.java @@ -11,7 +11,7 @@ public class RollDice extends ClientMessage{ /** * Default constructor for serialization purposes. */ - private RollDice() { /* empty */ } + public RollDice() { /* empty */ } @Override public void accept(ClientInterpreter interpreter, int from) { diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/DiceResult.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/DiceResult.java index 4d63f91..864a347 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/DiceResult.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/message/server/DiceResult.java @@ -7,19 +7,24 @@ import com.jme3.network.serializing.Serializable; @Serializable public class DiceResult extends ServerMessage{ - private List rollResult; + private final int a; + private final int b; /** * Default constructor for serialization purposes. */ - private DiceResult() { /* empty */ } + private DiceResult() { + a = 1; + b = 1; + } - public DiceResult(List rollResult) { - this.rollResult = rollResult; + public DiceResult(int a, int b) { + this.a = a; + this.b = b; } public List getRollResult() { - return rollResult; + return List.of(a,b); } @Override @@ -34,10 +39,10 @@ public class DiceResult extends ServerMessage{ } public boolean isDoublets() { - return rollResult.get(0) == rollResult.get(1); + return a == b; } public int calcTotal() { - return rollResult.get(0)+rollResult.get(1); + return a+b; } } diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/DiceRollEvent.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/DiceRollEvent.java new file mode 100644 index 0000000..ad54906 --- /dev/null +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/DiceRollEvent.java @@ -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); + } + +} diff --git a/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/GameEventListener.java b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/GameEventListener.java index 3dec0c6..a7592cf 100644 --- a/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/GameEventListener.java +++ b/Projekte/monopoly/model/src/main/java/pp/monopoly/notification/GameEventListener.java @@ -45,4 +45,6 @@ public interface GameEventListener { * @param event the received event */ default void receivedEvent(ClientStateEvent event) { /* do nothing */ } + + default void receivedEvent(DiceRollEvent event) { /*Do nothing */} } diff --git a/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java b/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java index 4c1b56d..a79f960 100644 --- a/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java +++ b/Projekte/monopoly/server/src/main/java/pp/monopoly/server/MonopolyServer.java @@ -37,6 +37,7 @@ import pp.monopoly.message.client.RollDice; import pp.monopoly.message.client.TradeOffer; import pp.monopoly.message.client.TradeResponse; import pp.monopoly.message.client.ViewAssetsRequest; +import pp.monopoly.message.server.DiceResult; import pp.monopoly.message.server.GameStart; import pp.monopoly.message.server.NextPlayerTurn; import pp.monopoly.message.server.ServerMessage; @@ -130,6 +131,7 @@ public class MonopolyServer implements MessageListener, Connec Serializer.registerClass(Player.class); Serializer.registerClass(Figure.class); Serializer.registerClass(PlayerHandler.class); + Serializer.registerClass(DiceResult.class); } private void registerListeners() {