-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLobby.cs
139 lines (112 loc) · 3.71 KB
/
Lobby.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Godot;
using System;
public class Lobby : Control
{
private GameState GameState => GetNode<GameState>("/root/GameState");
private LineEdit ConnectName => GetNode<LineEdit>("Connect/Name");
private Label ErrorLabel => GetNode<Label>("Connect/ErrorLabel");
private Button Host => GetNode<Button>("Connect/Host");
private Button Join => GetNode<Button>("Connect/Join");
private Panel ConnectPanel => GetNode<Panel>("Connect");
private Panel PlayersPanel => GetNode<Panel>("Players");
private AcceptDialog ErrorDialog => GetNode<AcceptDialog>("ErrorDialog");
private ItemList PlayersList => GetNode<ItemList>("Players/List");
public override void _Ready()
{
// # Called every time the node is added to the scene.
GameState.Connect(nameof(GameState.connection_failed), this, nameof(_on_connection_failed));
GameState.Connect(nameof(GameState.connection_succeeded), this, nameof(_on_connection_success));
GameState.Connect(nameof(GameState.player_list_changed), this, nameof(refresh_lobby));
GameState.Connect(nameof(GameState.game_ended), this, nameof(_on_game_ended));
GameState.Connect(nameof(GameState.game_error), this, nameof(_on_game_error));
// # Set the player name according to the system username. Fallback to the path.
if (OS.HasEnvironment("USERNAME"))
{
ConnectName.Text = OS.GetEnvironment("USERNAME");
}
else
{
var desktop_path = OS.GetSystemDir(0).Replace("\\", "/").Split("/");
ConnectName.Text = desktop_path[desktop_path.Length - 2];
}
}
public void _on_host_pressed()
{
if (ConnectName.Text == "")
{
ErrorLabel.Text = "Invalid name!";
return;
}
ConnectPanel.Hide();
PlayersPanel.Show();
ErrorLabel.Text = "";
var player_name = ConnectName.Text;
GameState.host_game(player_name);
refresh_lobby();
}
public void _on_join_pressed()
{
if (ConnectName.Text == "")
{
ErrorLabel.Text = "Invalid name!";
return;
}
var ip = GetNode<LineEdit>("Connect/IPAddress").Text;
if (!ip.IsValidIPAddress())
{
ErrorLabel.Text = "Invalid IP address!";
return;
}
ErrorLabel.Text = "";
Host.Disabled = true;
Join.Disabled = true;
var player_name = ConnectName.Text;
GameState.join_game(ip, player_name);
}
public void _on_connection_success()
{
ConnectPanel.Hide();
PlayersPanel.Show();
}
public void _on_connection_failed()
{
Host.Disabled = false;
Join.Disabled = false;
ErrorLabel.Text = "Connection failed.";
}
public void _on_game_ended()
{
Show();
ConnectPanel.Show();
PlayersPanel.Hide();
Host.Disabled = false;
Join.Disabled = false;
}
public void _on_game_error(string errtxt)
{
ErrorDialog.DialogText = errtxt;
ErrorDialog.PopupCenteredMinsize();
Host.Disabled = false;
Join.Disabled = false;
}
public void refresh_lobby()
{
var players = GameState.get_player_list();
players.Sort();
PlayersList.Clear();
PlayersList.AddItem(GameState.get_player_name() + " (You)");
foreach (var p in players)
{
PlayersList.AddItem(p);
}
GetNode<Button>("Players/Start").Disabled = !GetTree().IsNetworkServer();
}
public void _on_start_pressed()
{
GameState.begin_game();
}
public void _on_find_public_ip_pressed()
{
OS.ShellOpen("https://icanhazip.com/");
}
}