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

90 lines
2.6 KiB
C#
Raw Normal View History

2020-11-04 09:34:01 +00:00
using OWML.Common;
2021-03-09 19:45:00 +00:00
using QSB.Events;
using QSB.Player;
using QSB.SectorSync.WorldObjects;
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-09 19:45:00 +00:00
public class QSBSectorManager : MonoBehaviour, 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-03-09 19:45:00 +00:00
QSBPlayerManager.GetSyncObjects<TransformSync.TransformSync>()
.Where(x => x.HasAuthority).ToList().ForEach(CheckTransformSyncSector);
}
2020-12-14 20:41:56 +00:00
public void Awake()
2020-12-02 21:23:01 +00:00
{
2021-03-11 20:02:23 +00:00
if (Instance != null)
{
DebugLog.ToConsole("Error - Cannot have multiple QSBSectorManagers!", MessageType.Error);
Destroy(this);
return;
}
2020-12-02 21:23:01 +00:00
Instance = this;
QSBSceneManager.OnUniverseSceneLoaded += (OWScene scene) => RebuildSectors();
DebugLog.DebugWrite("Sector Manager ready.", MessageType.Success);
}
2020-12-03 08:28:05 +00:00
2021-03-09 21:56:52 +00:00
public void OnDestroy()
2021-03-09 19:45:00 +00:00
=> QSBSceneManager.OnUniverseSceneLoaded -= (OWScene scene) => RebuildSectors();
2021-03-11 20:02:23 +00:00
2020-12-02 21:23:01 +00:00
public void RebuildSectors()
{
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
}
2021-03-09 19:45:00 +00:00
private void CheckTransformSyncSector(TransformSync.TransformSync transformSync)
2021-02-27 09:56:07 +00:00
{
2021-03-09 19:45:00 +00:00
var syncedTransform = transformSync.SyncedTransform;
if (syncedTransform == null || syncedTransform.position == Vector3.zero)
2021-02-27 09:56:07 +00:00
{
return;
}
2021-03-09 19:45:00 +00:00
var closestSector = transformSync.SectorSync.GetClosestSector(syncedTransform);
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);
SendSector(transformSync.NetId.Value, closestSector);
2021-02-27 09:56:07 +00:00
}
2021-03-09 19:45:00 +00:00
private void SendSector(uint id, QSBSector sector) =>
QSBEventManager.FireEvent(EventNames.QSBSectorChange, id, sector);
2020-12-02 21:23:01 +00:00
}
2020-12-03 08:28:05 +00:00
}