quantum-space-buddies/QSB/QuantumSync/Events/SocketStateChangeEvent.cs
Mister_Nebula 53974485c9
Spiral sync (#242)
* add stuff

* extract patches

* extract worldobjects (#241)

* add spiral sync

* cleanup

* cleanup

* fix

* rename

* add computers

* remove qnet flagshelper

* Update README.md

* cleanup
2020-12-31 12:10:55 +00:00

38 lines
1.5 KiB
C#

using QSB.Events;
using QSB.QuantumSync.WorldObjects;
using QSB.WorldSync;
using System.Reflection;
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.GetWorldObject<QSBSocketedQuantumObject>(message.ObjectId).AttachedObject;
var socket = QSBWorldSync.GetWorldObject<QSBQuantumSocket>(message.SocketId).AttachedObject;
obj.GetType().GetMethod("MoveToSocket", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(obj, new object[] { socket });
obj.transform.localRotation = message.LocalRotation;
}
}
}