mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-06 01:00:16 +00:00
fix sector stuff
This commit is contained in:
parent
e250bbf2da
commit
db593c2c57
@ -12,6 +12,10 @@ namespace QSB.Player
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!QSBCore.IsInMultiplayer)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var controller = Locator.GetPlayerController();
|
||||
if (controller == null)
|
||||
{
|
||||
|
@ -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" />
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
@ -15,6 +15,7 @@ namespace QSB.TransformSync
|
||||
|
||||
protected override Transform InitLocalTransform()
|
||||
{
|
||||
SectorSync.SetSectorDetector(Locator.GetProbe().GetSectorDetector());
|
||||
var body = GetProbe();
|
||||
|
||||
SetSocket(Player.CameraBody.transform);
|
||||
|
@ -29,6 +29,7 @@ namespace QSB.TransformSync
|
||||
|
||||
protected override Transform InitLocalTransform()
|
||||
{
|
||||
SectorSync.SetSectorDetector(Locator.GetPlayerSectorDetector());
|
||||
var body = GetPlayerModel();
|
||||
|
||||
GetComponent<AnimationSync>().InitLocal(body);
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user