quantum-space-buddies/QSB/QSBSceneManager.cs

48 lines
1.3 KiB
C#
Raw Normal View History

2020-11-04 09:34:01 +00:00
using OWML.Common;
using QSB.Utility;
2021-03-23 13:18:29 +00:00
using QSB.WorldSync;
2020-11-04 09:34:01 +00:00
using System;
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB;
public static class QSBSceneManager
2020-08-16 20:39:21 +00:00
{
2022-03-03 03:46:33 +00:00
public static OWScene CurrentScene => LoadManager.GetCurrentScene();
public static bool IsInUniverse => InUniverse(CurrentScene);
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
public static event Action<OWScene, OWScene, bool> OnSceneLoaded;
public static event Action<OWScene, OWScene> OnUniverseSceneLoaded;
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
static QSBSceneManager()
{
LoadManager.OnCompleteSceneLoad += OnCompleteSceneLoad;
DebugLog.DebugWrite("Scene Manager ready.", MessageType.Success);
}
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
private static void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene)
{
DebugLog.DebugWrite($"COMPLETE SCENE LOAD ({oldScene} -> {newScene})", MessageType.Info);
QSBWorldSync.RemoveWorldObjects();
2022-03-03 03:46:33 +00:00
var universe = InUniverse(newScene);
if (QSBCore.IsInMultiplayer && universe)
2020-12-14 21:20:53 +00:00
{
2022-03-03 03:46:33 +00:00
// So objects have time to be deleted, made, whatever
Delay.RunNextFrame(() => QSBWorldSync.BuildWorldObjects(newScene).Forget());
2020-12-14 21:20:53 +00:00
}
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
OnSceneLoaded?.SafeInvoke(oldScene, newScene, universe);
if (universe)
2020-12-14 21:20:53 +00:00
{
2022-03-03 03:46:33 +00:00
OnUniverseSceneLoaded?.SafeInvoke(oldScene, newScene);
2020-12-14 21:20:53 +00:00
}
2020-08-16 20:39:21 +00:00
2022-03-03 03:46:33 +00:00
if (newScene == OWScene.TitleScreen && QSBCore.IsInMultiplayer)
{
QSBNetworkManager.singleton.StopHost();
}
2020-12-14 21:20:53 +00:00
}
2022-03-03 03:46:33 +00:00
private static bool InUniverse(OWScene scene) =>
scene is OWScene.SolarSystem or OWScene.EyeOfTheUniverse;
}