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

105 lines
2.4 KiB
C#
Raw Normal View History

2020-11-04 09:34:01 +00:00
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;
using UnityEngine;
2020-11-03 22:29:23 +00:00
namespace QSB.SectorSync
{
public class QSBSectorManager : WorldObjectManager
2020-12-02 21:23:01 +00:00
{
public override WorldObjectType WorldObjectType => WorldObjectType.Both;
2020-12-02 21:23:01 +00:00
public static QSBSectorManager Instance { get; private set; }
public bool IsReady { get; private set; }
2021-12-18 12:26:02 -08:00
public readonly List<QSBSector> FakeSectors = new();
2022-01-16 05:38:06 -08:00
public readonly List<BaseSectoredSync> TransformSyncs = new();
2021-03-09 19:45:00 +00:00
2022-01-16 05:38:06 -08:00
private const float UpdateInterval = 0.4f;
private float _timer = UpdateInterval;
2021-12-18 12:25:16 -08:00
private void Update()
{
2022-01-16 05:38:06 -08:00
_timer += Time.unscaledDeltaTime;
if (_timer < UpdateInterval)
2021-12-18 12:25:16 -08:00
{
return;
}
2022-01-16 05:38:06 -08:00
_timer = 0;
UpdateReferenceSectors();
2021-12-18 12:25:16 -08:00
}
2022-01-16 05:38:06 -08:00
public void UpdateReferenceSectors()
2020-12-02 21:23:01 +00:00
{
if (!Instance.IsReady || !QSBWorldSync.AllObjectsReady)
{
return;
}
2022-01-16 05:38:06 -08:00
foreach (var sync in TransformSyncs)
2021-04-21 11:02:17 +01:00
{
2022-01-14 22:39:41 -08:00
if (sync.AttachedTransform == null)
2021-04-21 11:02:17 +01:00
{
continue;
}
2021-06-18 22:38:32 +01:00
2022-01-14 22:39:41 -08:00
if (sync.hasAuthority
&& sync.AttachedTransform.gameObject.activeInHierarchy
2022-01-16 05:38:06 -08:00
&& sync.IsInitialized
&& sync.SectorDetector.IsReady)
{
2022-01-16 05:38:06 -08:00
UpdateReferenceSector(sync);
}
}
2021-03-09 19:45:00 +00:00
}
public void Awake()
2020-12-02 21:23:01 +00:00
{
Instance = this;
DebugLog.DebugWrite("Sector Manager ready.", MessageType.Success);
}
2020-12-03 08:28:05 +00:00
public override void BuildWorldObjects(OWScene scene)
2020-12-02 21:23:01 +00:00
{
DebugLog.DebugWrite("Building sectors...", MessageType.Info);
2021-03-11 20:02:23 +00:00
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
{
var timeLoopRing = GameObject.Find("TimeLoopRing_Body");
if (timeLoopRing != null)
{
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-06-18 22:38:32 +01:00
2020-12-23 23:14:32 +00:00
QSBWorldSync.Init<QSBSector, Sector>();
2020-12-11 13:14:58 +00:00
IsReady = QSBWorldSync.GetWorldObjects<QSBSector>().Any();
2020-12-02 21:23:01 +00:00
}
public override void UnbuildWorldObjects() =>
IsReady = false;
2022-01-16 05:38:06 -08:00
private static void UpdateReferenceSector(BaseSectoredSync transformSync)
2021-02-27 09:56:07 +00:00
{
var closestSector = transformSync.SectorDetector.GetClosestSector();
2022-01-16 05:38:06 -08:00
if (closestSector == null)
2021-01-26 14:07:17 +00:00
{
2021-02-27 09:56:07 +00:00
return;
2021-01-26 14:07:17 +00:00
}
2021-06-18 22:38:32 +01:00
2021-03-09 19:45:00 +00:00
transformSync.SetReferenceSector(closestSector);
2021-02-27 09:56:07 +00:00
}
2020-12-02 21:23:01 +00:00
}
2022-01-16 05:38:06 -08:00
}