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

83 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
{
2022-01-14 22:39:41 -08:00
public abstract class BaseSectoredSync : SyncBase
2022-01-14 20:59:42 -08:00
{
2022-01-16 06:11:44 -08:00
protected 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-14 20:59:42 -08:00
public SectorSync.SectorSync SectorSync { get; private set; }
2022-01-16 07:28:59 -08:00
private int _sectorId = -1;
2022-01-14 20:59:42 -08:00
public override void Start()
{
SectorSync = gameObject.AddComponent<SectorSync.SectorSync>();
2022-01-16 05:38:06 -08:00
QSBSectorManager.Instance.TransformSyncs.Add(this);
2022-01-14 20:59:42 -08:00
base.Start();
}
protected override void OnDestroy()
{
base.OnDestroy();
2022-01-16 05:38:06 -08:00
QSBSectorManager.Instance.TransformSyncs.Remove(this);
Destroy(SectorSync);
2022-01-14 20:59:42 -08:00
}
protected override void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse)
{
base.OnSceneLoaded(oldScene, newScene, isInUniverse);
SetReferenceSector(null);
}
protected override void Serialize(NetworkWriter writer, bool initialState)
2022-01-14 20:59:42 -08:00
{
base.Serialize(writer, initialState);
writer.Write(_sectorId);
}
protected override void Deserialize(NetworkReader reader, bool initialState)
{
base.Deserialize(reader, initialState);
_sectorId = reader.ReadInt();
}
2022-01-16 04:51:37 -08:00
2022-01-16 07:42:46 -08:00
protected void GetFromSector()
{
if (ReferenceSector != null)
2022-01-14 20:59:42 -08:00
{
_sectorId = ReferenceSector.ObjectId;
}
else
{
_sectorId = -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
{
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
}