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

112 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();
2021-12-18 12:47:37 -08:00
public readonly List<IBaseSectoredSync> SectoredSyncs = new();
2021-03-09 19:45:00 +00:00
2021-12-18 12:25:16 -08:00
#region repeating timer
private const float TimeInterval = 0.4f;
private float _checkTimer = TimeInterval;
private void Update()
{
_checkTimer += Time.unscaledDeltaTime;
if (_checkTimer < TimeInterval)
{
return;
}
Invoke();
_checkTimer = 0;
}
#endregion
2021-05-18 14:33:17 +01:00
2021-03-09 19:45:00 +00:00
public void Invoke()
2020-12-02 21:23:01 +00:00
{
if (!Instance.IsReady || !AllObjectsReady)
{
return;
}
2021-08-14 15:03:01 +01:00
foreach (var sync in SectoredSyncs)
2021-04-21 11:02:17 +01:00
{
2021-12-13 12:42:27 -08:00
if (sync.ReturnObject() == null)
2021-04-21 11:02:17 +01:00
{
continue;
}
2021-06-18 22:38:32 +01:00
if (sync.HasAuthority
2021-12-13 12:42:27 -08:00
&& sync.ReturnObject().gameObject.activeInHierarchy
2021-10-16 16:57:01 +01:00
&& sync.IsReady
&& sync.SectorSync.IsReady)
{
2021-08-14 15:03:01 +01:00
CheckTransformSyncSector(sync);
}
}
2021-03-09 19:45:00 +00:00
}
2021-03-23 13:18:29 +00:00
public override void Awake()
2020-12-02 21:23:01 +00:00
{
2021-03-23 13:18:29 +00:00
base.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
2021-03-23 13:18:29 +00:00
protected override void RebuildWorldObjects(OWScene scene)
2020-12-02 21:23:01 +00:00
{
2021-08-12 14:18:35 +01:00
DebugLog.DebugWrite("Rebuilding 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
}
2021-12-13 12:42:27 -08:00
private void CheckTransformSyncSector(IBaseSectoredSync transformSync)
2021-02-27 09:56:07 +00:00
{
2021-12-18 12:08:26 -08:00
var closestSector = transformSync.SectorSync.GetClosestSector();
2021-03-09 19:45:00 +00:00
if (closestSector == default(QSBSector))
2021-02-27 09:56:07 +00:00
{
return;
}
2021-06-18 22:38:32 +01:00
2021-03-09 19:45:00 +00:00
if (closestSector == transformSync.ReferenceSector)
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
}
2020-12-03 08:28:05 +00:00
}