quantum-space-buddies/QSB/QuantumSync/Events/SocketStateChangeEvent.cs

37 lines
1.4 KiB
C#
Raw Normal View History

2020-12-22 21:39:53 +00:00
using QSB.Events;
using QSB.WorldSync;
using System.Reflection;
using UnityEngine;
namespace QSB.QuantumSync.Events
{
2020-12-23 11:26:47 +00:00
public class SocketStateChangeEvent : QSBEvent<SocketStateChangeMessage>
2020-12-22 21:39:53 +00:00
{
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.GetWorldObject<QSBSocketedQuantumObject>(message.ObjectId).AttachedObject;
var socket = QSBWorldSync.GetWorldObject<QSBQuantumSocket>(message.SocketId).AttachedSocket;
obj.GetType().GetMethod("MoveToSocket", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(obj, new object[] { socket });
obj.transform.localRotation = message.LocalRotation;
}
}
}