Refactor to fit game-shell structure; externalized lobby client code

This commit is contained in:
2026-08-23 12:55:53 -05:00
parent 2a99880a98
commit 9a067711c7
24 changed files with 81 additions and 625 deletions

View File

@@ -1,112 +0,0 @@
class_name Lobby extends LobbyBase
func _ready() -> void:
setup_signals()
host_address_input.grab_focus()
func _exit_tree() -> void:
# TODO: Maybe no longer needed...
if Globals.multiplayer_data.is_multiplayer_active: return
client._do_wait_close_connection()
func setup_signals() -> void:
MessageBus.subscribe("lobby_wait_for_connection", client._do_wait_for_connection)
MessageBus.subscribe("lobby_close_connection", client._do_wait_close_connection)
MessageBus.subscribe("receive_match_list", _on_receive_match_list)
MessageBus.subscribe("match_list_activate_entry", _on_match_list_entry_activated)
MessageBus.subscribe("match_list_add_entry", _on_match_list_entry_added)
MessageBus.subscribe("match_list_remove_entry", _on_match_list_entry_removed)
MessageBus.subscribe("match_list_entry_load_match", _on_match_list_entry_load_match)
func _server_host_started() -> void:
host_connect_bttn.visible = false
# NOTE: 'matches_list' can be empty; so long as we get the call from the
# server we know we are connected and thus hide respective UI elements.
func _on_receive_match_list(matches_list: Array) -> void:
push_warning("Client Joined Host: Full Match List Recieved...\n", matches_list)
match_create_vbox.visible = true
search_vbox.visible = true
host_disconnect_bttn.visible = true
host_connect_bttn.visible = false
for match_entry in matches_list:
_create_match_entry(match_entry)
client.lobby_fully_connected = true
new_match_name_input.grab_focus()
func _on_match_list_entry_activated(match_id: String) -> void:
for match_entry in match_list.get_children():
match_entry.join_bttn.visible = false
if not match_entry.match_id == match_id: continue
active_match = match_entry
match_entry.join_bttn.text = "Delete"
match_entry.join_bttn.visible = true
match_create_vbox.visible = false
match_search_input.visible = false
match_search_bttn.visible = false
match_entry.join_bttn.pressed.disconnect(_join_match)
match_entry.join_bttn.pressed.connect(
_delete_match.bind(match_entry.match_id)
)
func _on_match_list_entry_added(match_entry: Dictionary) -> void:
push_warning("Client: Added Match List Entry...", match_entry)
_create_match_entry(match_entry)
func _on_match_list_entry_removed(match_id: String) -> void:
push_warning("Client: Removed Match List Entry...", match_id)
if active_match && (match_id == active_match.match_id):
active_match = null
match_create_vbox.visible = true
match_search_input.visible = true
match_search_bttn.visible = false
new_match_name_input.grab_focus()
for match_entry in match_list.get_children():
if not active_match:
match_entry.join_bttn.visible = true
if not match_id == match_entry.match_id: continue
match_entry.queue_free()
func _on_match_list_entry_load_match(match_entry: Dictionary, is_player_1: bool) -> void:
push_warning(
"Client: Loading Game Match...\nMatch Data: ", match_entry, "\nis_player_1: ", is_player_1
)
Globals.multiplayer_data.set_multiplayer_active()
Globals.game_data.set_game_mode(match_entry.type)
Globals.game_data.set_player_id(is_player_1)
Globals.game_state.set_game_scene("res://scenes/game.tscn")
func _create_match_entry(match_entry: Dictionary) -> void:
var container = MATCH_ENTRY.instantiate()
match_list.add_child(container)
container.name_lbl.text = match_entry.name
container.type_lbl.text = match_entry.type
container.match_id = match_entry.match_id
container.join_bttn.pressed.connect(
_join_match.bind(match_entry.match_id)
)
if active_match:
container.join_bttn.visible = false
func _join_match(match_id: String) -> void:
MessageBus.emit_request.rpc_id(1, "match_list_join_entry", match_id)
func _delete_match(match_id: String) -> void:
MessageBus.emit_request.rpc_id(1, "match_list_remove_entry", match_id)

View File

@@ -1 +0,0 @@
uid://dk7afnf7ol7dy

View File

@@ -1,42 +0,0 @@
[gd_scene load_steps=4 format=3 uid="uid://dkes5cdu45q6e"]
[ext_resource type="Script" uid="uid://dk7afnf7ol7dy" path="res://scenes/screens/lobby_screen/lobby.gd" id="1_hhiik"]
[ext_resource type="PackedScene" uid="uid://dg7pgj6i5pk8p" path="res://scenes/ui/lobby/lobby_ui.tscn" id="2_lqt48"]
[ext_resource type="Script" uid="uid://dv7j35h2ngiq7" path="res://scenes/screens/lobby_screen/lobby_client.gd" id="4_glu7g"]
[node name="lobby" type="Node"]
script = ExtResource("1_hhiik")
[node name="lobby_ui" parent="." instance=ExtResource("2_lqt48")]
[node name="host_address_input" parent="lobby_ui/body/left_body/server_host_hbox" index="1"]
context_menu_enabled = false
select_all_on_focus = true
[node name="host_port_range" parent="lobby_ui/body/left_body/server_host_hbox" index="2"]
select_all_on_focus = true
[node name="match_search_input" parent="lobby_ui/body/left_body/search_vbox/search_hbox" index="0"]
context_menu_enabled = false
[node name="right_body" parent="lobby_ui/body" index="1"]
visible = false
[node name="new_match_name_input" parent="lobby_ui/body/right_body/create_match_hbox" index="0"]
context_menu_enabled = false
select_all_on_focus = true
[node name="client" type="Node" parent="."]
script = ExtResource("4_glu7g")
[connection signal="pressed" from="lobby_ui/body/left_body/server_host_hbox/home_bttn" to="." method="_on_home_bttn_pressed"]
[connection signal="pressed" from="lobby_ui/body/left_body/server_host_hbox/host_connect_bttn" to="." method="_on_host_connect_bttn_pressed"]
[connection signal="pressed" from="lobby_ui/body/left_body/server_host_hbox/host_disconnect_bttn" to="." method="_on_host_disconnect_bttn_pressed"]
[connection signal="pressed" from="lobby_ui/body/left_body/search_vbox/search_hbox/match_search_bttn" to="." method="_on_match_search_bttn_pressed"]
[connection signal="pressed" from="lobby_ui/body/right_body/create_match_hbox/match_create_bttn" to="." method="_on_match_create_bttn_pressed"]
[connection signal="pressed" from="lobby_ui/body/right_body/vbox/8ball" to="." method="_on_mode_bttn_pressed" flags=18]
[connection signal="pressed" from="lobby_ui/body/right_body/vbox/9ball" to="." method="_on_mode_bttn_pressed" flags=18]
[connection signal="pressed" from="lobby_ui/body/right_body/vbox/snooker" to="." method="_on_mode_bttn_pressed" flags=18]
[connection signal="pressed" from="lobby_ui/body/right_body/vbox/freeball" to="." method="_on_mode_bttn_pressed" flags=18]
[editable path="lobby_ui"]

