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

79 lines
1.7 KiB
C#
Raw Normal View History

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