diff --git a/QSB/Player/PlayerEntanglementWatcher.cs b/QSB/Player/PlayerEntanglementWatcher.cs index 3e6b8e66..e95b1248 100644 --- a/QSB/Player/PlayerEntanglementWatcher.cs +++ b/QSB/Player/PlayerEntanglementWatcher.cs @@ -12,6 +12,10 @@ namespace QSB.Player private void Update() { + if (!QSBCore.IsInMultiplayer) + { + return; + } var controller = Locator.GetPlayerController(); if (controller == null) { diff --git a/QSB/QSB.csproj b/QSB/QSB.csproj index f7657ebb..ea10f418 100644 --- a/QSB/QSB.csproj +++ b/QSB/QSB.csproj @@ -238,7 +238,7 @@ - + @@ -287,7 +287,7 @@ - + diff --git a/QSB/QSBCore.cs b/QSB/QSBCore.cs index 408638ec..9790ee66 100644 --- a/QSB/QSBCore.cs +++ b/QSB/QSBCore.cs @@ -13,6 +13,7 @@ using QSB.QuantumSync.WorldObjects; using QSB.SectorSync; using QSB.StatueSync; using QSB.TimeSync; +using QSB.TransformSync; using QSB.TranslationSync; using QSB.Utility; using QSB.WorldSync; @@ -151,9 +152,9 @@ namespace QSB var offset3 = 10f; GUI.Label(new Rect(420, offset3, 200f, 20f), $"Current sectors :"); offset3 += _debugLineSpacing; - foreach (var obj in QSBSectorManager.Instance.SectorList) + foreach (var obj in PlayerTransformSync.LocalInstance?.SectorSync.SectorList) { - GUI.Label(new Rect(420, offset3, 200f, 20f), $"- {obj.AttachedObject.name}"); + GUI.Label(new Rect(420, offset3, 200f, 20f), $"- {obj.AttachedObject.name} : {Vector3.Distance(obj.Position, Locator.GetPlayerTransform().position)}"); offset3 += _debugLineSpacing; } diff --git a/QSB/QSBNetworkManager.cs b/QSB/QSBNetworkManager.cs index 69851e2b..d038565c 100644 --- a/QSB/QSBNetworkManager.cs +++ b/QSB/QSBNetworkManager.cs @@ -174,7 +174,7 @@ namespace QSB QSBEventManager.Init(); - gameObject.AddComponent(); + gameObject.AddComponent(); gameObject.AddComponent(); if (QSBSceneManager.IsInUniverse) @@ -209,7 +209,7 @@ namespace QSB { DebugLog.DebugWrite("OnStopClient", MessageType.Info); DebugLog.ToConsole("Disconnecting from server...", MessageType.Info); - Destroy(GetComponent()); + Destroy(GetComponent()); Destroy(GetComponent()); QSBEventManager.Reset(); QSBPlayerManager.PlayerList.ForEach(player => player.HudMarker?.Remove()); @@ -256,7 +256,7 @@ namespace QSB public override void OnStopServer() { DebugLog.DebugWrite("OnStopServer", MessageType.Info); - Destroy(GetComponent()); + Destroy(GetComponent()); Destroy(GetComponent()); QSBEventManager.Reset(); DebugLog.ToConsole("Server stopped!", MessageType.Info); diff --git a/QSB/SectorSync/QSBSectorManager.cs b/QSB/SectorSync/QSBSectorManager.cs index 831d3ab7..3ae3026e 100644 --- a/QSB/SectorSync/QSBSectorManager.cs +++ b/QSB/SectorSync/QSBSectorManager.cs @@ -1,24 +1,27 @@ using OWML.Common; +using QSB.Events; +using QSB.Player; using QSB.SectorSync.WorldObjects; using QSB.Utility; using QSB.WorldSync; -using System.Collections.Generic; using System.Linq; using UnityEngine; namespace QSB.SectorSync { - public class QSBSectorManager : MonoBehaviour + public class QSBSectorManager : MonoBehaviour, IRepeating { public static QSBSectorManager Instance { get; private set; } - - public List SectorList = new List(); public bool IsReady { get; private set; } - private readonly Sector.Name[] _sectorBlacklist = + private void OnEnable() => RepeatingManager.Repeatings.Add(this); + private void OnDisable() => RepeatingManager.Repeatings.Remove(this); + + public void Invoke() { - Sector.Name.Ship - }; + QSBPlayerManager.GetSyncObjects() + .Where(x => x.HasAuthority).ToList().ForEach(CheckTransformSyncSector); + } public void Awake() { @@ -27,72 +30,37 @@ namespace QSB.SectorSync DebugLog.DebugWrite("Sector Manager ready.", MessageType.Success); } - public void OnDestroy() - { - QSBSceneManager.OnUniverseSceneLoaded -= (OWScene scene) => RebuildSectors(); - FindObjectOfType().OnEnterSector -= AddSector; - FindObjectOfType().OnExitSector -= RemoveSector; - } + public void OnDestroy() + => QSBSceneManager.OnUniverseSceneLoaded -= (OWScene scene) => RebuildSectors(); public void RebuildSectors() { DebugLog.DebugWrite("Rebuilding sectors...", MessageType.Warning); QSBWorldSync.Init(); - SectorList.Clear(); IsReady = QSBWorldSync.GetWorldObjects().Any(); - - FindObjectOfType().OnEnterSector += AddSector; - FindObjectOfType().OnExitSector += RemoveSector; } - private void AddSector(Sector sector) + private void CheckTransformSyncSector(TransformSync.TransformSync transformSync) { - var worldObject = QSBWorldSync.GetWorldFromUnity(sector); - if (worldObject == null) + var syncedTransform = transformSync.SyncedTransform; + if (syncedTransform == null || syncedTransform.position == Vector3.zero) { - DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error); - } - if (SectorList.Contains(worldObject)) - { - DebugLog.ToConsole($"Warning - Trying to add {sector.name}, but is already in list", MessageType.Warning); return; } - SectorList.Add(worldObject); - } - - private void RemoveSector(Sector sector) - { - var worldObject = QSBWorldSync.GetWorldFromUnity(sector); - if (worldObject == null) + var closestSector = transformSync.SectorSync.GetClosestSector(syncedTransform); + if (closestSector == default(QSBSector)) { - DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error); return; } - if (!SectorList.Contains(worldObject)) + if (closestSector == transformSync.ReferenceSector) { - DebugLog.ToConsole($"Warning - Trying to remove {sector.name}, but is not in list!", MessageType.Warning); return; } - SectorList.Remove(worldObject); + transformSync.SetReferenceSector(closestSector); + SendSector(transformSync.NetId.Value, closestSector); } - public QSBSector GetClosestSector(Transform trans) // trans rights \o/ - { - if (!IsReady) - { - DebugLog.ToConsole($"Warning - Tried to get closest sector to {trans.name} before SectorManager was ready.", MessageType.Warning); - return null; - } - - var listToCheck = SectorList.Count == 0 - ? QSBWorldSync.GetWorldObjects() - : SectorList; - return listToCheck - .Where(sector => sector.AttachedObject != null - && !_sectorBlacklist.Contains(sector.Type) - && sector.Transform.gameObject.activeInHierarchy) - .OrderBy(sector => Vector3.Distance(sector.Position, trans.position)) - .FirstOrDefault(); - } + private void SendSector(uint id, QSBSector sector) => + QSBEventManager.FireEvent(EventNames.QSBSectorChange, id, sector); } } \ No newline at end of file diff --git a/QSB/SectorSync/SectorSync.cs b/QSB/SectorSync/SectorSync.cs index 96a9d253..b917d9b4 100644 --- a/QSB/SectorSync/SectorSync.cs +++ b/QSB/SectorSync/SectorSync.cs @@ -1,48 +1,81 @@ -using QSB.Events; -using QSB.Player; +using OWML.Common; using QSB.SectorSync.WorldObjects; using QSB.Utility; +using QSB.WorldSync; +using System.Collections.Generic; using System.Linq; using UnityEngine; namespace QSB.SectorSync { - public class SectorSync : MonoBehaviour, IRepeating + public class SectorSync : MonoBehaviour { - private void OnEnable() => RepeatingManager.Repeatings.Add(this); - private void OnDisable() => RepeatingManager.Repeatings.Remove(this); + public List SectorList = new List(); - public void Invoke() + private SectorDetector _sectorDetector; + private readonly Sector.Name[] _sectorBlacklist = { Sector.Name.Ship }; + + public void SetSectorDetector(SectorDetector detector) + { + if (_sectorDetector != null) + { + _sectorDetector.OnEnterSector -= AddSector; + _sectorDetector.OnExitSector -= RemoveSector; + } + _sectorDetector = detector; + _sectorDetector.OnEnterSector += AddSector; + _sectorDetector.OnExitSector += RemoveSector; + } + + private void AddSector(Sector sector) + { + var worldObject = QSBWorldSync.GetWorldFromUnity(sector); + if (worldObject == null) + { + DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error); + } + if (SectorList.Contains(worldObject)) + { + DebugLog.ToConsole($"Warning - Trying to add {sector.name}, but is already in list", MessageType.Warning); + return; + } + SectorList.Add(worldObject); + } + + private void RemoveSector(Sector sector) + { + var worldObject = QSBWorldSync.GetWorldFromUnity(sector); + if (worldObject == null) + { + DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error); + return; + } + if (!SectorList.Contains(worldObject)) + { + DebugLog.ToConsole($"Warning - Trying to remove {sector.name}, but is not in list!", MessageType.Warning); + return; + } + SectorList.Remove(worldObject); + } + + public QSBSector GetClosestSector(Transform trans) // trans rights \o/ { if (!QSBSectorManager.Instance.IsReady) { - return; + DebugLog.ToConsole($"Warning - Tried to get closest sector to {trans.name} before QSBSectorManager was ready.", MessageType.Warning); + return null; } - QSBPlayerManager.GetSyncObjects() - .Where(x => x.HasAuthority).ToList().ForEach(CheckTransformSyncSector); - } - private void CheckTransformSyncSector(TransformSync.TransformSync transformSync) - { - var syncedTransform = transformSync.SyncedTransform; - if (syncedTransform == null || syncedTransform.position == Vector3.zero) - { - return; - } - var closestSector = QSBSectorManager.Instance.GetClosestSector(syncedTransform); - if (closestSector == default(QSBSector)) - { - return; - } - if (closestSector == transformSync.ReferenceSector) - { - return; - } - transformSync.SetReferenceSector(closestSector); - SendSector(transformSync.NetId.Value, closestSector); + var listToCheck = SectorList.Count == 0 + ? QSBWorldSync.GetWorldObjects() + : SectorList; + // TODO : maybe improve this by more carefully picking sectors that are in the same place? maybe by last entered or closest to edge? + return listToCheck + .Where(sector => sector.AttachedObject != null + && !_sectorBlacklist.Contains(sector.Type) + && sector.Transform.gameObject.activeInHierarchy) + .OrderBy(sector => Vector3.Distance(sector.Position, trans.position)) + .FirstOrDefault(); } - - private void SendSector(uint id, QSBSector sector) => - QSBEventManager.FireEvent(EventNames.QSBSectorChange, id, sector); } } \ No newline at end of file diff --git a/QSB/TransformSync/PlayerCameraSync.cs b/QSB/TransformSync/PlayerCameraSync.cs index f060ab92..f03d2509 100644 --- a/QSB/TransformSync/PlayerCameraSync.cs +++ b/QSB/TransformSync/PlayerCameraSync.cs @@ -10,6 +10,7 @@ namespace QSB.TransformSync { protected override Transform InitLocalTransform() { + SectorSync.SetSectorDetector(Locator.GetPlayerSectorDetector()); var body = Locator.GetPlayerCamera().gameObject.transform; Player.Camera = Locator.GetPlayerCamera(); diff --git a/QSB/TransformSync/PlayerProbeSync.cs b/QSB/TransformSync/PlayerProbeSync.cs index 0cdc42ff..7cb55252 100644 --- a/QSB/TransformSync/PlayerProbeSync.cs +++ b/QSB/TransformSync/PlayerProbeSync.cs @@ -15,6 +15,7 @@ namespace QSB.TransformSync protected override Transform InitLocalTransform() { + SectorSync.SetSectorDetector(Locator.GetProbe().GetSectorDetector()); var body = GetProbe(); SetSocket(Player.CameraBody.transform); diff --git a/QSB/TransformSync/PlayerTransformSync.cs b/QSB/TransformSync/PlayerTransformSync.cs index 1a3821bd..e5556ec8 100644 --- a/QSB/TransformSync/PlayerTransformSync.cs +++ b/QSB/TransformSync/PlayerTransformSync.cs @@ -29,6 +29,7 @@ namespace QSB.TransformSync protected override Transform InitLocalTransform() { + SectorSync.SetSectorDetector(Locator.GetPlayerSectorDetector()); var body = GetPlayerModel(); GetComponent().InitLocal(body); diff --git a/QSB/TransformSync/ShipTransformSync.cs b/QSB/TransformSync/ShipTransformSync.cs index 85782447..81cf02ce 100644 --- a/QSB/TransformSync/ShipTransformSync.cs +++ b/QSB/TransformSync/ShipTransformSync.cs @@ -7,8 +7,11 @@ namespace QSB.TransformSync { private Transform GetShipModel() => Locator.GetShipTransform(); - protected override Transform InitLocalTransform() => - GetShipModel().Find("Module_Cockpit/Geo_Cockpit/Cockpit_Geometry/Cockpit_Exterior"); + protected override Transform InitLocalTransform() + { + SectorSync.SetSectorDetector(Locator.GetShipDetector().GetComponent()); + return GetShipModel().Find("Module_Cockpit/Geo_Cockpit/Cockpit_Geometry/Cockpit_Exterior"); + } protected override Transform InitRemoteTransform() { diff --git a/QSB/TransformSync/TransformSync.cs b/QSB/TransformSync/TransformSync.cs index 0497355b..b8ce22eb 100644 --- a/QSB/TransformSync/TransformSync.cs +++ b/QSB/TransformSync/TransformSync.cs @@ -17,6 +17,7 @@ namespace QSB.TransformSync public Transform SyncedTransform { get; private set; } public QSBSector ReferenceSector { get; set; } + public SectorSync.SectorSync SectorSync { get; private set; } private const float SmoothTime = 0.1f; private const float DistanceLeeway = 5f; @@ -33,6 +34,8 @@ namespace QSB.TransformSync .Where(x => x.NetId.Value <= NetId.Value).OrderBy(x => x.NetId.Value).Last(); NetIdentity.SetRootIdentity(lowestBound.NetIdentity); + SectorSync = gameObject.AddComponent(); + DontDestroyOnLoad(gameObject); QSBSceneManager.OnSceneLoaded += OnSceneLoaded; } @@ -53,7 +56,7 @@ namespace QSB.TransformSync protected void Init() { SyncedTransform = HasAuthority ? InitLocalTransform() : InitRemoteTransform(); - SetReferenceSector(QSBSectorManager.Instance.GetClosestSector(SyncedTransform)); + SetReferenceSector(SectorSync.GetClosestSector(SyncedTransform)); _isInitialized = true; _isVisible = true; }