View File

@@ -1,62 +0,0 @@
class_name LobbyBase extends Node
const MATCH_ENTRY = preload("res://scenes/screens/lobby_screen/match_entry.tscn")
@onready var host_address_input: LineEdit = $lobby_ui/body/left_body/server_host_hbox/host_address_input
@onready var host_port_range: SpinBox = $lobby_ui/body/left_body/server_host_hbox/host_port_range
@onready var host_connect_bttn: Button = $lobby_ui/body/left_body/server_host_hbox/host_connect_bttn
@onready var host_disconnect_bttn: Button = $lobby_ui/body/left_body/server_host_hbox/host_disconnect_bttn
@onready var search_vbox: VBoxContainer = $lobby_ui/body/left_body/search_vbox
@onready var match_search_input: LineEdit = $lobby_ui/body/left_body/search_vbox/search_hbox/match_search_input
@onready var match_search_bttn: Button = $lobby_ui/body/left_body/search_vbox/search_hbox/match_search_bttn
@onready var match_list: VBoxContainer = $lobby_ui/body/left_body/search_vbox/scroll_container/match_list
@onready var match_create_vbox: VBoxContainer = $lobby_ui/body/right_body
@onready var new_match_name_input: LineEdit = $lobby_ui/body/right_body/create_match_hbox/new_match_name_input
@onready var client: LobbyClient = $client
var active_mode: String = "freeball"
var active_match: HBoxContainer = null
func _on_home_bttn_pressed() -> void:
Globals.game_state.set_game_scene(
"res://scenes/screens/start_screen/start.tscn"
)
func _on_host_connect_bttn_pressed() -> void:
Globals.game_data.game_address = host_address_input.text
Globals.game_data.game_port = int(host_port_range.value)
client.start_client(
Globals.game_data.game_address, Globals.game_data.game_port
)
func _on_host_disconnect_bttn_pressed() -> void:
match_create_vbox.visible = false
search_vbox.visible = false
host_disconnect_bttn.visible = false
host_connect_bttn.visible = true
for match_block in match_list.get_children():
match_block.queue_free()
client.close_connection()
func _on_match_search_bttn_pressed() -> void:
pass
func _on_match_create_bttn_pressed() -> void:
if active_match: return
var match_entry: Dictionary = {
"name": new_match_name_input.text,
"type": active_mode
}
MessageBus.emit_request.rpc("match_list_add_entry", match_entry)
func _on_mode_bttn_pressed(button: Button) -> void:
active_mode = button.name.to_lower()

View File

@@ -1 +0,0 @@
uid://5tvhtoe6fdu1

View File

@@ -1,35 +0,0 @@
class_name LobbyClient extends ClientNetworking
var lobby_fully_connected: bool = false
func _ready() -> void:
pass
func _process(_delta: float) -> void:
pass
func _do_wait_for_connection() -> void:
_wait_for_connection()
while not lobby_fully_connected:
await get_tree().process_frame
func _do_wait_close_connection():
_wait_close_connection()
lobby_fully_connected = false
func client_connected_to_server() -> void:
push_warning("Connected to server!")
func client_disconnected_from_server() -> void:
push_warning("Disconnected from, server...")
func client_connection_failed_to_server() -> void:
push_warning("Connection failed!")
unset_client()

View File

@@ -1 +0,0 @@
uid://dv7j35h2ngiq7

View File

@@ -1,8 +0,0 @@
class_name MatchEntry extends HBoxContainer
@onready var name_lbl: Label = $name_lbl
@onready var type_lbl: Label = $type_lbl
@onready var join_bttn: Button = $join_bttn
var match_id: String = ""

View File

@@ -1 +0,0 @@
uid://ck663xy8rqumo

View File

@@ -1,23 +0,0 @@
[gd_scene load_steps=2 format=3 uid="uid://b5b18vkfe3caa"]
[ext_resource type="Script" uid="uid://ck663xy8rqumo" path="res://scenes/screens/lobby_screen/match_entry.gd" id="1_kw53p"]
[node name="match_entry" type="HBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_kw53p")
[node name="name_lbl" type="Label" parent="."]
layout_mode = 2
size_flags_horizontal = 3
[node name="type_lbl" type="Label" parent="."]
layout_mode = 2
size_flags_horizontal = 3
[node name="join_bttn" type="Button" parent="."]
layout_mode = 2
text = "Join"