quantum-space-buddies/QSB/QuantumSync/Events/SocketStateChangeEvent.cs
2021-02-24 10:45:25 +00:00

35 lines
1.2 KiB
C#

using QSB.Events;
using QSB.QuantumSync.WorldObjects;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.QuantumSync.Events
{
public class SocketStateChangeEvent : QSBEvent<SocketStateChangeMessage>
{
public override QSB.Events.EventType Type => QSB.Events.EventType.SocketStateChange;
public override void SetupListener() => GlobalMessenger<int, int, Quaternion>.AddListener(EventNames.QSBSocketStateChange, Handler);
public override void CloseListener() => GlobalMessenger<int, int, Quaternion>.RemoveListener(EventNames.QSBSocketStateChange, Handler);
private void Handler(int objid, int socketid, Quaternion localRotation) => SendEvent(CreateMessage(objid, socketid, localRotation));
private SocketStateChangeMessage CreateMessage(int objid, int socketid, Quaternion localRotation) => new SocketStateChangeMessage
{
AboutId = LocalPlayerId,
ObjectId = objid,
SocketId = socketid,
LocalRotation = localRotation
};
public override void OnReceiveRemote(bool server, SocketStateChangeMessage message)
{
if (!QSBCore.HasWokenUp)
{
return;
}
var obj = QSBWorldSync.GetWorldFromId<QSBSocketedQuantumObject>(message.ObjectId);
obj.MoveToSocket(message);
}
}
}