class_name EscapeMenuMultiplayer extends CenterContainer # TODO: Display and update player count. const STATUS_DEFAULT := [ "No connection" , Color.ORANGE ] const STATUS_CLIENT_CONNECTING := [ "Connecting ..." , Color.YELLOW ] const STATUS_CLIENT_CONNECTED := [ "Connected (%d player%s)" , Color.GREEN ] const STATUS_CLIENT_FAILED_TO_CONNECT := [ "Failed to connect" , Color.RED ] const STATUS_CLIENT_DISCONNECTED := [ "Disconnected" , Color.ORANGE ] const STATUS_SERVER_DISCONNECTED := [ "Server disconnected" , Color.ORANGE ] const STATUS_SERVER_RUNNING := [ "Server running (%d player%s)" , Color.GREEN ] const STATUS_SERVER_STOPPED := [ "Server stopped" , Color.ORANGE ] const STATUS_ERROR_ALREADY_IN_USE := [ "Already in use" , Color.RED ] const STATUS_ERROR_CANT_CREATE := [ "Cant create" , Color.RED ] @onready var status_label : Label = %Status @onready var port_box : SpinBox = %Port @onready var address_edit : LineEdit = %Address @onready var open_close_button : Button = %OpenClose @onready var dis_connect_button : Button = %DisConnect func _ready() -> void: _update_status(STATUS_DEFAULT) Game.NETWORK.status_changed.connect(_on_status_changed) func _on_hide_address_toggled(toggled_on: bool) -> void: address_edit.secret = toggled_on func _on_status_changed(new_status: Network.Status, old_status: Network.Status) -> void: match new_status: Network.Status.SINGLEPLAYER: match old_status: Network.Status.CLIENT_CONNECTING: _update_status(STATUS_CLIENT_FAILED_TO_CONNECT) Network.Status.CLIENT_CONNECTED : _update_status(STATUS_SERVER_DISCONNECTED) Network.Status.SERVER_RUNNING : _update_status(STATUS_SERVER_STOPPED) open_close_button.text = "Open Server" dis_connect_button.disabled = false dis_connect_button.text = "Connect" open_close_button.disabled = false Network.Status.CLIENT_CONNECTING: _update_status(STATUS_CLIENT_CONNECTING) dis_connect_button.text = "Disconnect" open_close_button.disabled = true Network.Status.CLIENT_CONNECTED: _update_status(STATUS_CLIENT_CONNECTED) Network.Status.SERVER_RUNNING: _update_status(STATUS_SERVER_RUNNING) open_close_button.text = "Close Server" dis_connect_button.disabled = true func _on_open_close_pressed() -> void: if open_close_button.text == "Open Server": match Game.NETWORK.start_server(int(port_box.value)): ERR_ALREADY_IN_USE: _update_status(STATUS_ERROR_ALREADY_IN_USE) ERR_CANT_CREATE : _update_status(STATUS_ERROR_CANT_CREATE) else: Game.NETWORK.stop_server() func _on_dis_connect_pressed() -> void: if dis_connect_button.text == "Connect": var address := address_edit.text if address_edit.text else "localhost" if Game.NETWORK.connect_to_server(address): _update_status(STATUS_ERROR_CANT_CREATE) else: Game.NETWORK.disconnect_from_server() # Without this it would (incorrectly?) display "failed to connect". _update_status(STATUS_CLIENT_DISCONNECTED) func _update_status(status: Array) -> void: status_label.text = status[0] status_label.modulate = status[1]