2022-01-28 20:49:07 -08:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using OWML.Common;
|
2020-12-31 12:10:55 +00:00
|
|
|
|
using QSB.SectorSync.WorldObjects;
|
2021-08-14 15:03:01 +01:00
|
|
|
|
using QSB.Syncs.Sectored;
|
2020-11-04 09:34:01 +00:00
|
|
|
|
using QSB.Utility;
|
|
|
|
|
using QSB.WorldSync;
|
2021-03-11 20:02:23 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-08-21 14:04:13 +01:00
|
|
|
|
using System.Linq;
|
2022-01-28 20:49:07 -08:00
|
|
|
|
using System.Threading;
|
2020-08-16 17:15:36 +02:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
namespace QSB.SectorSync
|
2020-08-16 17:15:36 +02:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public class QSBSectorManager : WorldObjectManager
|
|
|
|
|
{
|
|
|
|
|
public override WorldObjectScene WorldObjectScene => WorldObjectScene.Both;
|
2021-12-20 18:41:12 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public static QSBSectorManager Instance { get; private set; }
|
|
|
|
|
private bool _isReady;
|
|
|
|
|
public readonly List<QSBSector> FakeSectors = new();
|
2020-08-16 17:15:36 +02:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public readonly List<BaseSectoredSync> SectoredSyncs = new();
|
2021-03-09 19:45:00 +00:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
private const float UpdateInterval = 0.4f;
|
|
|
|
|
private float _timer = UpdateInterval;
|
2021-12-18 12:25:16 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
private void Update()
|
2021-12-18 12:25:16 -08:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
_timer += Time.unscaledDeltaTime;
|
|
|
|
|
if (_timer < UpdateInterval)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-18 12:25:16 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
_timer = 0;
|
|
|
|
|
UpdateReferenceSectors();
|
|
|
|
|
}
|
2021-07-17 09:53:13 +01:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public void UpdateReferenceSectors()
|
2022-02-24 22:04:54 -08:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
if (!Instance._isReady || !QSBWorldSync.AllObjectsReady)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var sync in SectoredSyncs)
|
|
|
|
|
{
|
|
|
|
|
if (sync.hasAuthority
|
|
|
|
|
&& sync.IsValid
|
|
|
|
|
&& sync.AttachedTransform.gameObject.activeInHierarchy)
|
|
|
|
|
{
|
|
|
|
|
UpdateReferenceSector(sync);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-09 19:45:00 +00:00
|
|
|
|
}
|
2020-08-16 17:15:36 +02:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
private static void UpdateReferenceSector(BaseSectoredSync sync)
|
2022-01-21 16:52:30 -08:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
var closestSector = sync.SectorDetector.GetClosestSector();
|
|
|
|
|
if (closestSector == null)
|
2022-01-21 16:52:30 -08:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
return;
|
2022-01-21 16:52:30 -08:00
|
|
|
|
}
|
2022-02-27 04:40:44 -08:00
|
|
|
|
|
|
|
|
|
sync.SetReferenceSector(closestSector);
|
2022-01-21 16:52:30 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public void Awake()
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
Instance = this;
|
|
|
|
|
DebugLog.DebugWrite("Sector Manager ready.", MessageType.Success);
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct)
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
DebugLog.DebugWrite("Building sectors...", MessageType.Info);
|
|
|
|
|
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
|
2021-03-11 20:02:23 +00:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
var timeLoopRing = GameObject.Find("TimeLoopRing_Body");
|
|
|
|
|
if (timeLoopRing != null)
|
2021-03-11 20:02:23 +00:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
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);
|
2021-03-11 20:02:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-27 04:40:44 -08:00
|
|
|
|
|
|
|
|
|
QSBWorldSync.Init<QSBSector, Sector>();
|
|
|
|
|
_isReady = QSBWorldSync.GetWorldObjects<QSBSector>().Any();
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2020-08-16 17:15:36 +02:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public override void UnbuildWorldObjects() =>
|
|
|
|
|
_isReady = false;
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2022-02-24 22:04:54 -08:00
|
|
|
|
}
|