diff --git a/components/client_interface.gd b/components/client_interface.gd index 0027053..cf83ac4 100644 --- a/components/client_interface.gd +++ b/components/client_interface.gd @@ -72,3 +72,49 @@ func _on_web_client_tcp_client_joined_lobby(client_name: String) -> void: "client_name": client_name, "you": " (that's you!)" if client_name == name_input.text.strip_edges() else "" })) + + +func _on_web_client_tcp_connection_status_changed(new_status: WebClientTCP.ConnectionStatus) -> void: + match new_status: + WebClientTCP.ConnectionStatus.DISCONNECTED: + connect_button.disabled = false + create_lobby_button.disabled = true + join_lobby_button.disabled = true + leave_lobby_button.disabled = true + name_input.editable = true + lobby_code_input.editable = false + lobby_code_input.text = "" + + WebClientTCP.ConnectionStatus.ATTEMPTING_CONNECTION: + connect_button.disabled = true + create_lobby_button.disabled = true + join_lobby_button.disabled = true + leave_lobby_button.disabled = true + name_input.editable = true + lobby_code_input.editable = false + lobby_code_input.text = "" + + WebClientTCP.ConnectionStatus.LOBBYLESS: + connect_button.disabled = true + create_lobby_button.disabled = false + join_lobby_button.disabled = false + leave_lobby_button.disabled = true + name_input.editable = true + lobby_code_input.editable = true + lobby_code_input.text = "" + + WebClientTCP.ConnectionStatus.ATTEMPTING_JOIN: + connect_button.disabled = true + create_lobby_button.disabled = true + join_lobby_button.disabled = true + leave_lobby_button.disabled = true + name_input.editable = false + lobby_code_input.editable = false + + WebClientTCP.ConnectionStatus.JOINED: + connect_button.disabled = true + create_lobby_button.disabled = true + join_lobby_button.disabled = true + leave_lobby_button.disabled = false + name_input.editable = false + lobby_code_input.editable = false diff --git a/components/web_client_tcp.gd b/components/web_client_tcp.gd index 1d61030..a090ae0 100644 --- a/components/web_client_tcp.gd +++ b/components/web_client_tcp.gd @@ -24,6 +24,16 @@ signal disconnected_from_lobby() signal client_joined_lobby(client_name: String) +signal connection_status_changed(new_status: ConnectionStatus) + +enum ConnectionStatus { + DISCONNECTED, + ATTEMPTING_CONNECTION, + LOBBYLESS, # == connected + ATTEMPTING_JOIN, + JOINED, +} + func _process(delta: float) -> void: _tcp.poll() _new_status = _tcp.get_status() @@ -31,15 +41,17 @@ func _process(delta: float) -> void: if not _processed_status == _new_status: match _new_status: StreamPeerTCP.Status.STATUS_NONE: - pass + connection_status_changed.emit(ConnectionStatus.DISCONNECTED) StreamPeerTCP.Status.STATUS_CONNECTING: - pass + connection_status_changed.emit(ConnectionStatus.ATTEMPTING_CONNECTION) StreamPeerTCP.Status.STATUS_CONNECTED: _thread = Thread.new() _thread.start(_listen_for_data) connected_to_server.emit() + connection_status_changed.emit(ConnectionStatus.LOBBYLESS) StreamPeerTCP.Status.STATUS_ERROR: connection_error_occurred.emit() + connection_status_changed.emit(ConnectionStatus.DISCONNECTED) _processed_status = _new_status @@ -59,9 +71,11 @@ func _handle_response(response: String) -> void: "create_lobby": if data["status"] == "ok": connected_to_lobby.emit.call_deferred(_lobby_id) + connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED) "join_lobby": if data["status"] == "ok": connected_to_lobby.emit.call_deferred(_lobby_id) + connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED) "client_joined_lobby": client_joined_lobby.emit.call_deferred(data["new_client_name"]) @@ -79,6 +93,7 @@ func disconnect_from_server() -> void: if _thread: _thread.wait_to_finish() disconnected_from_server.emit() + connection_status_changed.emit(ConnectionStatus.DISCONNECTED) func create_lobby(client_name: String) -> void: _lobby_id = _generate_code() @@ -102,6 +117,7 @@ func leave_lobby() -> void: _client_name = "" disconnected_from_lobby.emit() + connection_status_changed.emit(ConnectionStatus.LOBBYLESS) func _send_message(message: String) -> void: _tcp.put_data(message.to_utf8_buffer()) diff --git a/game.tscn b/game.tscn index be0a97c..8f3801a 100644 --- a/game.tscn +++ b/game.tscn @@ -88,6 +88,7 @@ autowrap_mode = 2 [connection signal="connected_to_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_lobby"] [connection signal="connected_to_server" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connected_to_server"] [connection signal="connection_error_occurred" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connection_error_occurred"] +[connection signal="connection_status_changed" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_connection_status_changed"] [connection signal="disconnected_from_lobby" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_disconnected_from_lobby"] [connection signal="disconnected_from_server" from="WebClientTCP" to="ClientInterface" method="_on_web_client_tcp_disconnected_from_server"] [connection signal="pressed" from="ClientInterface/HBoxContainer/VBoxContainer/ConnectButton" to="ClientInterface" method="_on_connect_button_pressed"]