-
Notifications
You must be signed in to change notification settings - Fork 0
/
Global.cs
231 lines (198 loc) · 6.97 KB
/
Global.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Godot;
namespace LD56;
public partial class Global : Node
{
public static readonly PackedScene toastScene = GD.Load<PackedScene>("res://ui/toast.tscn");
private const string DEFAULT_C2_BASE_URI = "wss://banana4.life";
private const string DEFAULT_C2_STATS_URI = "https://banana4.life/ld56/stats";
private string c2_base_uri;
private PackedScene worldScene = GD.Load<PackedScene>("res://world.tscn");
private PackedScene mainMenuScene = GD.Load<PackedScene>("res://ui/main_menu/main_menu.tscn");
private PackedScene countDownScene = GD.Load<PackedScene>("res://ui/transitions/countdown.tscn");
private PackedScene respawnScene = GD.Load<PackedScene>("res://ui/transitions/respawn.tscn");
public static Global Instance { get; private set; }
public State State;
public PlayerManager PlayerManager = new();
public string StatsUri;
private World world;
private Respawn respawn;
private Countdown countdown;
private bool ready;
public ImmutableList<string> defaultUnlockedColors = ["Pure Green", "Pure Red", "Pure Blue"];
public List<string> unlockedColors;
private ConfigFile config = new();
public readonly RandomNumberGenerator Random = new();
public string selectedColor;
private Toast activeToast;
public Global()
{
Instance = this;
var result = config.Load("user://config.cfg");
c2_base_uri = DEFAULT_C2_BASE_URI;
StatsUri = DEFAULT_C2_STATS_URI;
unlockedColors = defaultUnlockedColors.ToList();
selectedColor = defaultUnlockedColors.First();
if (result == Error.Ok)
{
var cfg = config.GetValue("savegame", "colors", defaultUnlockedColors.ToArray());
unlockedColors = cfg.AsStringArray().Where(UnlockableColors.Colors.ContainsKey).ToList();
if (!unlockedColors.Any())
{
unlockedColors.AddRange(defaultUnlockedColors);
}
config.SetValue("savegame", "colors", unlockedColors.ToArray());
config.Save("user://config.cfg");
selectedColor = unlockedColors[Random.RandiRange(0, unlockedColors.Count-1)];
c2_base_uri = config.GetValue("c2server", "host", DEFAULT_C2_BASE_URI).AsString();
StatsUri = config.GetValue("c2server", "stats", DEFAULT_C2_STATS_URI).AsString();
}
else
{
GD.PushError($"Failed to load config file: {result}");
}
}
public void EnterClientState(string playerName)
{
LoadCountdownScene();
State = new ClientState(Multiplayer, c2_base_uri, playerName);
SetWindowTitle("Joining");
}
public void EnterServerState(string playerName)
{
State = new ServerState(Multiplayer, c2_base_uri, playerName);
}
public override void _Process(double delta)
{
if (State != null)
{
State.Update(delta);
}
}
public void SendPlayerInfo(string playerName)
{
GD.Print($"SendPlayerInfo: {playerName}");
RpcId(1, MethodName.ReceivePlayerInfo, playerName, Multiplayer.GetUniqueId(), selectedColor);
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
public void ReceivePlayerInfo(string playerName, int peerId, string selectedColor)
{
GD.Print($"ReceivePlayerInfo: {playerName} ({peerId})");
if (State is ServerState)
{
PlayerManager.AddPlayer(playerName, peerId, selectedColor);
}
else
{
GD.PrintErr("Got Player Info on Client?");
}
}
public void SendPlayerReady()
{
if (ready)
{
return;
}
ready = true;
GD.Print($"SendPlayerReady: {Multiplayer.GetUniqueId()}");
RpcId(1, MethodName.ReceivePlayerReady, Multiplayer.GetUniqueId());
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
public void ReceivePlayerReady(long peerId)
{
GD.Print($"ReceivePlayerReady: {peerId}");
PlayerManager.SetPlayerReady(peerId);
}
public void SendPlayerDead()
{
ready = false;
GD.Print($"SendPlayerDead: {Multiplayer.GetUniqueId()}");
RpcId(1, MethodName.ReceivePlayerDead, Multiplayer.GetUniqueId());
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
public void ReceivePlayerDead(long peerId)
{
GD.Print($"ReceivePlayerReady: {peerId}");
PlayerManager.SetPlayerDead(peerId);
}
public void LoadMainMenu()
{
GetTree().ChangeSceneToPacked(mainMenuScene);
}
public void LoadWorldScene(bool show)
{
GD.Print("Loading World Scene...");
if (world == null)
{
world = worldScene.Instantiate<World>();
GetTree().Root.AddChild(world);
}
if (show)
{
if (GetTree().CurrentScene is Countdown current1)
{
GD.Print($"Free {current1.Name} Scene...");
current1.QueueFree();
}
else if (GetTree().CurrentScene is Mainmenu current2)
{
GD.Print($"Free {current2.Name} Scene...");
current2.QueueFree();
}
GD.Print("Show World Scene...");
GetTree().SetCurrentScene(world);
}
world.Visible = show;
}
private void LoadCountdownScene()
{
GetTree().ChangeSceneToPacked(countDownScene);
LoadWorldScene(false);
}
public void LoadRespawnScene(int score)
{
respawn = respawnScene.Instantiate<Respawn>();
GetTree().Root.AddChild(respawn);
GetTree().SetCurrentScene(respawn);
respawn.GetNode<Label>("score").Text = score.ToString();
}
public void LoadCountdownSceneInWorld()
{
respawn.QueueFree();
countdown = countDownScene.Instantiate<Countdown>();
countdown.respawn = true;
SetWindowTitle("Respawning");
GetTree().Root.AddChild(countdown);
GetTree().SetCurrentScene(countdown);
}
public void SetWindowTitle(string title)
{
var name = ProjectSettings.GetSetting("application/config/name", "Game").AsString();
DisplayServer.WindowSetTitle($"{name} - {title}");
}
public void AwardUnlockedColor(string unlock)
{
if (!unlockedColors.Contains(unlock))
{
unlockedColors.Add(unlock);
config.SetValue("savegame", "colors", unlockedColors.ToArray());
config.Save("user://config.cfg");
GD.Print($"Awarded {unlock}");
Audio.Instance.Ding();
if (activeToast == null)
{
activeToast = toastScene.Instantiate<Toast>();
AddChild(activeToast);
}
activeToast.Present(unlock);
}
}
public void ResetWorld()
{
world.QueueFree();
world = null;
ready = false;
}
}