From 4ac3fbd962f6bd596a90b369d9e43a21762298a6 Mon Sep 17 00:00:00 2001 From: denizk0461 Date: Mon, 7 Jul 2025 22:11:02 +0200 Subject: [PATCH] client now displays currently connected players by name --- components/client_interface.gd | 12 +++++++++--- components/web_client_tcp.gd | 4 ++-- game.tscn | 11 ++++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/components/client_interface.gd b/components/client_interface.gd index 548e5cc..ee76aa4 100644 --- a/components/client_interface.gd +++ b/components/client_interface.gd @@ -11,6 +11,8 @@ extends Control @export var chat_message_input: LineEdit @export var send_chat_message_button: Button +@export var connected_players_label: Label + @export var name_input: LineEdit @export var lobby_code_input: LineEdit @@ -54,11 +56,12 @@ func _on_web_client_tcp_connected_to_lobby(lobby_id: String) -> void: func _on_web_client_tcp_disconnected_from_lobby() -> void: _write_line("You disconnected from the lobby") -func _on_web_client_tcp_client_joined_lobby(client_name: String, currently_connected_client_names: Array) -> void: +func _on_web_client_tcp_client_joined_lobby(client_name: String, connected_client_names: Array) -> void: _write_line("{client_name} joined the lobby{you}".format({ "client_name": client_name, "you": " (that's you!)" if client_name == name_input.text.strip_edges() else "" })) + _update_connected_players(connected_client_names) func _on_web_client_tcp_received_chat_message(client_name: String, message: String) -> void: _write_line("<{client_name}> {message}".format({ @@ -66,8 +69,9 @@ func _on_web_client_tcp_received_chat_message(client_name: String, message: Stri "message": message, })) -func _on_web_client_tcp_client_left_lobby(client_name: String, currently_connected_client_names: Array) -> void: +func _on_web_client_tcp_client_left_lobby(client_name: String, connected_client_names: Array) -> void: _write_line("{client_name} left the lobby".format({"client_name": client_name})) + _update_connected_players(connected_client_names) func _on_web_client_tcp_connection_status_changed(new_status: WebClientTCP.ConnectionStatus) -> void: match new_status: @@ -124,6 +128,8 @@ func _on_web_client_tcp_connection_status_changed(new_status: WebClientTCP.Conne chat_message_input.editable = true send_chat_message_button.disabled = false - func _on_web_client_tcp_received_connection_message(message: String) -> void: _write_line(message) + +func _update_connected_players(new_client_names: Array[String]) -> void: + connected_players_label.text = "Connected Players:\n" + "\n".join(new_client_names) diff --git a/components/web_client_tcp.gd b/components/web_client_tcp.gd index b1357e6..0f975c7 100644 --- a/components/web_client_tcp.gd +++ b/components/web_client_tcp.gd @@ -26,8 +26,8 @@ signal disconnected_from_server() signal connected_to_lobby(lobby_id: String) signal disconnected_from_lobby() -signal client_joined_lobby(client_name: String, currently_connected_client_names: Array[String]) -signal client_left_lobby(client_name: String, currently_connected_client_names: Array[String]) +signal client_joined_lobby(client_name: String, connected_client_names: Array[String]) +signal client_left_lobby(client_name: String, connected_client_names: Array[String]) signal received_connection_message(message: String) signal received_chat_message(client_name: String, message: String) diff --git a/game.tscn b/game.tscn index ccb4c23..54f0910 100644 --- a/game.tscn +++ b/game.tscn @@ -20,7 +20,7 @@ anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 -[node name="ClientInterface" type="ColorRect" parent="HBoxContainer" node_paths=PackedStringArray("client", "connect_button", "create_lobby_button", "join_lobby_button", "leave_lobby_button", "chat_message_input", "send_chat_message_button", "name_input", "lobby_code_input", "output")] +[node name="ClientInterface" type="ColorRect" parent="HBoxContainer" node_paths=PackedStringArray("client", "connect_button", "create_lobby_button", "join_lobby_button", "leave_lobby_button", "chat_message_input", "send_chat_message_button", "connected_players_label", "name_input", "lobby_code_input", "output")] custom_minimum_size = Vector2(316, 0) layout_mode = 2 color = Color(0.26, 0.2236, 0.230273, 1) @@ -32,6 +32,7 @@ join_lobby_button = NodePath("MarginContainer/VBoxContainer/ServerInputs/JoinLob leave_lobby_button = NodePath("MarginContainer/VBoxContainer/ServerInputs/LeaveLobbyButton") chat_message_input = NodePath("MarginContainer/VBoxContainer/ChatContainer/MessageInput") send_chat_message_button = NodePath("MarginContainer/VBoxContainer/ChatContainer/SendMessageButton") +connected_players_label = NodePath("MarginContainer/VBoxContainer/ConnectedPlayersLabel") name_input = NodePath("MarginContainer/VBoxContainer/ServerInputs/NameInput") lobby_code_input = NodePath("MarginContainer/VBoxContainer/ServerInputs/LobbyCodeInput") output = NodePath("MarginContainer/VBoxContainer/ScrollContainer/Output") @@ -97,6 +98,14 @@ layout_mode = 2 disabled = true text = "Disconnect" +[node name="ConnectedPlayersLabel" type="Label" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer"] +layout_mode = 2 + +[node name="Divider" type="ColorRect" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer"] +custom_minimum_size = Vector2(0, 2) +layout_mode = 2 +color = Color(0.55, 0.3905, 0.3905, 1) + [node name="ScrollContainer" type="ScrollContainer" parent="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer"] layout_mode = 2 size_flags_vertical = 3