summaryrefslogtreecommitdiff
path: root/Network
diff options
context:
space:
mode:
Diffstat (limited to 'Network')
-rw-r--r--Network/Lobby.gd67
-rw-r--r--Network/Lobby.tscn64
-rw-r--r--Network/Network.gd6
3 files changed, 77 insertions, 60 deletions
diff --git a/Network/Lobby.gd b/Network/Lobby.gd
index 67a1f34..80b663b 100644
--- a/Network/Lobby.gd
+++ b/Network/Lobby.gd
@@ -1,6 +1,12 @@
extends Control
+var ConnectNodeContainer: Node
+var ConnectNode: Node
+var PlayersNodeContainer: Node
+var PlayersNode: Node
+
+
func _ready():
Network.connect("connection_succeeded", Callable(self, "_on_connection_succeeded"))
Network.connect("connection_failed", Callable(self, "_on_connection_failed"))
@@ -9,13 +15,18 @@ func _ready():
Network.connect("game_ended", Callable(self, "_on_game_ended"))
$HTTPRequest.connect("request_completed", Callable(self, '_set_remote_ip'))
+ ConnectNodeContainer = $CenterContainer
+ ConnectNode = $CenterContainer/Connect
+ PlayersNodeContainer = $CenterContainer2
+ PlayersNode = $CenterContainer2/Players
+
if OS.has_environment("USERNAME"):
- $Connect/Name.text = OS.get_environment("USERNAME")
+ ConnectNode.get_node("Name").text = OS.get_environment("USERNAME")
elif OS.has_environment("USER"):
- $Connect/Name.text = OS.get_environment("USER")
+ ConnectNode.get_node("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]
+ ConnectNode.get_node("Name").text = desktop_path[desktop_path.size() - 2]
# show lobby if we're already connected somewhere
if Network.peer != null:
@@ -25,20 +36,20 @@ func _ready():
func disable_connect_buttons(is_disabled = true):
- $Connect/Host.disabled = is_disabled
- $Connect/Join.disabled = is_disabled
+ ConnectNode.get_node("Host").disabled = is_disabled
+ ConnectNode.get_node("Join").disabled = is_disabled
func refresh_lobby():
var players = Network.players.values()
var player = Network.player
- $Players/List.clear()
- $Players/List.add_item(player.name + " (You)")
+ PlayersNode.get_node("List").clear()
+ PlayersNode.get_node("List").add_item(player.name + " (You)")
for p in players:
- $Players/List.add_item(p.name)
+ PlayersNode.get_node("List").add_item(p.name)
- $Players/Start.disabled = not multiplayer.is_server()
+ PlayersNode.get_node("Start").disabled = not multiplayer.is_server()
func set_ip():
@@ -48,29 +59,29 @@ func set_ip():
func set_local_ip():
for ip in IP.get_local_addresses():
if ip.begins_with("192.168"):
- $Players/LocalIP.text = ip
+ PlayersNode.get_node("LocalIP").text = ip
break
func set_remote_ip():
- $Players/RemoteIP.text = ""
+ PlayersNode.get_node("RemoteIP").text = ""
$HTTPRequest.request("https://ipv4.icanhazip.com/")
func _set_remote_ip(_result, response_code, _headers, body):
if response_code == 200:
- $Players/RemoteIP.text = body.get_string_from_utf8()
+ PlayersNode.get_node("RemoteIP").text = body.get_string_from_utf8()
else:
- $Players/RemoteIP.text = "Remote IP request failed!"
+ PlayersNode.get_node("RemoteIP").text = "Remote IP request failed!"
self._on_game_error("Remote IP request failed!")
func _on_connection_succeeded():
- $Connect.hide()
- $Players.show()
+ ConnectNodeContainer.hide()
+ PlayersNodeContainer.show()
func _on_connection_failed():
self.disable_connect_buttons()
- $Connect/Error.set_text("Connection failed!")
+ ConnectNode.get_node("Error").set_text("Connection failed!")
func _on_game_error(error):
@@ -81,34 +92,34 @@ func _on_game_error(error):
func _on_game_ended():
self.show()
- $Connect.show()
- $Players.hide()
+ ConnectNodeContainer.show()
+ PlayersNodeContainer.hide()
self.disable_connect_buttons()
func _on_Host_pressed():
- $Connect.hide()
- $Players.show()
- $Connect/Error.text = ""
+ ConnectNodeContainer.hide()
+ PlayersNodeContainer.show()
+ ConnectNode.get_node("Error").text = ""
self.set_ip()
- var player_name = $Connect/Name.text
+ var player_name = ConnectNode.get_node("Name").text
Network.host_game(player_name)
self.refresh_lobby()
func _on_Join_pressed():
- var ip = $Connect/IP.text
+ var ip = ConnectNode.get_node("IP").text
if not ip.is_valid_ip_address():
- $Connect/Error.text = "Invalid IP address!"
+ ConnectNode.get_node("Error").text = "Invalid IP address!"
return
- $Connect/Error.text = ""
+ ConnectNode.get_node("Error").text = ""
self.disable_connect_buttons(false)
self.set_ip()
- var player_name = $Connect/Name.text
+ var player_name = ConnectNode.get_node("Name").text
Network.join_game(ip, player_name)
@@ -117,6 +128,6 @@ func _on_Start_pressed():
func _on_Back_pressed():
- $Connect.show()
- $Players.hide()
+ ConnectNodeContainer.show()
+ PlayersNodeContainer.hide()
Network.leave_game()
diff --git a/Network/Lobby.tscn b/Network/Lobby.tscn
index f0b2622..60bf28e 100644
--- a/Network/Lobby.tscn
+++ b/Network/Lobby.tscn
@@ -12,14 +12,19 @@ grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1")
-[node name="Connect" type="Panel" parent="."]
-layout_mode = 0
-offset_left = 424.0
-offset_top = 232.0
-offset_right = 600.0
-offset_bottom = 376.0
+[node name="CenterContainer" type="CenterContainer" parent="."]
+layout_mode = 1
+anchors_preset = 15
+anchor_right = 1.0
+anchor_bottom = 1.0
+grow_horizontal = 2
+grow_vertical = 2
-[node name="Name" type="LineEdit" parent="Connect"]
+[node name="Connect" type="Panel" parent="CenterContainer"]
+custom_minimum_size = Vector2(180, 150)
+layout_mode = 2
+
+[node name="Name" type="LineEdit" parent="CenterContainer/Connect"]
layout_mode = 0
offset_left = 8.0
offset_top = 8.0
@@ -27,7 +32,7 @@ offset_right = 168.0
offset_bottom = 32.0
placeholder_text = "Name"
-[node name="IP" type="LineEdit" parent="Connect"]
+[node name="IP" type="LineEdit" parent="CenterContainer/Connect"]
layout_mode = 0
offset_left = 8.0
offset_top = 40.0
@@ -35,7 +40,7 @@ offset_right = 168.0
offset_bottom = 64.0
placeholder_text = "IP"
-[node name="Host" type="Button" parent="Connect"]
+[node name="Host" type="Button" parent="CenterContainer/Connect"]
layout_mode = 0
offset_left = 8.0
offset_top = 112.0
@@ -43,7 +48,7 @@ offset_right = 64.0
offset_bottom = 136.0
text = "Host"
-[node name="Join" type="Button" parent="Connect"]
+[node name="Join" type="Button" parent="CenterContainer/Connect"]
layout_mode = 0
offset_left = 112.0
offset_top = 112.0
@@ -51,7 +56,7 @@ offset_right = 168.0
offset_bottom = 136.0
text = "Join"
-[node name="Error" type="Label" parent="Connect"]
+[node name="Error" type="Label" parent="CenterContainer/Connect"]
layout_mode = 0
offset_left = 8.0
offset_top = 72.0
@@ -59,15 +64,20 @@ 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="."]
+[node name="CenterContainer2" type="CenterContainer" parent="."]
visible = false
-layout_mode = 0
-offset_left = 384.0
-offset_top = 48.0
-offset_right = 640.0
-offset_bottom = 512.0
+layout_mode = 1
+anchors_preset = 15
+anchor_right = 1.0
+anchor_bottom = 1.0
+grow_horizontal = 2
+grow_vertical = 2
+
+[node name="Players" type="Panel" parent="CenterContainer2"]
+custom_minimum_size = Vector2(260, 460)
+layout_mode = 2
-[node name="Label" type="Label" parent="Players"]
+[node name="Label" type="Label" parent="CenterContainer2/Players"]
layout_mode = 0
offset_left = 16.0
offset_top = 16.0
@@ -75,7 +85,7 @@ offset_right = 64.0
offset_bottom = 32.0
text = "Lobby"
-[node name="LocalIP" type="Label" parent="Players"]
+[node name="LocalIP" type="Label" parent="CenterContainer2/Players"]
layout_mode = 0
offset_left = 128.0
offset_top = 24.0
@@ -83,7 +93,7 @@ offset_right = 240.0
offset_bottom = 38.0
text = "000.000.000.000"
-[node name="RemoteIP" type="Label" parent="Players"]
+[node name="RemoteIP" type="Label" parent="CenterContainer2/Players"]
layout_mode = 0
offset_left = 128.0
offset_top = 8.0
@@ -91,14 +101,14 @@ offset_right = 240.0
offset_bottom = 22.0
text = "000.000.000.000"
-[node name="List" type="ItemList" parent="Players"]
+[node name="List" type="ItemList" parent="CenterContainer2/Players"]
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"]
+[node name="Start" type="Button" parent="CenterContainer2/Players"]
layout_mode = 0
offset_left = 64.0
offset_top = 416.0
@@ -106,7 +116,7 @@ offset_right = 192.0
offset_bottom = 448.0
text = "START!"
-[node name="Back" type="Button" parent="Players"]
+[node name="Back" type="Button" parent="CenterContainer2/Players"]
layout_mode = 0
offset_left = 16.0
offset_top = 416.0
@@ -128,7 +138,7 @@ 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"]
-[connection signal="pressed" from="Players/Back" to="." method="_on_Back_pressed"]
+[connection signal="pressed" from="CenterContainer/Connect/Host" to="." method="_on_Host_pressed"]
+[connection signal="pressed" from="CenterContainer/Connect/Join" to="." method="_on_Join_pressed"]
+[connection signal="pressed" from="CenterContainer2/Players/Start" to="." method="_on_Start_pressed"]
+[connection signal="pressed" from="CenterContainer2/Players/Back" to="." method="_on_Back_pressed"]
diff --git a/Network/Network.gd b/Network/Network.gd
index 0b3e379..ff076a4 100644
--- a/Network/Network.gd
+++ b/Network/Network.gd
@@ -14,8 +14,7 @@ const PORT = 10567
const MAX_PEERS = 2
-var peer = null
-var ip = null
+var peer: ENetMultiplayerPeer = null
var player = {
idx = 0,
@@ -59,7 +58,6 @@ func host_game(player_name):
self.peer = ENetMultiplayerPeer.new()
self.peer.create_server(self.PORT, self.MAX_PEERS)
multiplayer.multiplayer_peer = self.peer
- #get_tree().set_multiplayer_peer(self.peer)
func join_game(ip, player_name):
@@ -67,13 +65,11 @@ func join_game(ip, player_name):
self.peer = ENetMultiplayerPeer.new()
self.peer.create_client(ip, self.PORT)
multiplayer.multiplayer_peer = self.peer
- #get_tree().set_multiplayer_peer(self.peer)
func leave_game():
rpc("_leave_game")
multiplayer.multiplayer_peer = null
- #get_tree().set_multiplayer_peer(null)
@rpc("any_peer") func _leave_game():