fake sectors: treat like normal sectors in sector detector

This commit is contained in:
JohnCorby 2022-03-20 00:13:40 -07:00
parent 542bb8f1ce
commit e0a7bc3f23
4 changed files with 17 additions and 64 deletions

View File

@ -30,12 +30,12 @@ public class FakeSector : Sector
go2.AddComponent<OWTriggerVolume>();
go2.AddComponent<SphereShape>().radius = fakeSector.Radius;
go2.AddComponent<Renderer>().FakeSector = fakeSector;
go2.AddComponent<DebugRenderer>().FakeSector = fakeSector;
go2.SetActive(true);
}
private class Renderer : MonoBehaviour
private class DebugRenderer : MonoBehaviour
{
[NonSerialized]
public FakeSector FakeSector;

View File

@ -93,10 +93,11 @@ public class QSBSectorDetector : MonoBehaviour
{
var type = _sectorDetector._occupantType;
var validSectors = SectorList.Where(x => x.ShouldSyncTo(type)).ToList();
var inASector = validSectors.Count > 0;
var validSectors = SectorList
.Where(x => x.ShouldSyncTo(type))
.ToList();
if (!inASector)
if (validSectors.Count == 0)
{
validSectors = QSBWorldSync.GetWorldObjects<QSBSector>()
.Where(x => x.ShouldSyncTo(type))
@ -108,27 +109,7 @@ public class QSBSectorDetector : MonoBehaviour
return null;
}
var closest = validSectors
.MinBy(sector => sector.CalculateScore(_sectorDetector._attachedRigidbody));
if (inASector)
{
var pos = _sectorDetector._attachedRigidbody.GetPosition();
bool IsSameDistanceAsClosest(QSBSector fakeSector)
=> OWMath.ApproxEquals(
Vector3.Distance(fakeSector.Position, pos),
Vector3.Distance(closest.Position, pos),
0.01f);
bool IsParentValid(QSBSector fakeSector)
=> validSectors.Any(x => x.AttachedObject == fakeSector.FakeSector._parentSector);
var fakeToSyncTo = QSBSectorManager.Instance.FakeSectors
.FirstOrDefault(x => IsSameDistanceAsClosest(x) && IsParentValid(x));
return fakeToSyncTo ?? closest;
}
return closest;
return validSectors
.MinBy(sector => sector.GetScore(_sectorDetector._attachedRigidbody));
}
}

View File

@ -17,7 +17,6 @@ public class QSBSectorManager : WorldObjectManager
public static QSBSectorManager Instance { get; private set; }
private bool _isReady;
public readonly List<QSBSector> FakeSectors = new();
public readonly List<BaseSectoredSync> SectoredSyncs = new();

View File

@ -1,17 +1,15 @@
using Cysharp.Threading.Tasks;
using OWML.Common;
using OWML.Common;
using QSB.Utility;
using QSB.WorldSync;
using System;
using System.Linq;
using System.Threading;
using UnityEngine;
namespace QSB.SectorSync.WorldObjects;
public class QSBSector : WorldObject<Sector>
{
public Sector.Name Type => AttachedObject.GetName();
private Sector.Name Type => AttachedObject.GetName();
public Transform Transform
{
get
@ -25,36 +23,11 @@ public class QSBSector : WorldObject<Sector>
return AttachedObject.transform;
}
}
public Vector3 Position => Transform.position;
public bool IsFakeSector => AttachedObject is FakeSector;
public FakeSector FakeSector => (FakeSector)AttachedObject;
public override async UniTask Init(CancellationToken ct)
{
if (IsFakeSector)
{
QSBSectorManager.Instance.FakeSectors.Add(this);
}
}
public override void OnRemoval()
{
if (IsFakeSector)
{
QSBSectorManager.Instance.FakeSectors.Remove(this);
}
}
public override void SendInitialState(uint to) { }
public bool ShouldSyncTo(DynamicOccupant occupantType)
{
if (IsFakeSector)
{
return false;
}
if (occupantType == DynamicOccupant.Ship && Type == Sector.Name.Ship)
{
return false;
@ -106,20 +79,20 @@ public class QSBSector : WorldObject<Sector>
return true;
}
public float CalculateScore(OWRigidbody rigidbody)
public float GetScore(OWRigidbody rigidbody)
{
var sqrDistance = (Position - rigidbody.GetPosition()).sqrMagnitude;
var sqrDistance = (Transform.position - rigidbody.GetPosition()).sqrMagnitude;
var radius = GetRadius();
var velocity = GetRelativeVelocity(rigidbody);
var sqrVelocity = GetSqrVelocity(rigidbody);
return sqrDistance + radius * radius + velocity;
return sqrDistance + radius * radius + sqrVelocity;
}
private float GetRadius()
{
if (IsFakeSector)
if (AttachedObject is FakeSector fakeSector)
{
return FakeSector.Radius;
return fakeSector.Radius;
}
// TODO : make this work for other stuff, not just shaped triggervolumes
@ -132,7 +105,7 @@ public class QSBSector : WorldObject<Sector>
return 0f;
}
private float GetRelativeVelocity(OWRigidbody rigidbody)
private float GetSqrVelocity(OWRigidbody rigidbody)
{
var sectorRigidbody = AttachedObject.GetOWRigidbody();
if (sectorRigidbody && rigidbody)