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

150 lines
3.5 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
{
2023-05-08 11:27:32 -07:00
if (sync.isOwned
&& 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-21 16:37:08 -07:00
this.Try("creating fake sectors", CreateFakeSectors);
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-21 16:37:08 -07:00
private static void CreateFakeSectors()
{
if (QSBSceneManager.CurrentScene != OWScene.SolarSystem)
{
return;
}
// time loop spinning ring
{
2022-08-29 21:31:13 -07:00
// NH can remove this
var TimeLoopRing_Body = GameObject.Find("TimeLoopRing_Body");
2022-08-29 21:31:13 -07:00
if (TimeLoopRing_Body)
{
var Sector_TimeLoopInterior = GameObject.Find("Sector_TimeLoopInterior").GetComponent<Sector>();
// use the same trigger as the parent sector
FakeSector.Create(TimeLoopRing_Body, Sector_TimeLoopInterior,
x => x._triggerRoot = Sector_TimeLoopInterior._triggerRoot);
}
}
2022-03-20 18:33:23 -07:00
// TH elevators
foreach (var elevator in QSBWorldSync.GetUnityObjects<Elevator>())
{
// just create a sphere at the attach point lol
// since players will be moved there when riding the elevator
FakeSector.Create(elevator._attachPoint.gameObject,
elevator.GetComponentInParent<Sector>(),
x =>
{
x.gameObject.AddComponent<OWTriggerVolume>();
x.gameObject.AddComponent<SphereShape>();
});
}
// rafts
foreach (var raft in QSBWorldSync.GetUnityObjects<RaftController>())
{
FakeSector.Create(raft.gameObject,
raft._sector,
x => x._triggerRoot = raft._rideVolume.gameObject);
}
// todo cage elevators
// todo prisoner elevator
2022-04-08 22:26:58 -07:00
// todo black hole forge
// OPC probe
{
var probe = Locator._orbitalProbeCannon
.GetRequiredComponent<OrbitalProbeLaunchController>()
._probeBody;
if (probe)
{
// just create a big circle around the probe lol
FakeSector.Create(probe.gameObject,
null,
x =>
{
x.gameObject.AddComponent<OWTriggerVolume>();
x.gameObject.AddComponent<SphereShape>().radius = 100;
});
}
}
}
}