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

99 lines
2.3 KiB
C#
Raw Normal View History

2022-01-29 04:49:07 +00:00
using Cysharp.Threading.Tasks;
using OWML.Common;
using QSB.SectorSync.WorldObjects;
2021-08-14 14:03:01 +00: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 13:04:13 +00:00
using System.Linq;
2022-01-29 04:49:07 +00:00
using System.Threading;
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.SectorSync;
public class QSBSectorManager : WorldObjectManager
{
2022-03-03 03:46:33 +00:00
public override WorldObjectScene WorldObjectScene => WorldObjectScene.Both;
2022-03-03 03:46:33 +00:00
public static QSBSectorManager Instance { get; private set; }
private bool _isReady;
public readonly List<QSBSector> FakeSectors = new();
2022-03-03 03:46:33 +00:00
public readonly List<BaseSectoredSync> SectoredSyncs = new();
2021-03-09 19:45:00 +00:00
2022-03-03 03:46:33 +00:00
private const float UpdateInterval = 0.4f;
private float _timer = UpdateInterval;
2021-12-18 20:25:16 +00:00
2022-03-03 03:46:33 +00:00
private void Update()
{
_timer += Time.unscaledDeltaTime;
if (_timer < UpdateInterval)
2021-12-18 20:25:16 +00:00
{
2022-03-03 03:46:33 +00:00
return;
}
2022-03-03 03:46:33 +00:00
_timer = 0;
UpdateReferenceSectors();
}
2022-03-03 03:46:33 +00:00
public void UpdateReferenceSectors()
{
if (!Instance._isReady || !QSBWorldSync.AllObjectsReady)
{
return;
2021-03-09 19:45:00 +00:00
}
2022-03-03 03:46:33 +00:00
foreach (var sync in SectoredSyncs)
2022-01-22 00:52:30 +00:00
{
2022-03-03 03:46:33 +00:00
if (sync.hasAuthority
&& sync.IsValid
&& sync.AttachedTransform.gameObject.activeInHierarchy)
2022-01-22 00:52:30 +00:00
{
2022-03-03 03:46:33 +00:00
UpdateReferenceSector(sync);
2022-01-22 00:52:30 +00:00
}
}
2022-03-03 03:46:33 +00:00
}
2022-01-22 00:52:30 +00:00
2022-03-03 03:46:33 +00: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-03 03:46:33 +00:00
return;
2020-12-02 21:23:01 +00:00
}
2020-12-03 08:28:05 +00:00
2022-03-03 03:46:33 +00: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);
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
var timeLoopRing = GameObject.Find("TimeLoopRing_Body");
if (timeLoopRing != null)
2021-03-11 20:02:23 +00:00
{
2022-03-03 03:46:33 +00:00
if (timeLoopRing.GetComponent<FakeSector>() == null)
{
2022-03-03 03:46:33 +00:00
timeLoopRing.AddComponent<FakeSector>().AttachedSector = GameObject.Find("Sector_TimeLoopInterior").GetComponent<Sector>();
2021-03-11 20:02:23 +00:00
}
}
2022-03-03 03:46:33 +00:00
else
{
DebugLog.ToConsole($"Error - TimeLoopRing_Body not found!", MessageType.Error);
}
2020-12-02 21:23:01 +00:00
}
2022-03-03 03:46:33 +00:00
QSBWorldSync.Init<QSBSector, Sector>();
_isReady = QSBWorldSync.GetWorldObjects<QSBSector>().Any();
2020-12-02 21:23:01 +00:00
}
2022-03-03 03:46:33 +00:00
public override void UnbuildWorldObjects() =>
_isReady = false;
}