forked from tralezab/probable-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlobby.gd
76 lines (67 loc) · 2.25 KB
/
lobby.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
extends Control
func _ready():
# Called every time the node is added to the scene.
gamestate.connect("connection_failed", self, "_on_connection_failed")
gamestate.connect("connection_succeeded", self, "_on_connection_success")
gamestate.connect("player_list_changed", self, "refresh_lobby")
gamestate.connect("game_ended", self, "_on_game_ended")
gamestate.connect("game_error", self, "_on_game_error")
# Set the player name according to the system username. Fallback to the path.
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 _on_host_pressed():
if $Connect/Name.text == "":
$Connect/ErrorLabel.text = "Invalid name!"
return
$Connect.hide()
$Players.show()
$Connect/ErrorLabel.text = ""
var player_name = $Connect/Name.text
var port = int($Connect/Port.text)
gamestate.host_game(player_name, port)
refresh_lobby()
func _on_join_pressed():
if $Connect/Name.text == "":
$Connect/ErrorLabel.text = "Invalid name!"
return
var ip = $Connect/IPAddress.text
var port = $Connect/Port.text
if not ip.is_valid_ip_address():
$Connect/ErrorLabel.text = "Invalid IP address!"
return
$Connect/ErrorLabel.text = ""
$Connect/Host.disabled = true
$Connect/Join.disabled = true
var player_name = $Connect/Name.text
gamestate.join_game(ip, player_name, port)
func _on_connection_success():
$Connect.hide()
$Players.show()
func _on_connection_failed():
$Connect/Host.disabled = false
$Connect/Join.disabled = false
$Connect/ErrorLabel.set_text("Connection failed.")
func _on_game_ended():
show()
$Connect.show()
$Players.hide()
$Connect/Host.disabled = false
$Connect/Join.disabled = false
func _on_game_error(errtxt):
$ErrorDialog.dialog_text = errtxt
$ErrorDialog.popup_centered_minsize()
$Connect/Host.disabled = false
$Connect/Join.disabled = false
func refresh_lobby():
var players = gamestate.get_player_list()
players.sort()
$Players/List.clear()
$Players/List.add_item(gamestate.get_player_name() + " (You)")
for p in players:
$Players/List.add_item(p)
$Players/Start.disabled = not get_tree().is_network_server()
func _on_start_pressed():
gamestate.begin_game()