mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-10 15:53:19 +00:00
99 lines
2.3 KiB
C#
99 lines
2.3 KiB
C#
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<QSBSector> FakeSectors = new();
|
|
|
|
public readonly List<BaseSectoredSync> 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<FakeSector>() == null)
|
|
{
|
|
timeLoopRing.AddComponent<FakeSector>().AttachedSector = GameObject.Find("Sector_TimeLoopInterior").GetComponent<Sector>();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DebugLog.ToConsole($"Error - TimeLoopRing_Body not found!", MessageType.Error);
|
|
}
|
|
}
|
|
|
|
QSBWorldSync.Init<QSBSector, Sector>();
|
|
_isReady = QSBWorldSync.GetWorldObjects<QSBSector>().Any();
|
|
}
|
|
|
|
public override void UnbuildWorldObjects() =>
|
|
_isReady = false;
|
|
} |