quantum-space-buddies/QSB/Player/Events/PlayerSectorEvent.cs

45 lines
1.4 KiB
C#
Raw Normal View History

2020-08-22 18:08:25 +01:00
using OWML.Common;
2020-12-14 16:24:52 +00:00
using QSB.Events;
2020-11-03 22:29:23 +00:00
using QSB.SectorSync;
2020-08-09 13:54:05 +01:00
using QSB.Utility;
using QSB.WorldSync;
2020-11-03 22:18:22 +00:00
using QSB.WorldSync.Events;
2020-08-09 12:38:35 +01:00
2020-11-03 22:29:23 +00:00
namespace QSB.Player.Events
2020-08-09 12:38:35 +01:00
{
2020-12-02 21:23:01 +00:00
public class PlayerSectorEvent : QSBEvent<WorldObjectMessage>
{
public override EventType Type => EventType.PlayerSectorChange;
2020-12-11 13:14:58 +00:00
public override void SetupListener() => GlobalMessenger<uint, QSBSector>.AddListener(EventNames.QSBSectorChange, Handler);
public override void CloseListener() => GlobalMessenger<uint, QSBSector>.RemoveListener(EventNames.QSBSectorChange, Handler);
2020-12-02 21:23:01 +00:00
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
};
2020-12-11 22:42:21 +00:00
public override void OnReceiveRemote(bool server, WorldObjectMessage message)
2020-12-02 21:23:01 +00:00
{
if (!QSBSceneManager.IsInUniverse)
{
return;
}
2020-12-11 13:14:58 +00:00
var sector = QSBWorldSync.GetWorldObject<QSBSector>(message.ObjectId);
2020-12-02 21:23:01 +00:00
if (sector == null)
{
2020-12-11 22:42:21 +00:00
DebugLog.ToConsole($"Sector with index id {message.ObjectId} not found!", MessageType.Warning);
2020-12-02 21:23:01 +00:00
return;
}
var transformSync = QSBPlayerManager.GetSyncObject<TransformSync.TransformSync>(message.AboutId);
2020-12-14 16:24:52 +00:00
QSBCore.Helper.Events.Unity.RunWhen(() => transformSync?.SyncedTransform != null,
2020-12-02 21:23:01 +00:00
() => transformSync?.SetReferenceSector(sector));
}
}
2020-12-03 08:28:05 +00:00
}