Skip to content

Commit

Permalink
feat: LobbyScene fully function
Browse files Browse the repository at this point in the history
  • Loading branch information
LapisBerry committed May 23, 2024
1 parent 45558cc commit 7f6d712
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 49 deletions.
36 changes: 18 additions & 18 deletions src/main/java/com/lapisberry/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair<Integer, String>> players; // Pair<clientId, playerName>
private ArrayList<Pair<Integer, String>> players; // Pair<clientId, playerName>

// Constructors
public LobbyController() {
Expand Down Expand Up @@ -39,4 +39,8 @@ public String toString() {
public ArrayList<Pair<Integer, String>> getPlayers() {
return players;
}

public void setPlayers(ArrayList<Pair<Integer, String>> players) {
this.players = players;
}
}
8 changes: 5 additions & 3 deletions src/main/java/com/lapisberry/gui/MediaController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/lapisberry/gui/scenes/JoinScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(() -> {
Expand All @@ -178,10 +184,5 @@ private void alert(final String alertMessage) {
Platform.runLater(() -> setRight(null));
}, "alert thread").start();
}

private AlertContainer() {
super();
setPadding(new Insets(10));
}
}
}
39 changes: 28 additions & 11 deletions src/main/java/com/lapisberry/gui/scenes/LobbyScene.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/lapisberry/net/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/lapisberry/net/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Pair<Integer, String>> players = new ArrayList<>(serverLobby.getPlayers());
sendPacketToAllClients(new LobbyPacket(players));
}
}

Expand All @@ -80,6 +82,7 @@ public void close() {
public void removeClientHandler(ClientHandler clientHandler) {
clientHandlers.remove(clientHandler);
serverLobby.removePlayer(clientHandler.getClientId());
sendPacketToAllClients(new LobbyPacket(serverLobby));
ArrayList<Pair<Integer, String>> players = new ArrayList<>(serverLobby.getPlayers());
sendPacketToAllClients(new LobbyPacket(players));
}
}
19 changes: 12 additions & 7 deletions src/main/java/com/lapisberry/net/packets/LobbyPacket.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<Pair<Integer, String>> players;

// Constructors
public LobbyPacket(LobbyController lobbyController) {
this.lobbyController = lobbyController;
public LobbyPacket(ArrayList<Pair<Integer, String>> players) {
this.players = players;
}

// Methods
@Override
public String toString() {
return "LobbyPacket{lobbyController=" + lobbyController + "}";
StringBuilder stringBuilder = new StringBuilder();
for (Pair<Integer, String> player : players) {
stringBuilder.append(player.getKey()).append("=").append(player.getValue()).append(", ");
}
return "LobbyPacket{lobbyController=" + stringBuilder + "}";
}

// Getters
public LobbyController getLobbyController() {
return lobbyController;
public ArrayList<Pair<Integer, String>> getPlayers() {
return players;
}
}

0 comments on commit 7f6d712

Please sign in to comment.