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-03-02 19:46:33 -08:00
|
|
|
|
namespace QSB.SectorSync;
|
|
|
|
|
|
|
|
|
|
public class QSBSectorManager : WorldObjectManager
|
2020-08-16 17:15:36 +02:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override WorldObjectScene WorldObjectScene => WorldObjectScene.Both;
|
2021-12-20 18:41:12 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public static QSBSectorManager Instance { get; private set; }
|
|
|
|
|
private bool _isReady;
|
2020-08-16 17:15:36 +02:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public readonly List<BaseSectoredSync> SectoredSyncs = new();
|
2021-03-09 19:45:00 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
private const float UpdateInterval = 0.4f;
|
|
|
|
|
private float _timer = UpdateInterval;
|
2021-12-18 12:25:16 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
_timer += Time.unscaledDeltaTime;
|
|
|
|
|
if (_timer < UpdateInterval)
|
2021-12-18 12:25:16 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
return;
|
2022-02-27 04:40:44 -08:00
|
|
|
|
}
|
2021-07-17 09:53:13 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
_timer = 0;
|
|
|
|
|
UpdateReferenceSectors();
|
|
|
|
|
}
|
2022-02-27 04:40:44 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void UpdateReferenceSectors()
|
|
|
|
|
{
|
|
|
|
|
if (!Instance._isReady || !QSBWorldSync.AllObjectsReady)
|
|
|
|
|
{
|
|
|
|
|
return;
|
2021-03-09 19:45:00 +00:00
|
|
|
|
}
|
2020-08-16 17:15:36 +02:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
foreach (var sync in SectoredSyncs)
|
2022-01-21 16:52:30 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
if (sync.hasAuthority
|
2022-03-19 22:55:58 -07:00
|
|
|
|
&& sync.IsValid
|
|
|
|
|
&& sync.AttachedTransform.gameObject.activeInHierarchy)
|
2022-01-21 16:52:30 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
UpdateReferenceSector(sync);
|
2022-01-21 16:52:30 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
2022-01-21 16:52:30 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
private static void UpdateReferenceSector(BaseSectoredSync sync)
|
|
|
|
|
{
|
|
|
|
|
var closestSector = sync.SectorDetector.GetClosestSector();
|
|
|
|
|
if (closestSector == null)
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
return;
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
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);
|
2022-03-20 00:02:48 -07:00
|
|
|
|
await CreateFakeSectors(ct);
|
2020-08-16 17:15:36 +02:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
QSBWorldSync.Init<QSBSector, Sector>();
|
|
|
|
|
_isReady = QSBWorldSync.GetWorldObjects<QSBSector>().Any();
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
|
|
|
|
|
public override void UnbuildWorldObjects() =>
|
|
|
|
|
_isReady = false;
|
2022-03-20 00:02:48 -07:00
|
|
|
|
|
|
|
|
|
private static async UniTask CreateFakeSectors(CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
if (QSBSceneManager.CurrentScene != OWScene.SolarSystem)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var TimeLoopRing_Body = GameObject.Find("TimeLoopRing_Body");
|
|
|
|
|
var Sector_TimeLoopInterior = GameObject.Find("Sector_TimeLoopInterior").GetComponent<Sector>();
|
|
|
|
|
// use same radius as parent sector
|
|
|
|
|
var radius = Sector_TimeLoopInterior.GetTriggerVolume().GetShape().CalcWorldBounds().radius;
|
|
|
|
|
FakeSector.CreateOn(TimeLoopRing_Body, radius, Sector_TimeLoopInterior);
|
|
|
|
|
|
|
|
|
|
foreach (var elevator in QSBWorldSync.GetUnityObjects<Elevator>())
|
|
|
|
|
{
|
2022-03-20 00:36:42 -07:00
|
|
|
|
var colliders = elevator.GetComponentsInChildren<Collider>();
|
|
|
|
|
await colliders.Select(x =>
|
|
|
|
|
UniTask.WaitUntil(() => x.bounds.extents != Vector3.zero, cancellationToken: ct));
|
|
|
|
|
|
|
|
|
|
static float Size(Collider collider)
|
2022-03-20 00:02:48 -07:00
|
|
|
|
{
|
2022-03-20 00:36:42 -07:00
|
|
|
|
var extents = collider.bounds.extents;
|
|
|
|
|
return Mathf.Max(extents.x, extents.y, extents.z);
|
2022-03-20 00:02:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 00:36:42 -07:00
|
|
|
|
var largestCollider = colliders.MaxBy(Size);
|
|
|
|
|
FakeSector.CreateOn(largestCollider.gameObject,
|
|
|
|
|
Size(largestCollider),
|
|
|
|
|
largestCollider.GetComponentInParent<Sector>());
|
2022-03-20 00:02:48 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-19 22:55:58 -07:00
|
|
|
|
}
|