fix sector stuff

This commit is contained in:
Mister_Nebula 2021-03-09 19:45:00 +00:00
parent e250bbf2da
commit db593c2c57
11 changed files with 110 additions and 95 deletions

View File

@ -12,6 +12,10 @@ namespace QSB.Player
private void Update()
{
if (!QSBCore.IsInMultiplayer)
{
return;
}
var controller = Locator.GetPlayerController();
if (controller == null)
{

View File

@ -238,7 +238,7 @@
<Compile Include="Player\Events\PlayerJoinMessage.cs" />
<Compile Include="Messaging\ToggleMessage.cs" />
<Compile Include="SectorSync\WorldObjects\QSBSector.cs" />
<Compile Include="SectorSync\QSBSectorManager.cs" />
<Compile Include="SectorSync\SectorSync.cs" />
<Compile Include="TransformSync\TransformSync.cs" />
<Compile Include="Utility\CustomCallbacks.cs" />
<Compile Include="Utility\DebugBoxManager.cs" />
@ -287,7 +287,7 @@
<Compile Include="Utility\QuaternionHelper.cs" />
<Compile Include="TimeSync\PreserveTimeScale.cs" />
<Compile Include="TransformSync\ShipTransformSync.cs" />
<Compile Include="SectorSync\SectorSync.cs" />
<Compile Include="SectorSync\QSBSectorManager.cs" />
<Compile Include="TimeSync\Events\ServerTimeMessage.cs" />
<Compile Include="TimeSync\WakeUpSync.cs" />
<Compile Include="QSBNetworkManager.cs" />

View File

@ -13,6 +13,7 @@ using QSB.QuantumSync.WorldObjects;
using QSB.SectorSync;
using QSB.StatueSync;
using QSB.TimeSync;
using QSB.TransformSync;
using QSB.TranslationSync;
using QSB.Utility;
using QSB.WorldSync;
@ -151,9 +152,9 @@ namespace QSB
var offset3 = 10f;
GUI.Label(new Rect(420, offset3, 200f, 20f), $"Current sectors :");
offset3 += _debugLineSpacing;
foreach (var obj in QSBSectorManager.Instance.SectorList)
foreach (var obj in PlayerTransformSync.LocalInstance?.SectorSync.SectorList)
{
GUI.Label(new Rect(420, offset3, 200f, 20f), $"- {obj.AttachedObject.name}");
GUI.Label(new Rect(420, offset3, 200f, 20f), $"- {obj.AttachedObject.name} : {Vector3.Distance(obj.Position, Locator.GetPlayerTransform().position)}");
offset3 += _debugLineSpacing;
}

View File

@ -174,7 +174,7 @@ namespace QSB
QSBEventManager.Init();
gameObject.AddComponent<SectorSync.SectorSync>();
gameObject.AddComponent<QSBSectorManager>();
gameObject.AddComponent<RespawnOnDeath>();
if (QSBSceneManager.IsInUniverse)
@ -209,7 +209,7 @@ namespace QSB
{
DebugLog.DebugWrite("OnStopClient", MessageType.Info);
DebugLog.ToConsole("Disconnecting from server...", MessageType.Info);
Destroy(GetComponent<SectorSync.SectorSync>());
Destroy(GetComponent<SectorSync.QSBSectorManager>());
Destroy(GetComponent<RespawnOnDeath>());
QSBEventManager.Reset();
QSBPlayerManager.PlayerList.ForEach(player => player.HudMarker?.Remove());
@ -256,7 +256,7 @@ namespace QSB
public override void OnStopServer()
{
DebugLog.DebugWrite("OnStopServer", MessageType.Info);
Destroy(GetComponent<SectorSync.SectorSync>());
Destroy(GetComponent<SectorSync.QSBSectorManager>());
Destroy(GetComponent<RespawnOnDeath>());
QSBEventManager.Reset();
DebugLog.ToConsole("Server stopped!", MessageType.Info);

View File

@ -1,24 +1,27 @@
using OWML.Common;
using QSB.Events;
using QSB.Player;
using QSB.SectorSync.WorldObjects;
using QSB.Utility;
using QSB.WorldSync;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace QSB.SectorSync
{
public class QSBSectorManager : MonoBehaviour
public class QSBSectorManager : MonoBehaviour, IRepeating
{
public static QSBSectorManager Instance { get; private set; }
public List<QSBSector> SectorList = new List<QSBSector>();
public bool IsReady { get; private set; }
private readonly Sector.Name[] _sectorBlacklist =
private void OnEnable() => RepeatingManager.Repeatings.Add(this);
private void OnDisable() => RepeatingManager.Repeatings.Remove(this);
public void Invoke()
{
Sector.Name.Ship
};
QSBPlayerManager.GetSyncObjects<TransformSync.TransformSync>()
.Where(x => x.HasAuthority).ToList().ForEach(CheckTransformSyncSector);
}
public void Awake()
{
@ -27,72 +30,37 @@ namespace QSB.SectorSync
DebugLog.DebugWrite("Sector Manager ready.", MessageType.Success);
}
public void OnDestroy()
{
QSBSceneManager.OnUniverseSceneLoaded -= (OWScene scene) => RebuildSectors();
FindObjectOfType<PlayerSectorDetector>().OnEnterSector -= AddSector;
FindObjectOfType<PlayerSectorDetector>().OnExitSector -= RemoveSector;
}
public void OnDestroy()
=> QSBSceneManager.OnUniverseSceneLoaded -= (OWScene scene) => RebuildSectors();
public void RebuildSectors()
{
DebugLog.DebugWrite("Rebuilding sectors...", MessageType.Warning);
QSBWorldSync.Init<QSBSector, Sector>();
SectorList.Clear();
IsReady = QSBWorldSync.GetWorldObjects<QSBSector>().Any();
FindObjectOfType<PlayerSectorDetector>().OnEnterSector += AddSector;
FindObjectOfType<PlayerSectorDetector>().OnExitSector += RemoveSector;
}
private void AddSector(Sector sector)
private void CheckTransformSyncSector(TransformSync.TransformSync transformSync)
{
var worldObject = QSBWorldSync.GetWorldFromUnity<QSBSector, Sector>(sector);
if (worldObject == null)
var syncedTransform = transformSync.SyncedTransform;
if (syncedTransform == null || syncedTransform.position == Vector3.zero)
{
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
}
if (SectorList.Contains(worldObject))
{
DebugLog.ToConsole($"Warning - Trying to add {sector.name}, but is already in list", MessageType.Warning);
return;
}
SectorList.Add(worldObject);
}
private void RemoveSector(Sector sector)
{
var worldObject = QSBWorldSync.GetWorldFromUnity<QSBSector, Sector>(sector);
if (worldObject == null)
var closestSector = transformSync.SectorSync.GetClosestSector(syncedTransform);
if (closestSector == default(QSBSector))
{
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
return;
}
if (!SectorList.Contains(worldObject))
if (closestSector == transformSync.ReferenceSector)
{
DebugLog.ToConsole($"Warning - Trying to remove {sector.name}, but is not in list!", MessageType.Warning);
return;
}
SectorList.Remove(worldObject);
transformSync.SetReferenceSector(closestSector);
SendSector(transformSync.NetId.Value, closestSector);
}
public QSBSector GetClosestSector(Transform trans) // trans rights \o/
{
if (!IsReady)
{
DebugLog.ToConsole($"Warning - Tried to get closest sector to {trans.name} before SectorManager was ready.", MessageType.Warning);
return null;
}
var listToCheck = SectorList.Count == 0
? QSBWorldSync.GetWorldObjects<QSBSector>()
: SectorList;
return listToCheck
.Where(sector => sector.AttachedObject != null
&& !_sectorBlacklist.Contains(sector.Type)
&& sector.Transform.gameObject.activeInHierarchy)
.OrderBy(sector => Vector3.Distance(sector.Position, trans.position))
.FirstOrDefault();
}
private void SendSector(uint id, QSBSector sector) =>
QSBEventManager.FireEvent(EventNames.QSBSectorChange, id, sector);
}
}

View File

@ -1,48 +1,81 @@
using QSB.Events;
using QSB.Player;
using OWML.Common;
using QSB.SectorSync.WorldObjects;
using QSB.Utility;
using QSB.WorldSync;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace QSB.SectorSync
{
public class SectorSync : MonoBehaviour, IRepeating
public class SectorSync : MonoBehaviour
{
private void OnEnable() => RepeatingManager.Repeatings.Add(this);
private void OnDisable() => RepeatingManager.Repeatings.Remove(this);
public List<QSBSector> SectorList = new List<QSBSector>();
public void Invoke()
private SectorDetector _sectorDetector;
private readonly Sector.Name[] _sectorBlacklist = { Sector.Name.Ship };
public void SetSectorDetector(SectorDetector detector)
{
if (_sectorDetector != null)
{
_sectorDetector.OnEnterSector -= AddSector;
_sectorDetector.OnExitSector -= RemoveSector;
}
_sectorDetector = detector;
_sectorDetector.OnEnterSector += AddSector;
_sectorDetector.OnExitSector += RemoveSector;
}
private void AddSector(Sector sector)
{
var worldObject = QSBWorldSync.GetWorldFromUnity<QSBSector, Sector>(sector);
if (worldObject == null)
{
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
}
if (SectorList.Contains(worldObject))
{
DebugLog.ToConsole($"Warning - Trying to add {sector.name}, but is already in list", MessageType.Warning);
return;
}
SectorList.Add(worldObject);
}
private void RemoveSector(Sector sector)
{
var worldObject = QSBWorldSync.GetWorldFromUnity<QSBSector, Sector>(sector);
if (worldObject == null)
{
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
return;
}
if (!SectorList.Contains(worldObject))
{
DebugLog.ToConsole($"Warning - Trying to remove {sector.name}, but is not in list!", MessageType.Warning);
return;
}
SectorList.Remove(worldObject);
}
public QSBSector GetClosestSector(Transform trans) // trans rights \o/
{
if (!QSBSectorManager.Instance.IsReady)
{
return;
DebugLog.ToConsole($"Warning - Tried to get closest sector to {trans.name} before QSBSectorManager was ready.", MessageType.Warning);
return null;
}
QSBPlayerManager.GetSyncObjects<TransformSync.TransformSync>()
.Where(x => x.HasAuthority).ToList().ForEach(CheckTransformSyncSector);
}
private void CheckTransformSyncSector(TransformSync.TransformSync transformSync)
{
var syncedTransform = transformSync.SyncedTransform;
if (syncedTransform == null || syncedTransform.position == Vector3.zero)
{
return;
}
var closestSector = QSBSectorManager.Instance.GetClosestSector(syncedTransform);
if (closestSector == default(QSBSector))
{
return;
}
if (closestSector == transformSync.ReferenceSector)
{
return;
}
transformSync.SetReferenceSector(closestSector);
SendSector(transformSync.NetId.Value, closestSector);
var listToCheck = SectorList.Count == 0
? QSBWorldSync.GetWorldObjects<QSBSector>()
: SectorList;
// TODO : maybe improve this by more carefully picking sectors that are in the same place? maybe by last entered or closest to edge?
return listToCheck
.Where(sector => sector.AttachedObject != null
&& !_sectorBlacklist.Contains(sector.Type)
&& sector.Transform.gameObject.activeInHierarchy)
.OrderBy(sector => Vector3.Distance(sector.Position, trans.position))
.FirstOrDefault();
}
private void SendSector(uint id, QSBSector sector) =>
QSBEventManager.FireEvent(EventNames.QSBSectorChange, id, sector);
}
}

View File

@ -10,6 +10,7 @@ namespace QSB.TransformSync
{
protected override Transform InitLocalTransform()
{
SectorSync.SetSectorDetector(Locator.GetPlayerSectorDetector());
var body = Locator.GetPlayerCamera().gameObject.transform;
Player.Camera = Locator.GetPlayerCamera();

View File

@ -15,6 +15,7 @@ namespace QSB.TransformSync
protected override Transform InitLocalTransform()
{
SectorSync.SetSectorDetector(Locator.GetProbe().GetSectorDetector());
var body = GetProbe();
SetSocket(Player.CameraBody.transform);

View File

@ -29,6 +29,7 @@ namespace QSB.TransformSync
protected override Transform InitLocalTransform()
{
SectorSync.SetSectorDetector(Locator.GetPlayerSectorDetector());
var body = GetPlayerModel();
GetComponent<AnimationSync>().InitLocal(body);

View File

@ -7,8 +7,11 @@ namespace QSB.TransformSync
{
private Transform GetShipModel() => Locator.GetShipTransform();
protected override Transform InitLocalTransform() =>
GetShipModel().Find("Module_Cockpit/Geo_Cockpit/Cockpit_Geometry/Cockpit_Exterior");
protected override Transform InitLocalTransform()
{
SectorSync.SetSectorDetector(Locator.GetShipDetector().GetComponent<SectorDetector>());
return GetShipModel().Find("Module_Cockpit/Geo_Cockpit/Cockpit_Geometry/Cockpit_Exterior");
}
protected override Transform InitRemoteTransform()
{

View File

@ -17,6 +17,7 @@ namespace QSB.TransformSync
public Transform SyncedTransform { get; private set; }
public QSBSector ReferenceSector { get; set; }
public SectorSync.SectorSync SectorSync { get; private set; }
private const float SmoothTime = 0.1f;
private const float DistanceLeeway = 5f;
@ -33,6 +34,8 @@ namespace QSB.TransformSync
.Where(x => x.NetId.Value <= NetId.Value).OrderBy(x => x.NetId.Value).Last();
NetIdentity.SetRootIdentity(lowestBound.NetIdentity);
SectorSync = gameObject.AddComponent<SectorSync.SectorSync>();
DontDestroyOnLoad(gameObject);
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
}
@ -53,7 +56,7 @@ namespace QSB.TransformSync
protected void Init()
{
SyncedTransform = HasAuthority ? InitLocalTransform() : InitRemoteTransform();
SetReferenceSector(QSBSectorManager.Instance.GetClosestSector(SyncedTransform));
SetReferenceSector(SectorSync.GetClosestSector(SyncedTransform));
_isInitialized = true;
_isVisible = true;
}