using OWML.Common; using QSB.SectorSync.WorldObjects; using QSB.Syncs; using QSB.Utility; using QSB.WorldSync; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace QSB.SectorSync { public class QSBSectorManager : WorldObjectManager, IRepeating { public static QSBSectorManager Instance { get; private set; } public bool IsReady { get; private set; } public List FakeSectors = new List(); private void OnEnable() => RepeatingManager.Repeatings.Add(this); private void OnDisable() => RepeatingManager.Repeatings.Remove(this); public List SectoredTransformSyncs = new List(); public List SectoredRigidbodySyncs = new List(); public void Invoke() { foreach (var sync in SectoredTransformSyncs) { if (sync.AttachedObject == null) { continue; } if (sync.HasAuthority && sync.AttachedObject.gameObject.activeInHierarchy && sync.IsReady) { CheckTransformSyncSector(sync as ISectoredSync); } } foreach (var sync in SectoredRigidbodySyncs) { if (sync.AttachedObject == null) { continue; } if (sync.HasAuthority && sync.AttachedObject.gameObject.activeInHierarchy && sync.IsReady) { CheckTransformSyncSector(sync as ISectoredSync); } } } public override void Awake() { base.Awake(); Instance = this; DebugLog.DebugWrite("Sector Manager ready.", MessageType.Success); } protected override void RebuildWorldObjects(OWScene scene) { DebugLog.DebugWrite("Rebuilding sectors...", MessageType.Warning); if (QSBSceneManager.CurrentScene == OWScene.SolarSystem) { var timeLoopRing = GameObject.Find("TimeLoopRing_Body"); if (timeLoopRing != null) { if (timeLoopRing.GetComponent() == null) { timeLoopRing.AddComponent().AttachedSector = GameObject.Find("Sector_TimeLoopInterior").GetComponent(); } } else { DebugLog.ToConsole($"Error - TimeLoopRing_Body not found!", MessageType.Error); } } QSBWorldSync.Init(); IsReady = QSBWorldSync.GetWorldObjects().Any(); } private void CheckTransformSyncSector(ISectoredSync transformSync) where T : Component { var attachedObject = (transformSync as SyncBase).AttachedObject; var closestSector = transformSync.SectorSync.GetClosestSector(attachedObject.transform); if (closestSector == default(QSBSector)) { return; } if (closestSector == transformSync.ReferenceSector) { return; } DebugLog.DebugWrite($"LOCAL Change {attachedObject.name} to sector {closestSector.Name}"); transformSync.SetReferenceSector(closestSector); } } }