diff options
| author | metamuffin <metamuffin@disroot.org> | 2024-06-24 20:35:26 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2024-06-24 20:35:26 +0200 | 
| commit | 7c15d7c2ae45c1694cb6b6def98ad5c79a414d08 (patch) | |
| tree | 100fe0d9265a3eeee0972b8590d3126e4e5efbd3 | |
| parent | dbb52c813e64fbe4e733dbfec4e29889d288873e (diff) | |
| download | hurrycurry-7c15d7c2ae45c1694cb6b6def98ad5c79a414d08.tar hurrycurry-7c15d7c2ae45c1694cb6b6def98ad5c79a414d08.tar.bz2 hurrycurry-7c15d7c2ae45c1694cb6b6def98ad5c79a414d08.tar.zst | |
show websocket errors
| -rw-r--r-- | client/game.gd | 32 | ||||
| -rw-r--r-- | client/global.gd | 1 | ||||
| -rw-r--r-- | client/menu/error_menu.gd | 7 | ||||
| -rw-r--r-- | client/menu/error_menu.tscn | 66 | ||||
| -rw-r--r-- | client/menu/ingame_menu.tscn | 2 | ||||
| -rw-r--r-- | client/multiplayer.gd | 20 | 
6 files changed, 101 insertions, 27 deletions
| diff --git a/client/game.gd b/client/game.gd index 67b92679..ad9467ca 100644 --- a/client/game.gd +++ b/client/game.gd @@ -24,30 +24,32 @@ extends Node3D  var marker_target = Vector3(0,0,0)  var players := {} -# Called when the node enters the scene tree for the first time.  func _ready(): +	mp.connect("connection_closed", func(reason: String): +		Global.error_message = reason; +		$SceneTransition.transition_to("res://menu/error_menu.tscn") +	) +	  	await mp.init  	if mp.player_id == -1:  		push_error("multiplayer has not been initialized") -	mp.connect("add_player", -		func(player: int, player_name: String, pos: Vector2, character: int): -			var player_instance: Player -			if player == mp.player_id: -				player_instance = ControllablePlayer.new(player, player_name, pos, character, self) -				camera.target = player_instance -			else: -				player_instance = Player.new(player, player_name, pos, character, self) -			players[player] = player_instance -			add_child(player_instance) +	mp.connect("add_player", func(player: int, player_name: String, pos: Vector2, character: int): +		var player_instance: Player +		if player == mp.player_id: +			player_instance = ControllablePlayer.new(player, player_name, pos, character, self) +			camera.target = player_instance +		else: +			player_instance = Player.new(player, player_name, pos, character, self) +		players[player] = player_instance +		add_child(player_instance)  	)  	mp.connect("update_map", map.update) -	mp.connect("position", -		func(player: int, pos: Vector2, rot: float): -			var player_instance: Player = players[player] -			player_instance.update_position(pos, rot) +	mp.connect("position", func(player: int, pos: Vector2, rot: float): +		var player_instance: Player = players[player] +		player_instance.update_position(pos, rot)  	)  	mp.connect("remove_player", func(id: int): diff --git a/client/global.gd b/client/global.gd index c0f9cc57..c75d12f3 100644 --- a/client/global.gd +++ b/client/global.gd @@ -2,3 +2,4 @@ extends Node  var server_url = ""  var character = 1 +var error_message = "" diff --git a/client/menu/error_menu.gd b/client/menu/error_menu.gd new file mode 100644 index 00000000..9938ee9d --- /dev/null +++ b/client/menu/error_menu.gd @@ -0,0 +1,7 @@ +extends Control + +func _ready(): +	$Panel/contents/mesage.text = Global.error_message + +func _on_return_pressed(): +	$SceneTransition.transition_to("res://menu/main_menu.tscn") diff --git a/client/menu/error_menu.tscn b/client/menu/error_menu.tscn new file mode 100644 index 00000000..0b86cceb --- /dev/null +++ b/client/menu/error_menu.tscn @@ -0,0 +1,66 @@ +[gd_scene load_steps=5 format=3 uid="uid://cimgn07lbcs4v"] + +[ext_resource type="Theme" uid="uid://b0qmvo504e457" path="res://menu/theme.tres" id="1_cabdu"] +[ext_resource type="PackedScene" uid="uid://l4vm07dtda4j" path="res://menu/menu_background.tscn" id="2_5fxol"] +[ext_resource type="Script" path="res://menu/error_menu.gd" id="2_dbe41"] +[ext_resource type="PackedScene" uid="uid://bg2d78ycorcqk" path="res://menu/scene_transition.tscn" id="4_1nbt3"] + +[node name="ErrorMenu" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +theme = ExtResource("1_cabdu") +script = ExtResource("2_dbe41") + +[node name="MenuBackground" parent="." instance=ExtResource("2_5fxol")] + +[node name="Panel" type="Panel" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="contents" type="VBoxContainer" parent="Panel"] +layout_mode = 1 +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -176.5 +offset_top = -49.5 +offset_right = 176.5 +offset_bottom = 49.5 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="title" type="Label" parent="Panel/contents"] +layout_mode = 2 +theme_override_font_sizes/font_size = 61 +text = "Error" +horizontal_alignment = 1 + +[node name="mesage" type="Label" parent="Panel/contents"] +layout_mode = 2 +theme_override_font_sizes/font_size = 24 +text = "This should be the error message." + +[node name="Control" type="Control" parent="Panel/contents"] +custom_minimum_size = Vector2(0, 15.805) +layout_mode = 2 + +[node name="return" type="Button" parent="Panel/contents"] +layout_mode = 2 +size_flags_horizontal = 4 +text = "Return to Main Menu" + +[node name="SceneTransition" parent="." instance=ExtResource("4_1nbt3")] +visible = false +layout_mode = 1 + +[connection signal="pressed" from="Panel/contents/return" to="." method="_on_return_pressed"] diff --git a/client/menu/ingame_menu.tscn b/client/menu/ingame_menu.tscn index 7f2bfa5d..da0767ff 100644 --- a/client/menu/ingame_menu.tscn +++ b/client/menu/ingame_menu.tscn @@ -72,7 +72,7 @@ anchors_preset = 9  anchor_bottom = 1.0  offset_left = -400.0  offset_right = -90.0 -offset_bottom = 1944.0 +offset_bottom = 3888.0  grow_vertical = 2  [node name="margin" type="MarginContainer" parent="side"] diff --git a/client/multiplayer.gd b/client/multiplayer.gd index 2d4b9533..b9242a41 100644 --- a/client/multiplayer.gd +++ b/client/multiplayer.gd @@ -2,19 +2,19 @@  # Copyright 2024 nokoe  # Copyright 2024 metamuffin  # Copyright 2024 tpart -#  +#  # This program is free software: you can redistribute it and/or modify  # it under the terms of the GNU Affero General Public License as published by  # the Free Software Foundation, version 3 of the License only. -#  +#  # This program is distributed in the hope that it will be useful,  # but WITHOUT ANY WARRANTY; without even the implied warranty of  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  # GNU Affero General Public License for more details. -#  +#  # You should have received a copy of the GNU Affero General Public License  # along with this program.  If not, see <https://www.gnu.org/licenses/>. -#  +#  class_name Multiplayer  extends Node @@ -35,6 +35,8 @@ signal remove_player_item(player: int)  signal set_progress(tile: Vector2i, progress: float, warn: bool)  signal set_finished(tile: Vector2i, warn: bool) +signal connection_closed(reason: String) +  var connected := false  var socket := WebSocketPeer.new() @@ -51,6 +53,7 @@ func _ready():  	print("Multiplayer connect");  	socket.connect_to_url(Global.server_url)  	connected = true +  func _notification(what):  	if what == NOTIFICATION_PREDELETE:  		print("Multiplayer disconnect"); @@ -64,16 +67,11 @@ func _process(_delta):  		if state == WebSocketPeer.STATE_OPEN:  			while socket.get_available_packet_count():  				handle_packet(socket.get_packet()) -		elif state == WebSocketPeer.STATE_CONNECTING: -			print("connecting") -		elif state == WebSocketPeer.STATE_CLOSING: -			# Keep polling to achieve proper close. -			print("closing") -			pass  		elif state == WebSocketPeer.STATE_CLOSED:  			var code = socket.get_close_code()  			var reason = socket.get_close_reason() -			print("WebSocket closed with code: %d, reason %s. Clean: %s" % [code, reason, code != - 1]) +			emit_signal("connection_closed", "WebSocket closed with code: %d, reason %s. Clean: %s" % [code, reason, code != -1]) +			self.queue_free()  func handle_packet(bytes: PackedByteArray):  	var decoded = decode_packet(bytes) | 
