diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-06-18 16:43:22 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-06-18 16:43:22 +0200 |
commit | 395c25b1fbe51f42decf402f97e51eabcc8c0a77 (patch) | |
tree | 72a17c71a39b53d328a76e111ae205d9538d64d0 /Network | |
parent | 2686ec6d4af6b5437a5f5a5cac008f3d38295f78 (diff) |
Godot v3 -> v4
Diffstat (limited to 'Network')
-rw-r--r-- | Network/Lobby.gd | 20 | ||||
-rw-r--r-- | Network/Lobby.tscn | 176 | ||||
-rw-r--r-- | Network/Network.gd | 53 |
3 files changed, 123 insertions, 126 deletions
diff --git a/Network/Lobby.gd b/Network/Lobby.gd index be045b0..67a1f34 100644 --- a/Network/Lobby.gd +++ b/Network/Lobby.gd @@ -2,15 +2,17 @@ extends Control func _ready(): - Network.connect("connection_succeeded", self, "_on_connection_succeeded") - Network.connect("connection_failed", self, "_on_connection_failed") - Network.connect("player_list_changed", self, "refresh_lobby") - Network.connect("game_error", self, "_on_game_error") - Network.connect("game_ended", self, "_on_game_ended") - $HTTPRequest.connect("request_completed", self, '_set_remote_ip') + Network.connect("connection_succeeded", Callable(self, "_on_connection_succeeded")) + Network.connect("connection_failed", Callable(self, "_on_connection_failed")) + Network.connect("player_list_changed", Callable(self, "refresh_lobby")) + Network.connect("game_error", Callable(self, "_on_game_error")) + Network.connect("game_ended", Callable(self, "_on_game_ended")) + $HTTPRequest.connect("request_completed", Callable(self, '_set_remote_ip')) if OS.has_environment("USERNAME"): $Connect/Name.text = OS.get_environment("USERNAME") + elif OS.has_environment("USER"): + $Connect/Name.text = OS.get_environment("USER") else: var desktop_path = OS.get_system_dir(0).replace("\\", "/").split("/") $Connect/Name.text = desktop_path[desktop_path.size() - 2] @@ -36,7 +38,7 @@ func refresh_lobby(): for p in players: $Players/List.add_item(p.name) - $Players/Start.disabled = not get_tree().is_network_server() + $Players/Start.disabled = not multiplayer.is_server() func set_ip(): @@ -53,7 +55,7 @@ func set_remote_ip(): $Players/RemoteIP.text = "" $HTTPRequest.request("https://ipv4.icanhazip.com/") -func _set_remote_ip(result, response_code, headers, body): +func _set_remote_ip(_result, response_code, _headers, body): if response_code == 200: $Players/RemoteIP.text = body.get_string_from_utf8() else: @@ -73,7 +75,7 @@ func _on_connection_failed(): func _on_game_error(error): $ErrorDialog.dialog_text = error - $ErrorDialog.popup_centered_minsize() + $ErrorDialog.popup_centered_clamped() self.disable_connect_buttons() diff --git a/Network/Lobby.tscn b/Network/Lobby.tscn index d019cc0..f0b2622 100644 --- a/Network/Lobby.tscn +++ b/Network/Lobby.tscn @@ -1,141 +1,133 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=3 format=3 uid="uid://c72pr86s5ai6k"] -[ext_resource path="res://Network/Lobby.gd" type="Script" id=1] -[ext_resource path="res://UI/Menu/MenuButton.tscn" type="PackedScene" id=2] +[ext_resource type="Script" path="res://Network/Lobby.gd" id="1"] +[ext_resource type="PackedScene" uid="uid://b0fuyoa5jbxn0" path="res://UI/Menu/MenuButton.tscn" id="2"] [node name="Lobby" type="Control"] +layout_mode = 3 +anchors_preset = 15 anchor_right = 1.0 anchor_bottom = 1.0 -script = ExtResource( 1 ) -__meta__ = { -"_edit_use_anchors_": false -} +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1") [node name="Connect" type="Panel" parent="."] -margin_left = 424.0 -margin_top = 232.0 -margin_right = 600.0 -margin_bottom = 376.0 -__meta__ = { -"_edit_use_anchors_": false -} +layout_mode = 0 +offset_left = 424.0 +offset_top = 232.0 +offset_right = 600.0 +offset_bottom = 376.0 [node name="Name" type="LineEdit" parent="Connect"] -margin_left = 8.0 -margin_top = 8.0 -margin_right = 168.0 -margin_bottom = 32.0 +layout_mode = 0 +offset_left = 8.0 +offset_top = 8.0 +offset_right = 168.0 +offset_bottom = 32.0 placeholder_text = "Name" -__meta__ = { -"_edit_use_anchors_": false -} [node name="IP" type="LineEdit" parent="Connect"] -margin_left = 8.0 -margin_top = 40.0 -margin_right = 168.0 -margin_bottom = 64.0 +layout_mode = 0 +offset_left = 8.0 +offset_top = 40.0 +offset_right = 168.0 +offset_bottom = 64.0 placeholder_text = "IP" [node name="Host" type="Button" parent="Connect"] -margin_left = 8.0 -margin_top = 112.0 -margin_right = 64.0 -margin_bottom = 136.0 +layout_mode = 0 +offset_left = 8.0 +offset_top = 112.0 +offset_right = 64.0 +offset_bottom = 136.0 text = "Host" [node name="Join" type="Button" parent="Connect"] -margin_left = 112.0 -margin_top = 112.0 -margin_right = 168.0 -margin_bottom = 136.0 +layout_mode = 0 +offset_left = 112.0 +offset_top = 112.0 +offset_right = 168.0 +offset_bottom = 136.0 text = "Join" [node name="Error" type="Label" parent="Connect"] -margin_left = 8.0 -margin_top = 72.0 -margin_right = 168.0 -margin_bottom = 104.0 -custom_colors/font_color = Color( 1, 0.137255, 0.137255, 1 ) +layout_mode = 0 +offset_left = 8.0 +offset_top = 72.0 +offset_right = 168.0 +offset_bottom = 104.0 +theme_override_colors/font_color = Color(1, 0.137255, 0.137255, 1) [node name="Players" type="Panel" parent="."] visible = false -margin_left = 384.0 -margin_top = 48.0 -margin_right = 640.0 -margin_bottom = 512.0 -__meta__ = { -"_edit_use_anchors_": false -} +layout_mode = 0 +offset_left = 384.0 +offset_top = 48.0 +offset_right = 640.0 +offset_bottom = 512.0 [node name="Label" type="Label" parent="Players"] -margin_left = 16.0 -margin_top = 16.0 -margin_right = 64.0 -margin_bottom = 32.0 +layout_mode = 0 +offset_left = 16.0 +offset_top = 16.0 +offset_right = 64.0 +offset_bottom = 32.0 text = "Lobby" -__meta__ = { -"_edit_use_anchors_": false -} [node name="LocalIP" type="Label" parent="Players"] -margin_left = 128.0 -margin_top = 24.0 -margin_right = 240.0 -margin_bottom = 38.0 +layout_mode = 0 +offset_left = 128.0 +offset_top = 24.0 +offset_right = 240.0 +offset_bottom = 38.0 text = "000.000.000.000" -align = 2 -__meta__ = { -"_edit_use_anchors_": false -} [node name="RemoteIP" type="Label" parent="Players"] -margin_left = 128.0 -margin_top = 8.0 -margin_right = 240.0 -margin_bottom = 22.0 +layout_mode = 0 +offset_left = 128.0 +offset_top = 8.0 +offset_right = 240.0 +offset_bottom = 22.0 text = "000.000.000.000" -align = 2 -__meta__ = { -"_edit_use_anchors_": false -} [node name="List" type="ItemList" parent="Players"] -margin_left = 16.0 -margin_top = 48.0 -margin_right = 240.0 -margin_bottom = 400.0 -__meta__ = { -"_edit_use_anchors_": false -} +layout_mode = 0 +offset_left = 16.0 +offset_top = 48.0 +offset_right = 240.0 +offset_bottom = 400.0 [node name="Start" type="Button" parent="Players"] -margin_left = 64.0 -margin_top = 416.0 -margin_right = 192.0 -margin_bottom = 448.0 +layout_mode = 0 +offset_left = 64.0 +offset_top = 416.0 +offset_right = 192.0 +offset_bottom = 448.0 text = "START!" -__meta__ = { -"_edit_use_anchors_": false -} [node name="Back" type="Button" parent="Players"] -margin_left = 16.0 -margin_top = 416.0 -margin_right = 48.0 -margin_bottom = 448.0 +layout_mode = 0 +offset_left = 16.0 +offset_top = 416.0 +offset_right = 48.0 +offset_bottom = 448.0 text = "<" [node name="ErrorDialog" type="AcceptDialog" parent="."] -visible = true -margin_right = 83.0 -margin_bottom = 58.0 -[node name="MenuButton" parent="." instance=ExtResource( 2 )] -margin_left = 976.0 -margin_right = 0.0 +[node name="MenuButton" parent="." instance=ExtResource("2")] +layout_mode = 0 +anchors_preset = 0 +anchor_right = 0.0 +anchor_bottom = 0.0 +offset_left = 1.0 +offset_top = 1.0 +offset_right = 54.0 +offset_bottom = 32.0 [node name="HTTPRequest" type="HTTPRequest" parent="."] + [connection signal="pressed" from="Connect/Host" to="." method="_on_Host_pressed"] [connection signal="pressed" from="Connect/Join" to="." method="_on_Join_pressed"] [connection signal="pressed" from="Players/Start" to="." method="_on_Start_pressed"] diff --git a/Network/Network.gd b/Network/Network.gd index d58de75..0b3e379 100644 --- a/Network/Network.gd +++ b/Network/Network.gd @@ -30,51 +30,54 @@ var players_ready = [] func _ready(): - get_tree().connect("network_peer_connected", self, '_player_connected') - get_tree().connect("network_peer_disconnected", self,"_player_disconnected") - get_tree().connect("connected_to_server", self, "_connection_succeeded") - get_tree().connect("connection_failed", self, "_connection_failed") - get_tree().connect("server_disconnected", self, "_server_disconnected") - Global.connect("game_started", self, "_on_Global_game_started") - Global.connect("game_ended", self, "_on_Global_game_ended") - Global.connect("game_won", self, "_on_Global_game_won") - - -remote func register_player(name): - var id = get_tree().get_rpc_sender_id() + multiplayer.connect("peer_connected", Callable(self, '_player_connected')) + multiplayer.connect("peer_disconnected", Callable(self, "_player_disconnected")) + multiplayer.connect("connected_to_server", Callable(self, "_connection_succeeded")) + multiplayer.connect("connection_failed", Callable(self, "_connection_failed")) + multiplayer.connect("server_disconnected", Callable(self, "_server_disconnected")) + Global.connect("game_started", Callable(self, "_on_Global_game_started")) + Global.connect("game_ended", Callable(self, "_on_Global_game_ended")) + Global.connect("game_won", Callable(self, "_on_Global_game_won")) + + +@rpc("any_peer") func register_player(player_name): + var id = get_tree().get_remote_sender_id() self.players[id] = { - name = name, + name = player_name, ip = self.peer.get_peer_address(id), } emit_signal("player_list_changed") -remote func unregister_player(id): +@rpc("any_peer") func unregister_player(id): self.players.erase(id) emit_signal("player_list_changed") func host_game(player_name): self.player.name = player_name - self.peer = NetworkedMultiplayerENet.new() + self.peer = ENetMultiplayerPeer.new() self.peer.create_server(self.PORT, self.MAX_PEERS) - get_tree().set_network_peer(self.peer) + multiplayer.multiplayer_peer = self.peer + #get_tree().set_multiplayer_peer(self.peer) func join_game(ip, player_name): self.player.name = player_name - self.peer = NetworkedMultiplayerENet.new() + self.peer = ENetMultiplayerPeer.new() self.peer.create_client(ip, self.PORT) - get_tree().set_network_peer(self.peer) + multiplayer.multiplayer_peer = self.peer + #get_tree().set_multiplayer_peer(self.peer) func leave_game(): rpc("_leave_game") - get_tree().set_network_peer(null) + multiplayer.multiplayer_peer = null + #get_tree().set_multiplayer_peer(null) -remote func _leave_game(): - var id = get_tree().get_rpc_sender_id() +@rpc("any_peer") func _leave_game(): + var id = get_tree().get_remote_sender_id() self.peer.disconnect_peer(id) @@ -94,11 +97,11 @@ func _on_Global_game_started(): rpc("_start_game") -remote func _preconfigure_game(idx): +@rpc("any_peer") func _preconfigure_game(idx): self.player.idx = idx -sync func _start_game(): +@rpc("any_peer", "call_local") func _start_game(): emit_signal("game_started") @@ -111,7 +114,7 @@ func _on_Global_game_won(winning_player): rpc("_end_game", winning_player) -sync func _end_game(winning_player): +@rpc("any_peer", "call_local") func _end_game(winning_player): self.winning_player = winning_player emit_signal("game_ended") @@ -133,7 +136,7 @@ func _connection_succeeded(): func _connection_failed(): - get_tree().set_network_peer(null) # remove peer + get_tree().set_multiplayer_peer(null) # remove peer emit_signal("connection_failed") |