quantum-space-buddies/QSB/TransformSync/PlayerSectorEvent.cs

52 lines
1.7 KiB
C#
Raw Normal View History

2020-08-13 21:46:16 +02:00
using QSB.Events;
using QSB.Messaging;
2020-08-09 13:54:05 +01:00
using QSB.Utility;
using QSB.WorldSync;
2020-08-09 12:38:35 +01:00
2020-08-13 21:46:16 +02:00
namespace QSB.TransformSync
2020-08-09 12:38:35 +01:00
{
public class PlayerSectorEvent : QSBEvent<WorldObjectMessage>
2020-08-09 12:38:35 +01:00
{
public override MessageType Type => MessageType.PlayerSectorChange;
public override void SetupListener()
{
2020-08-17 12:00:31 +01:00
GlobalMessenger<uint, QSBSector>.AddListener(EventNames.QSBSectorChange, Handler);
}
public override void CloseListener()
{
GlobalMessenger<uint, QSBSector>.RemoveListener(EventNames.QSBSectorChange, Handler);
2020-08-09 12:38:35 +01:00
}
2020-08-15 21:55:25 +02:00
private void Handler(uint netId, QSBSector sector) => SendEvent(CreateMessage(netId, sector));
2020-08-15 21:55:25 +02:00
private WorldObjectMessage CreateMessage(uint netId, QSBSector sector) => new WorldObjectMessage
2020-08-10 18:17:54 +02:00
{
SenderId = netId,
ObjectId = sector.ObjectId
2020-08-10 18:17:54 +02:00
};
public override void OnReceiveRemote(WorldObjectMessage message)
2020-08-09 12:38:35 +01:00
{
2020-08-16 22:39:21 +02:00
if (!QSBSceneManager.IsInUniverse)
{
return;
}
var sector = WorldRegistry.GetObject<QSBSector>(message.ObjectId);
2020-08-09 13:54:05 +01:00
if (sector == null)
{
DebugLog.ToConsole($"Sector with order id {message.ObjectId} not found!");
2020-08-09 13:54:05 +01:00
return;
}
var transformSync = PlayerRegistry.GetTransformSync(message.SenderId);
//DebugLog.ToConsole($"{transformSync.GetType().Name} of ID {message.SenderId} set to {sector.Name}");
2020-08-14 22:59:18 +02:00
UnityHelper.Instance.RunWhen(() => transformSync.SyncedTransform != null,
2020-08-14 23:00:56 +02:00
() => transformSync.SetReferenceSector(sector));
2020-08-09 12:38:35 +01:00
}
2020-08-13 17:44:27 +01:00
2020-08-09 12:38:35 +01:00
}
}