quantum-space-buddies/QSB/Syncs/Sectored/BaseSectoredSync.cs

78 lines
1.6 KiB
C#
Raw Normal View History

2022-01-15 04:59:42 +00:00
using Mirror;
2021-08-14 14:03:01 +00:00
using QSB.SectorSync;
2021-08-22 15:53:46 +00:00
using QSB.SectorSync.WorldObjects;
2021-08-14 14:03:01 +00:00
using QSB.WorldSync;
2022-03-03 03:46:33 +00:00
namespace QSB.Syncs.Sectored;
public abstract class BaseSectoredSync : SyncBase
2021-08-14 14:03:01 +00:00
{
2022-03-03 03:46:33 +00:00
protected sealed override bool AllowNullReferenceTransform => true;
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
public QSBSector ReferenceSector { get; private set; }
public QSBSectorDetector SectorDetector { get; private set; }
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
private int _sectorId = -1;
2022-03-03 03:46:33 +00:00
public override void OnStartClient()
{
SectorDetector = gameObject.AddComponent<QSBSectorDetector>();
QSBSectorManager.Instance.SectoredSyncs.Add(this);
base.OnStartClient();
}
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
public override void OnStopClient()
{
base.OnStopClient();
QSBSectorManager.Instance.SectoredSyncs.Remove(this);
Destroy(SectorDetector);
}
2022-03-03 03:46:33 +00:00
protected override void Uninit()
{
base.Uninit();
2022-03-03 03:46:33 +00:00
SectorDetector.Uninit();
SetReferenceSector(null);
}
2022-01-16 12:51:37 +00:00
2022-03-03 03:46:33 +00:00
protected void GetFromSector() => _sectorId = ReferenceSector?.ObjectId ?? -1;
2022-03-03 03:46:33 +00:00
protected override void Serialize(NetworkWriter writer)
{
writer.Write(_sectorId);
base.Serialize(writer);
}
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
protected override void Deserialize(NetworkReader reader)
{
_sectorId = reader.ReadInt();
base.Deserialize(reader);
}
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
protected void ApplyToSector()
{
if (_sectorId == -1)
{
2022-03-03 03:46:33 +00:00
return;
2022-01-15 04:59:42 +00:00
}
2022-03-03 03:46:33 +00:00
SetReferenceSector(_sectorId.GetWorldObject<QSBSector>());
}
2022-03-03 03:46:33 +00: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;
}
2022-03-03 03:46:33 +00:00
ReferenceSector = sector;
SetReferenceTransform(sector?.Transform);
2022-01-15 04:59:42 +00:00
}
}