2022-01-14 20:59:42 -08:00
|
|
|
|
using Mirror;
|
2021-08-14 15:03:01 +01:00
|
|
|
|
using QSB.SectorSync;
|
2021-08-22 16:53:46 +01:00
|
|
|
|
using QSB.SectorSync.WorldObjects;
|
2021-08-14 15:03:01 +01:00
|
|
|
|
using QSB.WorldSync;
|
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
namespace QSB.Syncs.Sectored
|
2021-08-14 15:03:01 +01:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public abstract class BaseSectoredSync : SyncBase
|
2022-02-24 22:04:54 -08:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
protected sealed override bool AllowNullReferenceTransform => true;
|
2022-01-14 20:59:42 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public QSBSector ReferenceSector { get; private set; }
|
|
|
|
|
public QSBSectorDetector SectorDetector { get; private set; }
|
2022-01-14 20:59:42 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
private int _sectorId = -1;
|
2022-01-21 16:37:30 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public override void OnStartClient()
|
|
|
|
|
{
|
|
|
|
|
SectorDetector = gameObject.AddComponent<QSBSectorDetector>();
|
|
|
|
|
QSBSectorManager.Instance.SectoredSyncs.Add(this);
|
|
|
|
|
base.OnStartClient();
|
|
|
|
|
}
|
2022-01-14 20:59:42 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
public override void OnStopClient()
|
|
|
|
|
{
|
|
|
|
|
base.OnStopClient();
|
|
|
|
|
QSBSectorManager.Instance.SectoredSyncs.Remove(this);
|
|
|
|
|
Destroy(SectorDetector);
|
|
|
|
|
}
|
2022-02-25 12:51:55 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
protected override void Uninit()
|
|
|
|
|
{
|
|
|
|
|
base.Uninit();
|
2022-01-16 07:23:35 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
SectorDetector.Uninit();
|
|
|
|
|
SetReferenceSector(null);
|
|
|
|
|
}
|
2022-01-16 04:51:37 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
protected void GetFromSector() => _sectorId = ReferenceSector?.ObjectId ?? -1;
|
|
|
|
|
|
|
|
|
|
protected override void Serialize(NetworkWriter writer)
|
2022-01-14 20:59:42 -08:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
writer.Write(_sectorId);
|
|
|
|
|
base.Serialize(writer);
|
2022-01-14 20:59:42 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
protected override void Deserialize(NetworkReader reader)
|
|
|
|
|
{
|
|
|
|
|
_sectorId = reader.ReadInt();
|
|
|
|
|
base.Deserialize(reader);
|
|
|
|
|
}
|
2022-01-14 20:59:42 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
protected void ApplyToSector()
|
2022-02-24 22:04:54 -08:00
|
|
|
|
{
|
2022-02-27 04:40:44 -08:00
|
|
|
|
if (_sectorId == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetReferenceSector(_sectorId.GetWorldObject<QSBSector>());
|
2022-01-14 20:59:42 -08:00
|
|
|
|
}
|
2022-02-24 22:04:54 -08:00
|
|
|
|
|
2022-02-27 04:40:44 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// use this instead of SetReferenceTransform
|
|
|
|
|
/// <para/>
|
|
|
|
|
/// called by QSBSectorManager (authority) and ApplyToSector (non authority)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SetReferenceSector(QSBSector sector)
|
|
|
|
|
{
|
|
|
|
|
if (ReferenceSector == sector)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReferenceSector = sector;
|
|
|
|
|
SetReferenceTransform(sector?.Transform);
|
|
|
|
|
}
|
2022-01-14 20:59:42 -08:00
|
|
|
|
}
|
2022-02-24 22:04:54 -08:00
|
|
|
|
}
|