-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStageProducer.cs
More file actions
291 lines (261 loc) · 9.71 KB
/
StageProducer.cs
File metadata and controls
291 lines (261 loc) · 9.71 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System.Threading.Tasks;
using FunkEngine;
using Godot;
using GodotSteam;
/**
* <summary>StageProducer: Handles scene transitions and persistent gameplay data.</summary>
*/
public partial class StageProducer : Node
{
public static StageProducer LiveInstance { get; private set; }
public static bool IsInitialized;
public static readonly RandomNumberGenerator GlobalRng = new();
public static MapLevels CurLevel { get; private set; }
public static MapGrid Map { get; private set; } = new();
private Stages _curStage = Stages.Title;
public static int CurRoom { get; private set; }
private Node _curScene;
private Node _preloadStage;
public static BattleConfig Config { get; private set; }
public static PlayerStats PlayerStats { get; private set; } //Hold here to persist between changes
public static CanvasLayer ContrastFilter { get; private set; }
#region Initialization
public override void _EnterTree()
{
InitFromCfg();
LiveInstance = this;
}
private void InitFromCfg()
{
OptionsMenu.ChangeVolume(
SaveSystem.GetConfigValue(SaveSystem.ConfigSettings.Volume).As<float>()
);
TranslationServer.SetLocale(
SaveSystem.GetConfigValue(SaveSystem.ConfigSettings.LanguageKey).As<string>()
);
ContrastFilter = GD.Load<PackedScene>("res://Globals/ContrastFilter/ContrastFilter.tscn")
.Instantiate<CanvasLayer>();
ContrastFilter.Visible = SaveSystem
.GetConfigValue(SaveSystem.ConfigSettings.HighContrast)
.AsBool();
GetTree().Root.CallDeferred("add_child", ContrastFilter);
}
private void GenerateMapConsistent()
{
//Fudge seed state, to get consistent maps across new/loaded games, might be bad practice
GlobalRng.State = GlobalRng.Seed << 5 / 2;
Map.InitMapGrid(CurLevel.GetCurrentConfig());
}
private void StartNewGame()
{
GlobalRng.Randomize();
if ((bool)SaveSystem.GetConfigValue(SaveSystem.ConfigSettings.FirstTime))
CurLevel = MapLevels.GetLevelFromId(0);
else
CurLevel = MapLevels.GetLevelFromId(1);
GenerateMapConsistent();
PlayerStats = new PlayerStats();
CurRoom = Map.GetRooms()[0].Idx;
Scribe.InitRelicPools();
IsInitialized = true;
}
private bool LoadGame()
{
SaveSystem.SaveFile sv = SaveSystem.LoadGame();
if (sv == null)
{
GD.PushWarning("Can't load game, either file 404 or invalid file.");
return false;
}
GlobalRng.Seed = sv.RngSeed;
CurLevel = MapLevels.GetLevelFromId(sv.Area);
GenerateMapConsistent();
GlobalRng.State = sv.RngState;
CurRoom = sv.LastRoomIdx;
Scribe.InitRelicPools();
PlayerStats = new PlayerStats();
PlayerStats.CurNotes = [];
foreach (int noteId in sv.NoteIds)
{
PlayerStats.AddNote(Scribe.NoteDictionary[noteId]);
}
foreach (int relicId in sv.RelicIds)
{
PlayerStats.AddRelic(Scribe.RelicDictionary[relicId]);
}
PlayerStats.CurrentHealth = sv.PlayerHealth;
PlayerStats.Money = sv.Money;
PlayerStats.Shortcuts = sv.Shortcuts;
IsInitialized = true;
return true;
}
#endregion
public static MapGrid.Room GetCurRoom()
{
return Map.GetRooms()[CurRoom];
}
#region Transitions
public void TransitionFromRoom(int nextRoomIdx)
{
TransitionStage(Map.GetRooms()[nextRoomIdx].Type, nextRoomIdx);
}
private Task _loadTask;
/// <summary>
/// To be used from Cartographer. Preloads the scene during transition animation. This removes the occasionally noticeable load time for the scene change.
/// </summary>
/// <param name="nextRoomIdx">Index of the next room in the map to get the stage from.</param>
public void PreloadScene(int nextRoomIdx)
{
Stages nextStage = Map.GetRooms()[nextRoomIdx].Type;
Config = MakeBattleConfig(nextStage, nextRoomIdx);
switch (nextStage)
{
case Stages.Elite:
case Stages.Battle:
case Stages.Boss:
_loadTask = Task.Run(() =>
{
_preloadStage = GD.Load<PackedScene>(BattleDirector.LoadPath)
.Instantiate<Node>();
});
break;
case Stages.Shop:
_loadTask = Task.Run(() =>
{
_preloadStage = GD.Load<PackedScene>(ShopScene.LoadPath).Instantiate<Node>();
});
break;
case Stages.Event:
case Stages.Chest:
_loadTask = Task.Run(() =>
{
_preloadStage = GD.Load<PackedScene>(ChestScene.LoadPath).Instantiate<Node>();
});
break;
default:
GD.PushError($"Error Scene Transition is {nextStage}");
break;
}
}
public void TransitionStage(Stages nextStage, int nextRoomIdx = -1)
{
GetTree().Root.RemoveChild(ContrastFilter);
switch (nextStage)
{
case Stages.Title:
IsInitialized = false;
GetTree().ChangeSceneToFile(TitleScreen.LoadPath);
break;
case Stages.Battle: //Currently these are only ever entered from map. Be aware if we change
case Stages.Boss: //this, scenes either need to be preloaded first, or a different setup is needed.
case Stages.Event:
case Stages.Elite:
case Stages.Shop:
case Stages.Chest:
_loadTask.Wait(); //Should always finish by the time it gets here, this guarantees it.
GetTree().GetCurrentScene().Free();
GetTree().Root.AddChild(_preloadStage);
GetTree().SetCurrentScene(_preloadStage);
break;
case Stages.Map:
GetTree().ChangeSceneToFile(Cartographer.LoadPath);
if (!IsInitialized)
{
StartNewGame();
}
break;
case Stages.Load:
if (!LoadGame())
StartNewGame();
GetTree().ChangeSceneToFile(Cartographer.LoadPath);
break;
case Stages.Quit:
GetTree().Quit();
return;
case Stages.Continue:
ProgressLevels();
GetTree().ChangeSceneToFile("res://Scenes/Maps/InBetween.tscn");
break;
default:
GD.PushError($"Error Scene Transition is {nextStage}");
break;
}
_preloadStage = null;
//Apply grayscale shader to all scenes
GetTree().Root.AddChild(ContrastFilter);
_curStage = nextStage;
}
#endregion
private BattleConfig MakeBattleConfig(Stages nextRoom, int nextRoomIdx)
{
BattleConfig result = new BattleConfig
{
BattleRoom = Map.GetRooms()[nextRoomIdx],
RoomType = nextRoom,
};
RandomNumberGenerator stageRng = new RandomNumberGenerator();
stageRng.SetSeed(GlobalRng.Seed + (ulong)nextRoomIdx);
switch (nextRoom)
{
case Stages.Battle:
int songIdx = stageRng.RandiRange(0, CurLevel.NormalBattles.Length - 1);
result.CurSong = Scribe.SongDictionary[CurLevel.NormalBattles[songIdx]];
result.EnemyScenePath = Scribe
.SongDictionary[CurLevel.NormalBattles[songIdx]]
.EnemyScenePath;
break;
case Stages.Elite:
int elitIdx = stageRng.RandiRange(0, CurLevel.EliteBattles.Length - 1);
result.CurSong = Scribe.SongDictionary[CurLevel.EliteBattles[elitIdx]];
result.EnemyScenePath = Scribe
.SongDictionary[CurLevel.EliteBattles[elitIdx]]
.EnemyScenePath;
break;
case Stages.Boss:
int bossIdx = stageRng.RandiRange(0, CurLevel.BossBattles.Length - 1);
result.CurSong = Scribe.SongDictionary[CurLevel.BossBattles[bossIdx]];
result.EnemyScenePath = Scribe
.SongDictionary[CurLevel.BossBattles[bossIdx]]
.EnemyScenePath;
break;
case Stages.Event:
case Stages.Shop:
case Stages.Chest:
break;
default:
GD.PushError($"Error making Battle Config for invalid room type: {nextRoom}");
break;
}
CurRoom = nextRoomIdx;
return result;
}
//Putting this here in an autoload.
public override void _Input(InputEvent @event)
{
//Consume controller input, if window out of focus.
//This handles ui_input, other scenes need to consume their own.
if (ControlSettings.IsOutOfFocus(this))
{
GetViewport().SetInputAsHandled();
return;
}
}
#region Area Management
/// <summary>
/// There should always be a mapconfig for each area. It's preferable to crash later if there isn't even a placeholder config.
/// </summary>
/// <returns>True if there is another area.</returns>
public static bool IsMoreLevels()
{
return CurLevel.HasMoreMaps();
}
public void ProgressLevels()
{
GD.Print(CurLevel.Id);
CurLevel = CurLevel.GetNextLevel();
Map = new();
GenerateMapConsistent();
CurRoom = Map.GetRooms()[0].Idx;
}
#endregion
}