more simplifying

This commit is contained in:
JohnCorby 2022-01-21 16:52:30 -08:00
parent cc4da03a9b
commit 0eeaba4eb0
4 changed files with 30 additions and 48 deletions

View File

@ -87,25 +87,11 @@ namespace QSB.SectorSync
SectorList.Remove(worldObject);
}
/// <summary>
/// called only by the sector manager
/// </summary>
public QSBSector GetClosestSector()
{
if (QSBSectorManager.Instance == null || !QSBSectorManager.Instance.IsReady)
{
return null;
}
if (!IsReady)
{
DebugLog.ToConsole($"Warning - Tried to use GetClosestSector() before this SectorSync is ready. Stacktrace:\r\n{Environment.StackTrace}", MessageType.Warning);
return null;
}
if (_sectorDetector == null)
{
IsReady = false;
return null;
}
var inASector = SectorList.Any(x => x.ShouldSyncTo(_targetType));
var listToCheck = inASector

View File

@ -14,10 +14,10 @@ namespace QSB.SectorSync
public override WorldObjectType WorldObjectType => WorldObjectType.Both;
public static QSBSectorManager Instance { get; private set; }
public bool IsReady { get; private set; }
private bool _isReady;
public readonly List<QSBSector> FakeSectors = new();
public readonly List<BaseSectoredSync> TransformSyncs = new();
public readonly List<BaseSectoredSync> SectoredSyncs = new();
private const float UpdateInterval = 0.4f;
private float _timer = UpdateInterval;
@ -36,21 +36,17 @@ namespace QSB.SectorSync
public void UpdateReferenceSectors()
{
if (!Instance.IsReady || !QSBWorldSync.AllObjectsReady)
if (!Instance._isReady || !QSBWorldSync.AllObjectsReady)
{
return;
}
foreach (var sync in TransformSyncs)
foreach (var sync in SectoredSyncs)
{
if (sync.AttachedTransform == null)
{
continue;
}
if (sync.hasAuthority
&& sync.AttachedTransform.gameObject.activeInHierarchy
&& sync.IsInitialized
&& sync.AttachedTransform
&& sync.AttachedTransform.gameObject.activeInHierarchy
&& sync.SectorDetector.IsReady)
{
UpdateReferenceSector(sync);
@ -58,6 +54,17 @@ namespace QSB.SectorSync
}
}
private static void UpdateReferenceSector(BaseSectoredSync sync)
{
var closestSector = sync.SectorDetector.GetClosestSector();
if (closestSector == null)
{
return;
}
sync.SetReferenceSector(closestSector);
}
public void Awake()
{
Instance = this;
@ -84,21 +91,10 @@ namespace QSB.SectorSync
}
QSBWorldSync.Init<QSBSector, Sector>();
IsReady = QSBWorldSync.GetWorldObjects<QSBSector>().Any();
_isReady = QSBWorldSync.GetWorldObjects<QSBSector>().Any();
}
public override void UnbuildWorldObjects() =>
IsReady = false;
private static void UpdateReferenceSector(BaseSectoredSync transformSync)
{
var closestSector = transformSync.SectorDetector.GetClosestSector();
if (closestSector == null)
{
return;
}
transformSync.SetReferenceSector(closestSector);
}
_isReady = false;
}
}

View File

@ -17,14 +17,14 @@ namespace QSB.Syncs.Sectored
public override void OnStartClient()
{
SectorDetector = gameObject.AddComponent<QSBSectorDetector>();
QSBSectorManager.Instance.TransformSyncs.Add(this);
QSBSectorManager.Instance.SectoredSyncs.Add(this);
base.OnStartClient();
}
public override void OnStopClient()
{
base.OnStopClient();
QSBSectorManager.Instance.TransformSyncs.Remove(this);
QSBSectorManager.Instance.SectoredSyncs.Remove(this);
Destroy(SectorDetector);
}

View File

@ -171,7 +171,7 @@ namespace QSB.Syncs
return;
}
if (AttachedTransform == null)
if (!AttachedTransform)
{
DebugLog.ToConsole($"Error - AttachedObject {LogName} is null!", MessageType.Error);
base.Update();
@ -184,7 +184,7 @@ namespace QSB.Syncs
return;
}
if (ReferenceTransform == null && !AllowNullReferenceTransform)
if (!ReferenceTransform && !AllowNullReferenceTransform)
{
DebugLog.ToConsole($"Warning - {LogName}'s ReferenceTransform is null. AttachedObject:{AttachedTransform.name}", MessageType.Warning);
base.Update();
@ -197,7 +197,7 @@ namespace QSB.Syncs
return;
}
if (ReferenceTransform != null && ReferenceTransform.position == Vector3.zero)
if (ReferenceTransform && ReferenceTransform.position == Vector3.zero)
{
DebugLog.ToConsole($"Warning - {LogName}'s ReferenceTransform is at (0,0,0). ReferenceTransform:{ReferenceTransform.name}, AttachedObject:{AttachedTransform.name}", MessageType.Warning);
}
@ -259,8 +259,8 @@ namespace QSB.Syncs
{
if (!QSBCore.ShowLinesInDebug
|| !IsInitialized
|| AttachedTransform == null
|| ReferenceTransform == null)
|| !AttachedTransform
|| !ReferenceTransform)
{
return;
}
@ -287,7 +287,7 @@ namespace QSB.Syncs
return;
}
if (AttachedTransform != null)
if (AttachedTransform)
{
DebugGUI.DrawLabel(AttachedTransform.transform, LogName);
}