206 lines
4.5 KiB
C#
Raw Normal View History

2020-12-23 13:48:31 +00:00
using QuantumUNET.Logging;
2020-12-07 21:19:16 +00:00
using QuantumUNET.Messages;
using QuantumUNET.Transport;
2020-12-03 13:18:45 +00:00
using UnityEngine;
2020-12-07 21:19:16 +00:00
namespace QuantumUNET.Components
2020-12-03 13:18:45 +00:00
{
2020-12-23 12:58:45 +00:00
public class QNetworkTransform : QNetworkBehaviour
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:28:45 +01:00
public float SendInterval { get; set; } = 0.05f;
2020-12-03 13:18:45 +00:00
2021-11-13 19:17:22 -08:00
private float _lastClientSendTime;
2021-04-21 11:02:17 +01:00
protected Vector3 _prevPosition;
protected Quaternion _prevRotation;
private QNetworkWriter _localTransformWriter;
public void Awake()
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
_prevPosition = transform.position;
_prevRotation = transform.rotation;
2020-12-04 09:23:27 +00:00
if (LocalPlayerAuthority)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
_localTransformWriter = new QNetworkWriter();
2020-12-03 13:18:45 +00:00
}
}
2020-12-23 12:58:45 +00:00
public override bool OnSerialize(QNetworkWriter writer, bool initialState)
2020-12-03 13:18:45 +00:00
{
if (!initialState)
{
2020-12-04 09:23:27 +00:00
if (SyncVarDirtyBits == 0U)
2020-12-03 13:18:45 +00:00
{
writer.WritePackedUInt32(0U);
return false;
}
2021-06-18 22:39:21 +01:00
2020-12-03 13:18:45 +00:00
writer.WritePackedUInt32(1U);
}
2021-06-18 22:39:21 +01:00
SerializeTransform(writer, initialState);
2020-12-03 13:18:45 +00:00
return true;
}
2020-12-23 12:58:45 +00:00
public override void OnDeserialize(QNetworkReader reader, bool initialState)
2020-12-03 13:18:45 +00:00
{
2020-12-23 12:58:45 +00:00
if (!IsServer || !QNetworkServer.localClientActive)
2020-12-03 13:18:45 +00:00
{
if (!initialState)
{
if (reader.ReadPackedUInt32() == 0U)
{
return;
}
}
2021-06-18 22:39:21 +01:00
DeserializeTransform(reader, initialState);
2020-12-03 13:18:45 +00:00
}
}
public virtual void SerializeTransform(QNetworkWriter writer, bool initialState)
2021-04-21 11:02:17 +01:00
{
writer.Write(transform.position);
SerializeRotation(writer, transform.rotation);
_prevPosition = transform.position;
_prevRotation = transform.rotation;
}
public virtual void DeserializeTransform(QNetworkReader reader, bool initialState)
2020-12-03 13:18:45 +00:00
{
2020-12-04 09:23:27 +00:00
if (HasAuthority)
2020-12-03 13:18:45 +00:00
{
reader.ReadVector3();
2021-04-21 11:02:17 +01:00
DeserializeRotation(reader);
return;
2020-12-03 13:18:45 +00:00
}
2021-06-18 22:39:21 +01:00
2021-04-21 11:02:17 +01:00
transform.position = reader.ReadVector3();
transform.rotation = DeserializeRotation(reader);
}
private void FixedUpdate()
{
if (!IsServer)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
return;
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
if (SyncVarDirtyBits != 0U)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
return;
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
if (!QNetworkServer.active)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
return;
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
if (GetNetworkSendInterval() != 0f && HasMoved())
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
SetDirtyBit(1U);
2020-12-03 13:18:45 +00:00
}
}
2021-04-21 11:02:17 +01:00
public virtual void Update()
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
if (!HasAuthority || !LocalPlayerAuthority)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
return;
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
if (QNetworkServer.active)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
return;
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
if (Time.time - _lastClientSendTime > GetNetworkSendInterval())
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
SendTransform();
_lastClientSendTime = Time.time;
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
}
2021-04-21 11:28:45 +01:00
public virtual bool HasMoved()
2021-04-21 11:02:17 +01:00
{
var displacementMagnitude = (transform.position - _prevPosition).magnitude;
return displacementMagnitude > 1E-05f
|| Quaternion.Angle(transform.rotation, _prevRotation) > 1E-05f;
2020-12-03 13:18:45 +00:00
}
private void SendTransform()
{
2020-12-23 12:58:45 +00:00
if (HasMoved() && QClientScene.readyConnection != null)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
_localTransformWriter.StartMessage(QMsgType.LocalPlayerTransform);
_localTransformWriter.Write(NetId);
SerializeTransform(_localTransformWriter, false);
2021-04-21 11:02:17 +01:00
_localTransformWriter.FinishMessage();
QClientScene.readyConnection.SendWriter(_localTransformWriter, GetNetworkChannel());
2020-12-03 13:18:45 +00:00
}
}
2020-12-23 12:58:45 +00:00
public static void HandleTransform(QNetworkMessage netMsg)
2020-12-03 13:18:45 +00:00
{
2020-12-04 09:23:27 +00:00
var networkInstanceId = netMsg.Reader.ReadNetworkId();
2020-12-23 12:58:45 +00:00
var gameObject = QNetworkServer.FindLocalObject(networkInstanceId);
2020-12-03 13:18:45 +00:00
if (gameObject == null)
{
QLog.Warning("Received NetworkTransform data for GameObject that doesn't exist");
2021-04-21 11:02:17 +01:00
return;
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
var component = gameObject.GetComponent<QNetworkTransform>();
if (component == null)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
QLog.Warning("HandleTransform null target");
return;
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
if (!component.LocalPlayerAuthority)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
QLog.Warning("HandleTransform no localPlayerAuthority");
return;
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
if (netMsg.Connection.ClientOwnedObjects == null)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
QLog.Warning("HandleTransform object not owned by connection");
return;
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
if (netMsg.Connection.ClientOwnedObjects.Contains(networkInstanceId))
2020-12-03 13:18:45 +00:00
{
component.DeserializeTransform(netMsg.Reader, false);
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
else
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
QLog.Warning(
$"HandleTransform netId:{networkInstanceId} is not for a valid player");
2020-12-03 13:18:45 +00:00
}
}
2021-04-21 11:02:17 +01:00
public static void SerializeRotation(QNetworkWriter writer, Quaternion rot)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
writer.Write(rot.eulerAngles.x);
writer.Write(rot.eulerAngles.y);
writer.Write(rot.eulerAngles.z);
2020-12-03 13:18:45 +00:00
}
2021-04-21 11:02:17 +01:00
public static Quaternion DeserializeRotation(QNetworkReader reader)
2020-12-03 13:18:45 +00:00
{
2021-04-21 11:02:17 +01:00
var rotation = Quaternion.identity;
var eulerAngles = Vector3.zero;
eulerAngles.Set(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
rotation.eulerAngles = eulerAngles;
return rotation;
2020-12-03 13:18:45 +00:00
}
2021-06-19 11:26:05 +01:00
public override int GetNetworkChannel()
2021-04-21 11:02:17 +01:00
=> 1;
2021-06-19 11:26:05 +01:00
public override float GetNetworkSendInterval()
2021-04-21 11:02:17 +01:00
=> SendInterval;
2020-12-03 13:18:45 +00:00
}
2021-11-13 16:57:06 -08:00
}