-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectBeatsGame.cs
546 lines (487 loc) · 18.8 KB
/
ProjectBeatsGame.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using PBGame.UI.Navigations.Screens;
using PBGame.UI.Navigations.Overlays;
using PBGame.Audio;
using PBGame.Rulesets.Maps;
using PBGame.Graphics;
using PBGame.Networking.API;
using PBGame.Notifications;
using PBFramework.Threading;
using UnityEngine;
using Logger = PBFramework.Debugging.Logger;
namespace PBGame
{
public class ProjectBeatsGame : BaseGame
{
/// <summary>
/// Holds the original screen size before modifying with game configurations.
/// </summary>
private Vector2 originalScreenSize;
/// <summary>
/// The offset of the current mapset.
/// </summary>
private IMusicOffset mapsetOffset;
/// <summary>
/// The offset of the current map.
/// </summary>
private IMusicOffset mapOffset;
/// <summary>
/// Log service for piping log messages to the notification box.
/// </summary>
private LogToNotificationService logToNotifService;
/// <summary>
/// Returns whether the initial splash view should be shown automatically.
/// </summary>
public virtual bool ShouldShowFirstView => true;
protected override void PostInitialize()
{
base.PostInitialize();
originalScreenSize = new Vector2(Screen.width, Screen.height);
// Initialize log service for notification.
logToNotifService = new LogToNotificationService()
{
NotificationBox = base.notificationBox,
};
Logger.Register(logToNotifService);
// Hook events
HookEngine();
HookMusicController();
HookScreenNavigator();
HookOverlayNavigator();
HookMapSelection();
HookConfigurations();
HookDownloadStore();
HookMapManager();
HookApi();
// Display splash view.
if (ShouldShowFirstView)
screenNavigator.Show<SplashScreen>();
}
public override void GracefulQuit()
{
// Confirm quit
var dialog = overlayNavigator.Show<DialogOverlay>();
dialog.Model.SetMessage("Are you sure you want to quit Project: Beats?");
dialog.Model.AddConfirmCancel(OnConfirmQuit, null);
}
public override void ForceQuit()
{
// Store configurations first
gameConfiguration.Save();
mapConfiguration.Save();
mapsetConfiguration.Save();
base.ForceQuit();
}
/// <summary>
/// Event called from dialog when user decided to quit the game.
/// </summary>
private void OnConfirmQuit()
{
overlayNavigator.Show<QuitOverlay>();
}
/// <summary>
/// Triggers actions on certain system events.
/// </summary>
private void HookEngine()
{
// Start listening to any exceptions that occurs during game.
AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
{
var exception = e.ExceptionObject as Exception;
Logger.LogError($"Unhandled exception: {exception.ToString()}");
};
Application.logMessageReceived += (condition, stackTrace, type) =>
{
if (type == LogType.Exception)
{
Logger.LogError($"Unhandled exception: {condition}\n{stackTrace}");
}
};
}
/// <summary>
/// Triggers actions on certain music controller events.
/// </summary>
private void HookMusicController()
{
musicController.OnEnd += () =>
{
// Loop the music when not in game screen.
if (!(screenNavigator.CurrentScreen.Value is GameScreen))
{
if (screenNavigator.CurrentScreen.Value is HomeScreen)
{
mapSelection.SelectMapset(musicPlaylist.PeekNext());
}
// Else if download screen, just stop there.
else if (screenNavigator.CurrentScreen.Value is DownloadScreen)
{
musicController.Stop();
}
else
{
musicController.Play();
musicController.Seek(musicController.LoopTime);
musicController.Fade(0f, 1f);
}
}
// Else, don't do anything.
};
}
/// <summary>
/// Triggers actions on certain screen navigator events.
/// </summary>
private void HookScreenNavigator()
{
screenNavigator.OnShowView += (view) =>
{
if (mapSelection.Map.Value != null)
{
// Change loop time based on the screens.
if (view is HomeScreen || view is DownloadScreen || view is GameScreen)
musicController.LoopTime = 0f;
else
musicController.LoopTime = mapSelection.Map.Value.Metadata.PreviewTime;
}
ApplyMenuBarOverlay();
};
screenNavigator.CurrentScreen.OnValueChanged += (curScreen, prevScreen) =>
{
// If navigating out of game screen, play music if stopped.
if (prevScreen is GameScreen)
{
if (!musicController.IsPlaying)
{
bool wasPaused = musicController.IsPaused;
musicController.Play();
// Play from preview point if music stopped at the end.
if (!wasPaused)
{
musicController.Seek(mapSelection.Map.Value.Metadata.PreviewTime);
musicController.Fade(0f, 1f);
}
}
}
};
}
/// <summary>
/// Triggers actions on cerain overlay navigator events.
/// </summary>
private void HookOverlayNavigator()
{
overlayNavigator.OnShowView += (view) =>
{
if(view is OffsetsOverlay)
musicController.SetVolume(0.5f);
if (!(view is MenuBarOverlay))
ApplyMenuBarOverlay();
};
overlayNavigator.OnHideView += (view) =>
{
if(view is OffsetsOverlay)
musicController.SetVolume(1f);
if (!(view is MenuBarOverlay))
ApplyMenuBarOverlay();
};
}
/// <summary>
/// Triggers actions on certain map selection events.
/// </summary>
private void HookMapSelection()
{
mapSelection.MapConfig.OnValueChanged += (config, prevConfig) =>
{
// Observe offset changes in the map.
if(prevConfig != null)
prevConfig.Offset.OnValueChanged -= OnMusicOffsetChange;
mapOffset = config;
if(mapOffset != null)
mapOffset.Offset.BindAndTrigger(OnMusicOffsetChange);
};
mapSelection.MapsetConfig.OnValueChanged += (config, prevConfig) =>
{
// Observe offset changes in the mapset.
if (prevConfig != null)
prevConfig.Offset.OnValueChanged -= OnMusicOffsetChange;
mapsetOffset = config;
if (mapsetOffset != null)
mapsetOffset.Offset.BindAndTrigger(OnMusicOffsetChange);
};
mapSelection.Map.OnNewValue += (map) =>
{
metronome.CurrentMap = map;
};
mapSelection.Mapset.OnNewValue += (mapset) =>
{
musicPlaylist.Focus(mapset);
};
mapSelection.Music.OnNewValue += (music) =>
{
musicController.MountAudio(music);
if (music == null)
{
musicController.Stop();
return;
}
musicController.Play();
// Seek to preview time if not home screen.
if (!(screenNavigator.CurrentScreen.Value is HomeScreen))
{
var previewTime = mapSelection.Map.Value.Metadata.PreviewTime;
// Some songs don't have a proper preview time.
if (previewTime < 0)
previewTime = music.Duration / 2;
musicController.LoopTime = previewTime;
musicController.Seek(previewTime);
}
else
{
musicController.LoopTime = 0f;
}
// Play song
musicController.Fade(0f, 1f);
};
}
/// <summary>
/// Triggers actions on certain configuration events.
/// </summary>
private void HookConfigurations()
{
// Action request events
gameConfiguration.OnRequestGameRepo += () =>
{
Application.OpenURL(App.GameRepository);
};
gameConfiguration.OnRequestFrameworkRepo += () =>
{
Application.OpenURL(App.FrameworkRepository);
};
gameConfiguration.OnRequestMapsetCheck += () =>
{
LoadMapsetsInDownloads();
};
// Game volume change events
gameConfiguration.MasterVolume.OnNewValue += (volume) =>
{
ApplyMusicVolume();
soundPool.SetVolume(gameConfiguration.MasterVolume.Value * gameConfiguration.EffectVolume.Value);
};
gameConfiguration.MusicVolume.OnNewValue += (volume) =>
{
ApplyMusicVolume();
};
gameConfiguration.EffectVolume.OnNewValue += (volume) =>
{
soundPool.SetVolume(gameConfiguration.MasterVolume.Value * gameConfiguration.EffectVolume.Value);
};
// Mapset sort change events
gameConfiguration.MapsetSort.OnNewValue += (sort) =>
{
mapManager.Sort(sort);
};
// Parallax events
gameConfiguration.UseParallax.OnNewValue += (useParallax) =>
{
inputManager.UseAcceleration = useParallax;
};
// Resolution & framerate change events
gameConfiguration.ResolutionQuality.OnNewValue += (quality) =>
{
ApplyScreenResolution();
};
gameConfiguration.Framerate.OnNewValue += (framerate) =>
{
ApplyScreenResolution();
};
// Offset events
gameConfiguration.GlobalOffset.OnNewValue += (offset) =>
{
musicController.Clock.Offset = offset;
};
// Game mode events
gameConfiguration.RulesetMode.OnNewValue += (mode) =>
{
NotifyUnplayableMode();
};
// Notification related
gameConfiguration.PersistNotificationLevel.OnNewValue += (level) =>
{
notificationBox.ForceStoreLevel = level;
};
gameConfiguration.LogToNotificationLevel.OnNewValue += (level) =>
{
logToNotifService.PipeLogLevel = level;
};
}
/// <summary>
/// Triggers actions on certain download store events.
/// </summary>
private void HookDownloadStore()
{
// Handle mapset import
downloadStore.MapStorage.OnAdded += (path) =>
{
mapManager.Import(downloadStore.MapStorage.GetFile(path));
notificationBox.Add(new Notification()
{
Message = $"Successfully downloaded mapset at ({path})",
});
};
}
/// <summary>
/// Triggers action on certain map manager events.
/// </summary>
private void HookMapManager()
{
mapManager.OnImportMapset += (mapset) =>
{
if (mapset == null)
return;
// Select new map if in songs selection.
if (screenNavigator.CurrentScreen.Value is SongsScreen)
mapSelection.SelectMapset(mapset);
notificationBox.Add(new Notification()
{
Message = $"Imported mapset ({mapset.Metadata.Artist} - {mapset.Metadata.Title})",
});
};
mapManager.AllMapsets.OnChange += (mapsets) =>
{
musicPlaylist.Refill(mapsets);
};
}
/// <summary>
/// Triggers action on certain API events.
/// </summary>
private void HookApi()
{
api.User.OnValueChanged += (user, oldUser) =>
{
OnApiUserChange(user);
};
}
/// <summary>
/// Applies menu bar overlay where necessary.
/// </summary>
private void ApplyMenuBarOverlay()
{
if (screenNavigator.CurrentScreen.Value is HomeScreen)
{
if (overlayNavigator.IsShowing(typeof(HomeMenuOverlay)))
overlayNavigator.Show<MenuBarOverlay>(true);
else
overlayNavigator.Hide<MenuBarOverlay>();
return;
}
if (overlayNavigator.IsShowing(typeof(GameLoadOverlay)) ||
screenNavigator.CurrentScreen.Value is GameScreen ||
screenNavigator.CurrentScreen.Value is InitializeScreen ||
screenNavigator.CurrentScreen.Value is SplashScreen)
{
overlayNavigator.Hide<MenuBarOverlay>();
return;
}
// Show menu ber by default.
overlayNavigator.Show<MenuBarOverlay>(true);
}
/// <summary>
/// Applies screen resolution and framerate settings.
/// </summary>
private void ApplyScreenResolution()
{
Vector2 newResolution = originalScreenSize * gameConfiguration.ResolutionQuality.Value.GetResolutionScale();
int framerate = gameConfiguration.Framerate.Value.GetFrameRate();
Screen.SetResolution((int)newResolution.x, (int)newResolution.y, FullScreenMode.ExclusiveFullScreen, framerate);
Application.targetFrameRate = framerate;
inputManager.SetResolution(newResolution);
}
/// <summary>
/// Applies music offset to music controller.
/// </summary>
private void ApplyMusicOffset() => musicController.Clock.Offset = GetMusicOffset();
/// <summary>
/// Applies volume to music controller.
/// </summary>
private void ApplyMusicVolume(float scale = 1f)
{
musicController.SetVolume(gameConfiguration.MasterVolume.Value * gameConfiguration.MusicVolume.Value * scale);
}
/// <summary>
/// Shows a notification to the user if the current game mode is unplayable yet.
/// </summary>
private void NotifyUnplayableMode()
{
var gameMode = gameConfiguration.RulesetMode.Value;
var modeService = modeManager.GetService(gameMode);
if (modeService != null)
{
if (!modeService.IsPlayable)
{
notificationBox.Add(new Notification()
{
Message = $"The selected game mode ({modeService.Name}) is not playable yet. Any maps selected will be playable by its original, or the first available mode.",
Scope = NotificationScope.Temporary,
Type = NotificationType.Warning,
});
}
}
}
/// <summary>
/// Loads all mapsets in the downloads folder.
/// </summary>
private void LoadMapsetsInDownloads()
{
TaskListener listener = new TaskListener();
string id = new Guid().ToString();
listener.OnFinished += () =>
{
notificationBox.RemoveById(id, false);
};
foreach (var archive in downloadStore.MapStorage.GetAllFiles())
mapManager.Import(archive, listener.CreateSubListener<IMapset>());
if (listener.SubListenerCount == 0)
{
notificationBox.Add(new Notification()
{
Message = "There are no mapsets to load.",
});
}
else
{
notificationBox.Add(new Notification()
{
Listener = listener,
Message = $"Loading {listener.SubListenerCount} mapsets in downloads directory.",
Type = NotificationType.Info,
Id = id,
});
}
}
/// <summary>
/// Returns the total music offset applied.
/// </summary>
private float GetMusicOffset()
{
return gameConfiguration.GlobalOffset.Value +
(mapOffset != null ? mapOffset.Offset.Value : 0) +
(mapsetOffset != null ? mapsetOffset.Offset.Value : 0);
}
/// <summary>
/// Event called when the user in API has become online.
/// </summary>
private void OnApiUserChange(IOnlineUser user)
{
userManager.SaveUser(userManager.CurrentUser.Value);
userManager.SetUser(user);
}
/// <summary>
/// Event called when the mapset/map's offset has been changed.
/// </summary>
private void OnMusicOffsetChange(int offset, int prevOffset)
{
ApplyMusicOffset();
}
}
}