quantum-space-buddies/QSB/Player/Events/PlayerSectorEvent.cs
Mister_Nebula c6c76cb395 C L E A N
2020-12-02 21:23:01 +00:00

54 lines
1.4 KiB
C#

using OWML.Common;
using QSB.EventsCore;
using QSB.SectorSync;
using QSB.Utility;
using QSB.WorldSync;
using QSB.WorldSync.Events;
namespace QSB.Player.Events
{
public class PlayerSectorEvent : QSBEvent<WorldObjectMessage>
{
public override EventType Type => EventType.PlayerSectorChange;
public override void SetupListener()
{
GlobalMessenger<uint, QSBSector>.AddListener(EventNames.QSBSectorChange, Handler);
}
public override void CloseListener()
{
GlobalMessenger<uint, QSBSector>.RemoveListener(EventNames.QSBSectorChange, Handler);
}
private void Handler(uint netId, QSBSector sector) => SendEvent(CreateMessage(netId, sector));
private WorldObjectMessage CreateMessage(uint netId, QSBSector sector) => new WorldObjectMessage
{
AboutId = netId,
ObjectId = sector.ObjectId
};
public override void OnReceiveRemote(WorldObjectMessage message)
{
if (!QSBSceneManager.IsInUniverse)
{
return;
}
var sector = WorldRegistry.GetObject<QSBSector>(message.ObjectId);
if (sector == null)
{
DebugLog.ToConsole($"Sector with order id {message.ObjectId} not found!", MessageType.Warning);
return;
}
var transformSync = QSBPlayerManager.GetSyncObject<TransformSync.TransformSync>(message.AboutId);
QSB.Helper.Events.Unity.RunWhen(() => transformSync?.SyncedTransform != null,
() => transformSync?.SetReferenceSector(sector));
}
}
}