From 7f6d71282ab23e2e120be9f65da3028154f9a348 Mon Sep 17 00:00:00 2001 From: LapisBerry Date: Thu, 23 May 2024 19:38:44 +0700 Subject: [PATCH] feat: LobbyScene fully function --- src/main/java/com/lapisberry/Main.java | 36 ++++++++--------- .../game/controllers/LobbyController.java | 8 +++- .../com/lapisberry/gui/MediaController.java | 8 ++-- .../com/lapisberry/gui/scenes/JoinScene.java | 11 +++--- .../com/lapisberry/gui/scenes/LobbyScene.java | 39 +++++++++++++------ src/main/java/com/lapisberry/net/Client.java | 9 ++++- src/main/java/com/lapisberry/net/Server.java | 7 +++- .../lapisberry/net/packets/LobbyPacket.java | 19 +++++---- 8 files changed, 88 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/lapisberry/Main.java b/src/main/java/com/lapisberry/Main.java index e664e34..a177bc0 100644 --- a/src/main/java/com/lapisberry/Main.java +++ b/src/main/java/com/lapisberry/Main.java @@ -18,24 +18,6 @@ public static void main(String[] args) { launch(args); } - @Override - public void start(Stage stage) { - primaryStage = stage; - primaryStage.setTitle("MaKillMai"); - primaryStage.getIcons().add(new Image(ClassLoader.getSystemResource("images/makillmai-icon.png").toString())); - goToJoinScene(); - primaryStage.setOnCloseRequest(event -> { - closeServer(); - closeClient(); - System.exit(0); - }); - - primaryStage.show(); - - // This line will make the username field unfocused when started. - primaryStage.getScene().getRoot().requestFocus(); - } - public static void goToJoinScene() { primaryStage.setScene(new JoinScene()); } @@ -70,4 +52,22 @@ public static Stage getPrimaryStage() { public static Client getClient() { return client; } + + @Override + public void start(Stage stage) { + primaryStage = stage; + primaryStage.setTitle("MaKillMai"); + primaryStage.getIcons().add(new Image(ClassLoader.getSystemResource("images/makillmai-icon.png").toString())); + goToJoinScene(); + primaryStage.setOnCloseRequest(event -> { + closeServer(); + closeClient(); + System.exit(0); + }); + + primaryStage.show(); + + // This line will make the username field unfocused when started. + primaryStage.getScene().getRoot().requestFocus(); + } } \ No newline at end of file diff --git a/src/main/java/com/lapisberry/game/controllers/LobbyController.java b/src/main/java/com/lapisberry/game/controllers/LobbyController.java index e9f9bf6..fdeab0b 100644 --- a/src/main/java/com/lapisberry/game/controllers/LobbyController.java +++ b/src/main/java/com/lapisberry/game/controllers/LobbyController.java @@ -8,9 +8,9 @@ public class LobbyController implements Serializable { @Serial - private static final long serialVersionUID = 2172799206019433070L; + private static final long serialVersionUID = -3466527055512798470L; // Fields - private final ArrayList> players; // Pair + private ArrayList> players; // Pair // Constructors public LobbyController() { @@ -39,4 +39,8 @@ public String toString() { public ArrayList> getPlayers() { return players; } + + public void setPlayers(ArrayList> players) { + this.players = players; + } } diff --git a/src/main/java/com/lapisberry/gui/MediaController.java b/src/main/java/com/lapisberry/gui/MediaController.java index 07413cd..3d2d13d 100644 --- a/src/main/java/com/lapisberry/gui/MediaController.java +++ b/src/main/java/com/lapisberry/gui/MediaController.java @@ -16,13 +16,15 @@ public final class MediaController { // Preload all musics // Music should play forever + // preload all sound fxs + // sound fx should play one time + public static final MediaPlayer buttonClickSound = loadRes("audios/button-click.mp3"); + // Set volume for music static { } - // preload all sound fxs - // sound fx should play one time - public static final MediaPlayer buttonClickSound = loadRes("audios/button-click.mp3"); + // Set volume for sound fx static { buttonClickSound.setVolume(0.8); diff --git a/src/main/java/com/lapisberry/gui/scenes/JoinScene.java b/src/main/java/com/lapisberry/gui/scenes/JoinScene.java index e454199..dc328ad 100644 --- a/src/main/java/com/lapisberry/gui/scenes/JoinScene.java +++ b/src/main/java/com/lapisberry/gui/scenes/JoinScene.java @@ -159,6 +159,7 @@ private CreateServerButton() { private static class AlertContainer extends BorderPane { private static final Label alertLabel = new Label(); + static { alertLabel.setFont(Font.loadFont(Inter_Regular, 18)); alertLabel.setTextFill(Color.WHITE); @@ -167,6 +168,11 @@ private static class AlertContainer extends BorderPane { alertLabel.setAlignment(Pos.CENTER); } + private AlertContainer() { + super(); + setPadding(new Insets(10)); + } + private void alert(final String alertMessage) { alertLabel.setText(alertMessage); new Thread(() -> { @@ -178,10 +184,5 @@ private void alert(final String alertMessage) { Platform.runLater(() -> setRight(null)); }, "alert thread").start(); } - - private AlertContainer() { - super(); - setPadding(new Insets(10)); - } } } \ No newline at end of file diff --git a/src/main/java/com/lapisberry/gui/scenes/LobbyScene.java b/src/main/java/com/lapisberry/gui/scenes/LobbyScene.java index fd450b5..6d9f4b6 100644 --- a/src/main/java/com/lapisberry/gui/scenes/LobbyScene.java +++ b/src/main/java/com/lapisberry/gui/scenes/LobbyScene.java @@ -1,8 +1,11 @@ package com.lapisberry.gui.scenes; import com.lapisberry.Main; +import com.lapisberry.game.controllers.LobbyController; +import javafx.application.Platform; import javafx.geometry.Insets; import javafx.geometry.Pos; +import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; @@ -17,19 +20,29 @@ import static com.lapisberry.gui.FontPreloader.Inter_SemiBold; public class LobbyScene extends Scene { + private static final Title title = new Title("Lobby"); + private static final PlayerPanel playerPanel = new PlayerPanel(); + private static final StartButton startButton = new StartButton("Start Game"); + private static final Container container = new Container(title, playerPanel, startButton); + public LobbyScene() { - super(new StackPane(new Container()), Main.getPrimaryStage().getScene().getWidth(), Main.getPrimaryStage().getScene().getHeight()); + super(new StackPane(container), Main.getPrimaryStage().getScene().getWidth(), Main.getPrimaryStage().getScene().getHeight()); } - private static class Container extends VBox { - private Container() { - super(); - Title title = new Title("Lobby"); - PlayerPanel playerPanel = new PlayerPanel(); - StartButton startButton = new StartButton("Start Game"); + public static void updatePlayerList(LobbyController lobbyController) { + Platform.runLater(() -> { + PlayerPanel.PlayerList.vbox.getChildren().clear(); + lobbyController.getPlayers().forEach(pair -> addPlayer(pair.getValue())); + }); + } + + private static void addPlayer(String name) { + PlayerPanel.PlayerList.addPlayer(name); + } - //VBox vbox = new VBox(); - getChildren().addAll(title, playerPanel, startButton); + private static class Container extends VBox { + private Container(Node... children) { + super(children); setMaxWidth(400); setMaxHeight(200); setAlignment(Pos.CENTER); @@ -68,10 +81,10 @@ private SubTitle(String text) { } private static class PlayerList extends ScrollPane { + private static final VBox vbox = new VBox(); + private PlayerList() { super(); - VBox vbox = new VBox(); - vbox.getChildren().addAll(new PlayerItem("Player 1"), new PlayerItem("Player 2"), new PlayerItem("Player 1"), new PlayerItem("Player 2"), new PlayerItem("Player 1"), new PlayerItem("Player 2"), new PlayerItem("Player 1"), new PlayerItem("Player 2")); vbox.setMinWidth(336); vbox.setBackground(new Background(new BackgroundFill(Color.valueOf("D9D9D9"), new CornerRadii(0), null))); vbox.setAlignment(Pos.TOP_CENTER); @@ -81,6 +94,10 @@ private PlayerList() { setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, new CornerRadii(0), null))); setContent(vbox); } + + private static void addPlayer(String name) { + vbox.getChildren().add(new PlayerItem(name)); + } } private static class PlayerItem extends Label { diff --git a/src/main/java/com/lapisberry/net/Client.java b/src/main/java/com/lapisberry/net/Client.java index 26f2435..adb5e77 100644 --- a/src/main/java/com/lapisberry/net/Client.java +++ b/src/main/java/com/lapisberry/net/Client.java @@ -2,8 +2,10 @@ import com.lapisberry.game.controllers.GameController; import com.lapisberry.game.controllers.LobbyController; +import com.lapisberry.gui.scenes.LobbyScene; import com.lapisberry.net.packets.ClientPacket; import com.lapisberry.net.packets.JoinResponsePacket; +import com.lapisberry.net.packets.LobbyPacket; import com.lapisberry.net.packets.ServerPacket; import com.lapisberry.utils.Config; import com.lapisberry.utils.exceptions.ConnectionRefusedException; @@ -16,11 +18,11 @@ public class Client implements Runnable { // Fields private final Socket socket; - private int clientId; private final ObjectInputStream inputStream; private final ObjectOutputStream outputStream; private final LobbyController clientLobby; private final GameController clientGame; + private int clientId; // Constructors public Client(final String host) { @@ -49,16 +51,21 @@ private void startListeningServerPacket() { processPacketFromServer(packet); } catch (IOException e) { System.out.println("Server disconnected."); + break; } catch (ClassNotFoundException | ClassCastException e) { System.out.println("Packet from server cannot be read."); } } + close(); } private void processPacketFromServer(ServerPacket packet) { System.out.println("Processing packet from server: " + packet); if (packet instanceof JoinResponsePacket joinResponsePacket) { setClientId(joinResponsePacket.getClientId()); + } else if (packet instanceof LobbyPacket lobbyPacket) { + clientLobby.setPlayers(lobbyPacket.getPlayers()); + LobbyScene.updatePlayerList(clientLobby); } } diff --git a/src/main/java/com/lapisberry/net/Server.java b/src/main/java/com/lapisberry/net/Server.java index 81e9e64..369dcfe 100644 --- a/src/main/java/com/lapisberry/net/Server.java +++ b/src/main/java/com/lapisberry/net/Server.java @@ -7,6 +7,7 @@ import com.lapisberry.net.packets.LobbyPacket; import com.lapisberry.net.packets.ServerPacket; import com.lapisberry.utils.Config; +import javafx.util.Pair; import java.io.IOException; import java.net.ServerSocket; @@ -65,7 +66,8 @@ public void processPacketFromClient(ClientHandler sender, ClientPacket packet) { System.out.println("Processing packet from " + sender.getSocket().getInetAddress().getHostAddress() + ": " + packet); if (packet instanceof JoinRequestPacket joinRequestPacket) { serverLobby.addPlayer(sender.getClientId(), joinRequestPacket.getUsername()); - sendPacketToAllClients(new LobbyPacket(serverLobby)); + ArrayList> players = new ArrayList<>(serverLobby.getPlayers()); + sendPacketToAllClients(new LobbyPacket(players)); } } @@ -80,6 +82,7 @@ public void close() { public void removeClientHandler(ClientHandler clientHandler) { clientHandlers.remove(clientHandler); serverLobby.removePlayer(clientHandler.getClientId()); - sendPacketToAllClients(new LobbyPacket(serverLobby)); + ArrayList> players = new ArrayList<>(serverLobby.getPlayers()); + sendPacketToAllClients(new LobbyPacket(players)); } } \ No newline at end of file diff --git a/src/main/java/com/lapisberry/net/packets/LobbyPacket.java b/src/main/java/com/lapisberry/net/packets/LobbyPacket.java index 3f322bd..2b042df 100644 --- a/src/main/java/com/lapisberry/net/packets/LobbyPacket.java +++ b/src/main/java/com/lapisberry/net/packets/LobbyPacket.java @@ -1,9 +1,10 @@ package com.lapisberry.net.packets; -import com.lapisberry.game.controllers.LobbyController; +import javafx.util.Pair; import java.io.Serial; import java.io.Serializable; +import java.util.ArrayList; /** * The {@code LobbyPacket} class is the packet sending from server to client to update the lobby. @@ -12,21 +13,25 @@ public class LobbyPacket extends ServerPacket implements Serializable { @Serial private static final long serialVersionUID = 5601782103675952467L; // Fields - private final LobbyController lobbyController; + private final ArrayList> players; // Constructors - public LobbyPacket(LobbyController lobbyController) { - this.lobbyController = lobbyController; + public LobbyPacket(ArrayList> players) { + this.players = players; } // Methods @Override public String toString() { - return "LobbyPacket{lobbyController=" + lobbyController + "}"; + StringBuilder stringBuilder = new StringBuilder(); + for (Pair player : players) { + stringBuilder.append(player.getKey()).append("=").append(player.getValue()).append(", "); + } + return "LobbyPacket{lobbyController=" + stringBuilder + "}"; } // Getters - public LobbyController getLobbyController() { - return lobbyController; + public ArrayList> getPlayers() { + return players; } }