QSBSectorDetector: improve

This commit is contained in:
JohnCorby 2022-02-15 21:54:12 -08:00
parent e119920db9
commit 2761d463e1
6 changed files with 28 additions and 35 deletions

View File

@ -22,7 +22,7 @@ namespace QSB.PlayerBodySetup.Local
{
DebugLog.DebugWrite($"CREATE PLAYER");
sectorDetector.Init(Locator.GetPlayerSectorDetector(), TargetType.Player);
sectorDetector.Init(Locator.GetPlayerSectorDetector());
// player body
var playerBody = Locator.GetPlayerTransform();

View File

@ -13,9 +13,8 @@ namespace QSB.SectorSync
public readonly List<QSBSector> SectorList = new();
private SectorDetector _sectorDetector;
private TargetType _targetType;
public void Init(SectorDetector detector, TargetType type)
public void Init(SectorDetector detector)
{
if (_sectorDetector)
{
@ -33,8 +32,6 @@ namespace QSB.SectorSync
_sectorDetector.OnExitSector += RemoveSector;
_sectorDetector._sectorList.ForEach(AddSector);
_targetType = type;
}
public void Uninit()
@ -106,35 +103,41 @@ namespace QSB.SectorSync
/// </summary>
public QSBSector GetClosestSector()
{
var inASector = SectorList.Any(x => x.ShouldSyncTo(_targetType));
var type = _sectorDetector._occupantType;
var listToCheck = inASector
? SectorList
: QSBWorldSync.GetWorldObjects<QSBSector>().Where(x => !x.IsFakeSector && x.Type != Sector.Name.Unnamed);
var validSectors = SectorList.Where(x => x.ShouldSyncTo(type)).ToList();
var inASector = validSectors.Count > 0;
var goodSectors = listToCheck.Where(sector => sector.ShouldSyncTo(_targetType)).ToList();
if (!inASector)
{
validSectors = QSBWorldSync.GetWorldObjects<QSBSector>()
.Where(x => !x.IsFakeSector && x.Type != Sector.Name.Unnamed && x.ShouldSyncTo(type))
.ToList();
}
if (goodSectors.Count == 0)
if (validSectors.Count == 0)
{
return null;
}
var closest = goodSectors
var closest = validSectors
.MinBy(sector => CalculateSectorScore(sector, _sectorDetector._attachedRigidbody));
if (inASector)
{
var pos = _sectorDetector._attachedRigidbody.GetPosition();
bool IsApproxCloseToClosestSector(QSBSector sectorToCheck)
=> OWMath.ApproxEquals(Vector3.Distance(sectorToCheck.Position, pos),
Vector3.Distance(closest.Position, pos),
0.01f);
bool IsSameDistanceAsClosest(QSBSector fakeSector)
=> OWMath.ApproxEquals(
Vector3.Distance(fakeSector.Position, pos),
Vector3.Distance(closest.Position, pos),
0.01f);
bool IsFakeSectorActive(QSBSector fakeSectorToCheck)
=> goodSectors.Any(x => fakeSectorToCheck.FakeSector.AttachedSector == x.AttachedObject);
bool IsAttachedValid(QSBSector fakeSector)
=> validSectors.Any(x => x.AttachedObject == fakeSector.FakeSector.AttachedSector);
var fakeToSyncTo = QSBSectorManager.Instance.FakeSectors.FirstOrDefault(x => IsApproxCloseToClosestSector(x) && IsFakeSectorActive(x));
var fakeToSyncTo = QSBSectorManager.Instance.FakeSectors
.FirstOrDefault(x => IsSameDistanceAsClosest(x) && IsAttachedValid(x));
return fakeToSyncTo ?? closest;
}
@ -174,4 +177,4 @@ namespace QSB.SectorSync
return 0;
}
}
}
}

View File

@ -1,9 +0,0 @@
namespace QSB.SectorSync
{
public enum TargetType
{
Player,
Probe,
Ship
}
}

View File

@ -26,6 +26,7 @@ namespace QSB.SectorSync.WorldObjects
}
}
public Vector3 Position => Transform.position;
public bool IsFakeSector => AttachedObject is FakeSector;
public FakeSector FakeSector => (FakeSector)AttachedObject;
@ -47,7 +48,7 @@ namespace QSB.SectorSync.WorldObjects
public override void SendInitialState(uint to) { }
public bool ShouldSyncTo(TargetType targetType)
public bool ShouldSyncTo(DynamicOccupant occupantType)
{
if (AttachedObject == null)
{
@ -60,7 +61,7 @@ namespace QSB.SectorSync.WorldObjects
return false;
}
if (targetType == TargetType.Ship && Type == Sector.Name.Ship)
if (occupantType == DynamicOccupant.Ship && Type == Sector.Name.Ship)
{
return false;
}

View File

@ -1,4 +1,3 @@
using QSB.SectorSync;
using QSB.Syncs.Sectored.Rigidbodies;
using QSB.Utility;
using UnityEngine;
@ -23,7 +22,7 @@ namespace QSB.ShipSync.TransformSync
protected override OWRigidbody InitAttachedRigidbody()
{
SectorDetector.Init(Locator.GetShipDetector().GetComponent<SectorDetector>(), TargetType.Ship);
SectorDetector.Init(Locator.GetShipDetector().GetComponent<SectorDetector>());
return Locator.GetShipBody();
}

View File

@ -1,5 +1,4 @@
using OWML.Common;
using QSB.SectorSync;
using QSB.Syncs.Sectored.Transforms;
using QSB.Tools.ProbeLauncherTool;
using QSB.Utility;
@ -26,7 +25,7 @@ namespace QSB.Tools.ProbeTool.TransformSync
protected override Transform InitLocalTransform()
{
SectorDetector.Init(Locator.GetProbe().GetSectorDetector(), TargetType.Probe);
SectorDetector.Init(Locator.GetProbe().GetSectorDetector());
var body = Locator.GetProbe().transform;
Player.ProbeBody = body.gameObject;