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

99 lines
2.6 KiB
C#
Raw Normal View History

2020-11-04 09:34:01 +00:00
using OWML.Common;
using QSB.SectorSync.WorldObjects;
using QSB.Syncs;
using QSB.Syncs.RigidbodySync;
2021-05-02 12:59:39 +00:00
using QSB.Syncs.TransformSync;
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-03-11 20:02:23 +00:00
public List<QSBSector> FakeSectors = new List<QSBSector>();
2021-03-09 19:45:00 +00:00
private void OnEnable() => RepeatingManager.Repeatings.Add(this);
private void OnDisable() => RepeatingManager.Repeatings.Remove(this);
public void Invoke()
2020-12-02 21:23:01 +00:00
{
2021-04-27 23:22:08 +00:00
foreach (var sync in SectoredTransformSync.SectoredNetworkTransformList)
2021-04-21 10:02:17 +00:00
{
if (sync.AttachedObject == null)
{
continue;
}
2021-04-23 15:55:57 +00:00
if (sync.HasAuthority
&& sync.AttachedObject.gameObject.activeInHierarchy
&& sync.IsReady)
{
CheckTransformSyncSector(sync);
}
}
foreach (var sync in SectoredRigidbodySync.SectoredNetworkTransformList)
{
if (sync.AttachedObject == null)
{
continue;
}
if (sync.HasAuthority
&& sync.AttachedObject.gameObject.activeInHierarchy
2021-04-23 15:55:57 +00:00
&& sync.IsReady)
2021-04-21 10:02:17 +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
{
DebugLog.DebugWrite("Rebuilding sectors...", MessageType.Warning);
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);
}
}
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
}
private void CheckTransformSyncSector<T>(ISectoredSync<T> transformSync)
where T : Component
2021-02-27 09:56:07 +00:00
{
2021-04-22 19:42:38 +00:00
var attachedObject = transformSync.AttachedObject;
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-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-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
}