quantum-space-buddies/QSB/SectorSync/QSBSectorManager.cs

117 lines
2.9 KiB
C#
Raw Normal View History

2022-01-28 20:49:07 -08:00
using Cysharp.Threading.Tasks;
using OWML.Common;
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;
using UnityEngine;
2022-03-02 19:46:33 -08:00
namespace QSB.SectorSync;
public class QSBSectorManager : WorldObjectManager
{
2022-03-02 19:46:33 -08:00
public override WorldObjectScene WorldObjectScene => WorldObjectScene.Both;
2022-03-02 19:46:33 -08:00
public static QSBSectorManager Instance { get; private set; }
private bool _isReady;
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-03-02 19:46:33 -08:00
_timer = 0;
UpdateReferenceSectors();
}
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
}
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
&& 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);
await CreateFakeSectors(ct);
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;
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>())
{
var colliders = elevator.GetComponentsInChildren<Collider>();
await colliders.Select(x =>
UniTask.WaitUntil(() => x.bounds.extents != Vector3.zero, cancellationToken: ct));
static float Size(Collider collider)
{
var extents = collider.bounds.extents;
return Mathf.Max(extents.x, extents.y, extents.z);
}
var largestCollider = colliders.MaxBy(Size);
FakeSector.CreateOn(largestCollider.gameObject,
Size(largestCollider),
largestCollider.GetComponentInParent<Sector>());
}
}
}