summaryrefslogtreecommitdiff
path: root/Network/Lobby.gd
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2021-01-02 14:54:57 +0100
committerDaniel Weipert <code@drogueronin.de>2021-01-02 14:54:57 +0100
commitebae6d04ffc95c6648e863d3de71835f6cd4be96 (patch)
tree58ce9119a65ccd6925d661f12aab98a057dfb53d /Network/Lobby.gd
parentfb64f15e22acfc1c03c7c5357dd66077454adbcf (diff)
Initial working commit
Diffstat (limited to 'Network/Lobby.gd')
-rw-r--r--Network/Lobby.gd82
1 files changed, 82 insertions, 0 deletions
diff --git a/Network/Lobby.gd b/Network/Lobby.gd
new file mode 100644
index 0000000..d962081
--- /dev/null
+++ b/Network/Lobby.gd
@@ -0,0 +1,82 @@
+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")
+
+ if OS.has_environment("USERNAME"):
+ $Connect/Name.text = OS.get_environment("USERNAME")
+ else:
+ var desktop_path = OS.get_system_dir(0).replace("\\", "/").split("/")
+ $Connect/Name.text = desktop_path[desktop_path.size() - 2]
+
+
+func disable_connect_buttons(is_disabled = true):
+ $Connect/Host.disabled = is_disabled
+ $Connect/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)")
+ for p in players:
+ $Players/List.add_item(p.name)
+
+ $Players/Start.disabled = not get_tree().is_network_server()
+
+
+func _on_connection_succeeded():
+ $Connect.hide()
+ $Players.show()
+
+
+func _on_connection_failed():
+ self.disable_connect_buttons()
+ $Connect/Error.set_text("Connection failed!")
+
+
+func _on_game_error(error):
+ $ErrorDialog.dialog_text = error
+ $ErrorDialog.popup_centered_minsize()
+ self.disable_connect_buttons()
+
+
+func _on_game_ended():
+ self.show()
+ $Connect.show()
+ $Players.hide()
+ self.disable_connect_buttons()
+
+
+func _on_Host_pressed():
+ $Connect.hide()
+ $Players.show()
+ $Connect/Error.text = ""
+
+ var player_name = $Connect/Name.text
+ Network.host_game(player_name)
+ self.refresh_lobby()
+
+
+func _on_Join_pressed():
+ var ip = $Connect/IP.text
+ if not ip.is_valid_ip_address():
+ $Connect/Error.text = "Invalid IP address!"
+ return
+
+ $Connect/Error.text = ""
+ self.disable_connect_buttons(false)
+
+ var player_name = $Connect/Name.text
+ Network.join_game(ip, player_name)
+
+
+func _on_Start_pressed():
+ Network.start_game()