scene manager (#179)

This commit is contained in:
AmazingAlek 2020-08-16 22:39:21 +02:00 committed by GitHub
parent 66cf60594f
commit 8d3d3e4bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 65 additions and 49 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Linq;
using OWML.ModHelper.Events;
using QSB.Events;
using QSB.Messaging;
@ -94,11 +95,8 @@ namespace QSB.Animation
playerAnimController.SetValue("_unsuitedAnimOverride", null);
playerAnimController.SetValue("_rightArmHidden", false);
var rightArmObjects = playerAnimController.GetValue<GameObject[]>("_rightArmObjects");
foreach (var rightArmObject in rightArmObjects)
{
rightArmObject.layer = LayerMask.NameToLayer("Default");
}
var rightArmObjects = playerAnimController.GetValue<GameObject[]>("_rightArmObjects").ToList();
rightArmObjects.ForEach(rightArmObject => rightArmObject.layer = LayerMask.NameToLayer("Default"));
body.Find("player_mesh_noSuit:Traveller_HEA_Player/player_mesh_noSuit:Player_Head").gameObject.layer = 0;
body.Find("Traveller_Mesh_v01:Traveller_Geo/Traveller_Mesh_v01:PlayerSuit_Helmet").gameObject.layer = 0;

View File

@ -22,11 +22,12 @@ namespace QSB.ElevatorSync
public override void OnReceiveRemote(ElevatorMessage message)
{
if (!IsInUniverse)
if (!QSBSceneManager.IsInUniverse)
{
return;
}
WorldRegistry.GetObject<QSBElevator>(message.ObjectId).RemoteCall(message.Direction);
var elevator = WorldRegistry.GetObject<QSBElevator>(message.ObjectId);
elevator?.RemoteCall(message.Direction);
}
}
}

View File

@ -14,13 +14,11 @@ namespace QSB.ElevatorSync
private void Awake()
{
Instance = this;
LoadManager.OnCompleteSceneLoad += OnCompleteSceneLoad;
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
QSB.Helper.HarmonyHelper.AddPostfix<Elevator>("StartLift", typeof(ElevatorPatches), nameof(ElevatorPatches.StartLift));
}
private void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene)
private void OnSceneLoaded(OWScene scene, bool isInUniverse)
{
_elevators = Resources.FindObjectsOfTypeAll<Elevator>().ToList();
for (var id = 0; id < _elevators.Count; id++)

View File

@ -14,8 +14,6 @@ namespace QSB.Events
public uint LocalPlayerId => PlayerRegistry.LocalPlayer.NetId;
private readonly MessageHandler<T> _eventHandler;
protected bool IsInUniverse { get; private set; }
protected QSBEvent()
{
_eventHandler = new MessageHandler<T>(Type);
@ -23,12 +21,6 @@ namespace QSB.Events
_eventHandler.OnServerReceiveMessage += OnServerReceive;
SetupListener();
LoadManager.OnCompleteSceneLoad += OnCompleteSceneLoad;
}
private void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene)
{
IsInUniverse = newScene == OWScene.SolarSystem || newScene == OWScene.EyeOfTheUniverse;
}
/// <summary>

View File

@ -22,12 +22,12 @@ namespace QSB.GeyserSync
public override void OnReceiveRemote(GeyserMessage message)
{
if (!IsInUniverse)
if (!QSBSceneManager.IsInUniverse)
{
return;
}
var geyser = WorldRegistry.GetObject<QSBGeyser>(message.ObjectId);
geyser.SetState(message.State);
geyser?.SetState(message.State);
}
}
}

View File

@ -10,11 +10,10 @@ namespace QSB.GeyserSync
private void Awake()
{
Instance = this;
LoadManager.OnCompleteSceneLoad += OnCompleteSceneLoad;
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
}
private void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene)
private void OnSceneLoaded(OWScene scene, bool isInUniverse)
{
var geyserControllers = Resources.FindObjectsOfTypeAll<GeyserController>();
for (var id = 0; id < geyserControllers.Length; id++)

View File

@ -133,6 +133,7 @@
<Compile Include="ElevatorSync\ElevatorPatches.cs" />
<Compile Include="Events\EventNames.cs" />
<Compile Include="DeathSync\PlayerDeathEvent.cs" />
<Compile Include="QSBSceneManager.cs" />
<Compile Include="TimeSync\WakeUpPatches.cs" />
<Compile Include="Tools\PlayerFlashlightEvent.cs" />
<Compile Include="Events\PlayerJoinEvent.cs" />

28
QSB/QSBSceneManager.cs Normal file
View File

@ -0,0 +1,28 @@
using System;
namespace QSB
{
public static class QSBSceneManager
{
public static OWScene CurrentScene => LoadManager.GetCurrentScene();
public static bool IsInUniverse => InUniverse(CurrentScene);
public static event Action<OWScene, bool> OnSceneLoaded;
static QSBSceneManager()
{
LoadManager.OnCompleteSceneLoad += OnCompleteSceneLoad;
}
private static void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene)
{
OnSceneLoaded?.Invoke(newScene, InUniverse(newScene));
}
private static bool InUniverse(OWScene scene)
{
return scene == OWScene.SolarSystem || scene == OWScene.EyeOfTheUniverse;
}
}
}

View File

