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

82 lines
1.9 KiB
C#
Raw Normal View History

2022-01-14 20:59:42 -08:00
using Mirror;
using OWML.Common;
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.Utility;
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 AllowDisabledAttachedObject => false;
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; }
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);
2022-01-14 20:59:42 -08:00
if (SectorSync != null)
{
Destroy(SectorSync);
}
}
protected override void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse)
{
base.OnSceneLoaded(oldScene, newScene, isInUniverse);
SetReferenceSector(null);
}
protected override void Serialize(NetworkWriter writer)
2022-01-14 20:59:42 -08:00
{
2022-01-16 04:51:37 -08:00
base.Serialize(writer);
2022-01-16 05:38:06 -08:00
if (ReferenceSector == null)
2022-01-14 20:59:42 -08:00
{
2022-01-16 05:38:06 -08:00
DebugLog.ToConsole($"Warning - ReferenceSector of {LogName} is null.", MessageType.Warning);
2022-01-14 20:59:42 -08:00
writer.Write(-1);
2022-01-16 05:38:06 -08:00
return;
2022-01-14 20:59:42 -08:00
}
2022-01-16 05:38:06 -08:00
writer.Write(ReferenceSector.ObjectId);
2022-01-14 20:59:42 -08:00
}
protected override void Deserialize(NetworkReader reader)
2022-01-14 20:59:42 -08:00
{
2022-01-16 04:51:37 -08:00
base.Deserialize(reader);
2022-01-16 05:38:06 -08:00
var sectorId = reader.ReadInt();
if (sectorId == -1)
2022-01-14 20:59:42 -08:00
{
2022-01-16 05:38:06 -08:00
DebugLog.ToConsole($"Error - {LogName} got sector of ID -1. (From deserializing transform.)", MessageType.Error);
2022-01-14 20:59:42 -08:00
return;
}
2022-01-16 05:38:06 -08:00
var sector = sectorId.GetWorldObject<QSBSector>();
SetReferenceSector(sector);
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
}