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

94 lines
2.3 KiB
C#
Raw Normal View History

2020-11-04 09:34:01 +00:00
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;
using UnityEngine;
2020-11-03 22:29:23 +00:00
namespace QSB.SectorSync
{
2021-03-23 13:18:29 +00:00
public class QSBSectorManager : WorldObjectManager, IRepeating
2020-12-02 21:23:01 +00:00
{
public static QSBSectorManager Instance { get; private set; }
public bool IsReady { get; private set; }
2021-11-20 19:49:50 +00:00
public List<QSBSector> FakeSectors = new();
2021-03-09 19:45:00 +00:00
private void OnEnable() => RepeatingManager.Repeatings.Add(this);
private void OnDisable() => RepeatingManager.Repeatings.Remove(this);
2021-12-13 20:42:27 +00:00
public List<IBaseSectoredSync> SectoredSyncs = new();
2021-05-18 13:33:17 +00: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 14:03:01 +00:00
foreach (var sync in SectoredSyncs)
2021-04-21 10:02:17 +00:00
{
2021-12-13 20:42:27 +00:00
if (sync.ReturnObject() == null)
2021-04-21 10:02:17 +00:00
{
continue;
}
2021-06-18 21:38:32 +00:00
if (sync.HasAuthority
2021-12-13 20:42:27 +00:00
&& sync.ReturnObject().gameObject.activeInHierarchy
2021-10-16 15:57:01 +00:00
&& sync.IsReady
&& sync.SectorSync.IsReady)
{
2021-08-14 14:03:01 +00: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 13:18:35 +00: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 21:38:32 +00: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 20:42:27 +00:00
private void CheckTransformSyncSector(IBaseSectoredSync transformSync)
2021-02-27 09:56:07 +00:00
{
2021-12-13 20:42:27 +00:00
var attachedObject = transformSync.ReturnObject();
2021-04-22 19:42:38 +00:00
var closestSector = transformSync.SectorSync.GetClosestSector(attachedObject.transform);
2021-03-09 19:45:00 +00:00
if (closestSector == default(QSBSector))
2021-02-27 09:56:07 +00:00
{
return;
}
2021-06-18 21:38:32 +00: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 21:38:32 +00: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
}