@ -11,7 +11,7 @@ namespace QSB.TimeSync
public static bool OnStartOfTimeLoopPrefix(ref PlayerCameraEffectController __instance)
{
if (__instance.gameObject.CompareTag("MainCamera") && LoadManager.GetCurrentScene() != OWScene.EyeOfTheUniverse)
if (__instance.gameObject.CompareTag("MainCamera") && QSBSceneManager.CurrentScene != OWScene.EyeOfTheUniverse)
{
__instance.Invoke("WakeUp");
}

View File

@ -37,22 +37,21 @@ namespace QSB.TimeSync
return;
}
var scene = LoadManager.GetCurrentScene();
if (scene == OWScene.SolarSystem || scene == OWScene.EyeOfTheUniverse)
if (QSBSceneManager.IsInUniverse)
{
Init();
}
else
{
LoadManager.OnCompleteSceneLoad += OnCompleteSceneLoad;
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
}
GlobalMessenger.AddListener(EventNames.RestartTimeLoop, OnLoopStart);
}
private void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene)
private void OnSceneLoaded(OWScene scene, bool isInUniverse)
{
if (newScene == OWScene.SolarSystem || newScene == OWScene.EyeOfTheUniverse)
if (isInUniverse)
{
Init();
}
@ -216,7 +215,7 @@ namespace QSB.TimeSync
var diff = _serverTime - Time.timeSinceLevelLoad;
Time.timeScale = Mathf.Lerp(MinFastForwardSpeed, MaxFastForwardSpeed, Mathf.Abs(diff) / MaxFastForwardDiff);
if (LoadManager.GetCurrentScene() == OWScene.SolarSystem && _isFirstFastForward)
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem && _isFirstFastForward)
{
var spawnPoint = Locator.GetPlayerBody().GetComponent<PlayerSpawner>().GetInitialSpawnPoint().transform;
Locator.GetPlayerTransform().position = spawnPoint.position;

View File

@ -22,12 +22,12 @@ namespace QSB.Tools
public override void OnReceiveRemote(ToggleMessage message)
{
var player = PlayerRegistry.GetPlayer(message.SenderId);
player.UpdateState(State.Flashlight, message.ToggleValue);
if (!IsInUniverse)
player?.UpdateState(State.Flashlight, message.ToggleValue);
if (!QSBSceneManager.IsInUniverse)
{
return;
}
player.FlashLight?.UpdateState(message.ToggleValue);
player?.FlashLight?.UpdateState(message.ToggleValue);
}
public override void OnReceiveLocal(ToggleMessage message)

View File

@ -23,7 +23,7 @@ namespace QSB.Tools
{
var player = PlayerRegistry.GetPlayer(message.SenderId);
player?.UpdateState(State.ProbeLauncher, message.ToggleValue);
if (!IsInUniverse)
if (!QSBSceneManager.IsInUniverse)
{
return;
}

View File

@ -22,12 +22,12 @@ namespace QSB.Tools
public override void OnReceiveRemote(ToggleMessage message)
{
var player = PlayerRegistry.GetPlayer(message.SenderId);
player.UpdateState(State.Signalscope, message.ToggleValue);
if (!IsInUniverse)
player?.UpdateState(State.Signalscope, message.ToggleValue);
if (!QSBSceneManager.IsInUniverse)
{
return;
}
player.Signalscope?.ChangeEquipState(message.ToggleValue);
player?.Signalscope?.ChangeEquipState(message.ToggleValue);
}
public override void OnReceiveLocal(ToggleMessage message)

View File

@ -22,12 +22,12 @@ namespace QSB.Tools
public override void OnReceiveRemote(ToggleMessage message)
{
var player = PlayerRegistry.GetPlayer(message.SenderId);
player.UpdateState(State.Translator, message.ToggleValue);
if (!IsInUniverse)
player?.UpdateState(State.Translator, message.ToggleValue);
if (!QSBSceneManager.IsInUniverse)
{
return;
}
player.Translator?.ChangeEquipState(message.ToggleValue);
player?.Translator?.ChangeEquipState(message.ToggleValue);
}
public override void OnReceiveLocal(ToggleMessage message)

View File

@ -22,7 +22,7 @@ namespace QSB.TransformSync
public override void OnReceiveRemote(WorldObjectMessage message)
{
if (!IsInUniverse)
if (!QSBSceneManager.IsInUniverse)
{
return;
}

View File

@ -18,10 +18,10 @@ namespace QSB.TransformSync
private void Awake()
{
Instance = this;
LoadManager.OnCompleteSceneLoad += OnCompleteSceneLoad;
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
}
private void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene)
private void OnSceneLoaded(OWScene scene, bool isInUniverse)
{
var sectors = Resources.FindObjectsOfTypeAll<Sector>().ToList();
for (var id = 0; id < sectors.Count; id++)
@ -43,7 +43,7 @@ namespace QSB.TransformSync
public QSBSector GetStartPlanetSector()
{
var sector = LoadManager.GetCurrentScene() == OWScene.SolarSystem
var sector = QSBSceneManager.CurrentScene == OWScene.SolarSystem
? Locator.GetAstroObject(AstroObject.Name.TimberHearth).GetRootSector()
: Locator.GetAstroObject(AstroObject.Name.Eye).GetRootSector();
return WorldRegistry.GetObjects<QSBSector>()

View File

@ -24,10 +24,10 @@ namespace QSB.TransformSync
{
PlayerRegistry.TransformSyncs.Add(this);
DontDestroyOnLoad(gameObject);
LoadManager.OnCompleteSceneLoad += OnCompleteSceneLoad;
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
}
private void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene)
private void OnSceneLoaded(OWScene scene, bool isInUniverse)
{
_isInitialized = false;
}

View File

@ -12,9 +12,9 @@ namespace QSB.WorldSync
_worldObjects.Add(worldObject);
}
public static List<T> GetObjects<T>()
public static IEnumerable<T> GetObjects<T>()
{
return _worldObjects.OfType<T>().ToList();
return _worldObjects.OfType<T>();
}
public static T GetObject<T>(int id) where T : WorldObject