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;
|
|
|
|
|
|
|
|
|
|
namespace QSB.Syncs.Sectored
|
|
|
|
|
{
|
2022-01-14 22:39:41 -08:00
|
|
|
|
public abstract class BaseSectoredSync : SyncBase
|
2022-01-14 20:59:42 -08:00
|
|
|
|
{
|
2022-01-21 18:58:14 -08:00
|
|
|
|
protected sealed override bool AllowNullReferenceTransform => true;
|
2022-01-14 20:59:42 -08:00
|
|
|
|
|
2022-01-16 05:38:06 -08:00
|
|
|
|
public QSBSector ReferenceSector { get; private set; }
|
2022-01-21 16:37:30 -08:00
|
|
|
|
public QSBSectorDetector SectorDetector { get; private set; }
|
2022-01-14 20:59:42 -08:00
|
|
|
|
|
2022-01-16 07:28:59 -08:00
|
|
|
|
private int _sectorId = -1;
|
2022-01-16 07:23:35 -08:00
|
|
|
|
|
2022-01-18 12:19:58 -08:00
|
|
|
|
public override void OnStartClient()
|
2022-01-14 20:59:42 -08:00
|
|
|
|
{
|
2022-01-21 16:37:30 -08:00
|
|
|
|
SectorDetector = gameObject.AddComponent<QSBSectorDetector>();
|
2022-01-21 16:52:30 -08:00
|
|
|
|
QSBSectorManager.Instance.SectoredSyncs.Add(this);
|
2022-01-18 12:19:58 -08:00
|
|
|
|
base.OnStartClient();
|
2022-01-14 20:59:42 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 15:06:45 -08:00
|
|
|
|
public override void OnStopClient()
|
2022-01-14 20:59:42 -08:00
|
|
|
|
{
|
2022-01-18 15:06:45 -08:00
|
|
|
|
base.OnStopClient();
|
2022-01-21 16:52:30 -08:00
|
|
|
|
QSBSectorManager.Instance.SectoredSyncs.Remove(this);
|
2022-01-21 16:37:30 -08:00
|
|
|
|
Destroy(SectorDetector);
|
2022-01-14 20:59:42 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 16:26:21 -08:00
|
|
|
|
protected override void Uninit()
|
2022-01-14 20:59:42 -08:00
|
|
|
|
{
|
2022-01-21 16:37:30 -08:00
|
|
|
|
base.Uninit();
|
|
|
|
|
|
|
|
|
|
SectorDetector.Uninit();
|
2022-01-14 20:59:42 -08:00
|
|
|
|
SetReferenceSector(null);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 03:15:52 -08:00
|
|
|
|
protected override void Serialize(NetworkWriter writer)
|
2022-01-14 20:59:42 -08:00
|
|
|
|
{
|
2022-01-27 03:15:52 -08:00
|
|
|
|
base.Serialize(writer);
|
2022-01-16 07:23:35 -08:00
|
|
|
|
writer.Write(_sectorId);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 03:15:52 -08:00
|
|
|
|
protected override void Deserialize(NetworkReader reader)
|
2022-01-16 07:23:35 -08:00
|
|
|
|
{
|
2022-01-27 03:15:52 -08:00
|
|
|
|
base.Deserialize(reader);
|
2022-01-16 07:23:35 -08:00
|
|
|
|
_sectorId = reader.ReadInt();
|
|
|
|
|
}
|
2022-01-16 04:51:37 -08:00
|
|
|
|
|
2022-01-31 14:23:50 +00:00
|
|
|
|
protected void GetFromSector() => _sectorId = ReferenceSector?.ObjectId ?? -1;
|
2022-01-14 20:59:42 -08:00
|
|
|
|
|
2022-01-16 07:42:46 -08:00
|
|
|
|
protected void ApplyToSector()
|
2022-01-14 20:59:42 -08:00
|
|
|
|
{
|
2022-01-16 07:23:35 -08:00
|
|
|
|
if (_sectorId == -1)
|
2022-01-14 20:59:42 -08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-16 07:28:59 -08:00
|
|
|
|
SetReferenceSector(_sectorId.GetWorldObject<QSBSector>());
|
2022-01-14 20:59:42 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-16 05:38:06 -08:00
|
|
|
|
public void SetReferenceSector(QSBSector sector)
|
2022-01-14 20:59:42 -08:00
|
|
|
|
{
|
2022-01-16 05:38:06 -08:00
|
|
|
|
if (ReferenceSector == sector)
|
2022-01-14 20:59:42 -08:00
|
|
|
|
{
|
2022-01-16 05:38:06 -08:00
|
|
|
|
return;
|
2022-01-14 20:59:42 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReferenceSector = sector;
|
|
|
|
|
SetReferenceTransform(sector?.Transform);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-14 15:03:01 +01:00
|
|
|
|
}
|