2021-03-17 17:04:57 +00:00
using OWML.Common ;
using QSB.Events ;
2020-12-31 12:10:55 +00:00
using QSB.QuantumSync.WorldObjects ;
2021-03-17 17:04:57 +00:00
using QSB.Utility ;
2020-12-22 21:39:53 +00:00
using QSB.WorldSync ;
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 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 ) ) ;
2021-11-20 19:49:50 +00:00
private SocketStateChangeMessage CreateMessage ( int objid , int socketid , Quaternion localRotation ) = > new ( )
2020-12-22 21:39:53 +00:00
{
AboutId = LocalPlayerId ,
ObjectId = objid ,
SocketId = socketid ,
LocalRotation = localRotation
} ;
public override void OnReceiveRemote ( bool server , SocketStateChangeMessage message )
{
2021-12-04 09:51:27 +00:00
if ( ! WorldObjectManager . AllObjectsReady )
2020-12-22 21:39:53 +00:00
{
return ;
}
2021-06-18 21:38:32 +00:00
2021-02-24 10:45:25 +00:00
var obj = QSBWorldSync . GetWorldFromId < QSBSocketedQuantumObject > ( message . ObjectId ) ;
2021-03-17 17:04:57 +00:00
if ( obj . ControllingPlayer ! = message . FromId )
{
DebugLog . ToConsole ( $"Error - Got SocketStateChangeEvent for {obj.Name} from {message.FromId}, but it's currently controlled by {obj.ControllingPlayer}!" , MessageType . Error ) ;
return ;
}
2021-06-18 21:38:32 +00:00
2021-01-26 14:07:17 +00:00
obj . MoveToSocket ( message ) ;
2020-12-22 21:39:53 +00:00
}
}
}