using Cysharp.Threading.Tasks; using OWML.Common; using QSB.SectorSync.WorldObjects; using QSB.Syncs.Sectored; using QSB.Utility; using QSB.WorldSync; using System.Collections.Generic; using System.Linq; using System.Threading; using UnityEngine; namespace QSB.SectorSync; public class QSBSectorManager : WorldObjectManager { public override WorldObjectScene WorldObjectScene => WorldObjectScene.Both; public static QSBSectorManager Instance { get; private set; } private bool _isReady; public readonly List FakeSectors = new(); public readonly List SectoredSyncs = new(); private const float UpdateInterval = 0.4f; private float _timer = UpdateInterval; private void Update() { _timer += Time.unscaledDeltaTime; if (_timer < UpdateInterval) { return; } _timer = 0; UpdateReferenceSectors(); } public void UpdateReferenceSectors() { if (!Instance._isReady || !QSBWorldSync.AllObjectsReady) { return; } foreach (var sync in SectoredSyncs) { if (sync.hasAuthority && sync.IsValid && sync.AttachedTransform.gameObject.activeInHierarchy) { UpdateReferenceSector(sync); } } } private static void UpdateReferenceSector(BaseSectoredSync sync) { var closestSector = sync.SectorDetector.GetClosestSector(); if (closestSector == null) { return; } sync.SetReferenceSector(closestSector); } public void Awake() { Instance = this; DebugLog.DebugWrite("Sector Manager ready.", MessageType.Success); } public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct) { DebugLog.DebugWrite("Building sectors...", MessageType.Info); 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(); } public override void UnbuildWorldObjects() => _isReady = false; }