custom network behaviour and transform

This commit is contained in:
JohnCorby 2022-01-14 20:13:04 -08:00
parent b9911c0162
commit 3f90a34b1f
3 changed files with 140 additions and 2 deletions

View File

@ -15,7 +15,7 @@
<DebugType>pdbonly</DebugType>
</PropertyGroup>
<Target Name="weave qsb" BeforeTargets="PostBuildEvent" Condition="Exists($(GameDir))">
<Exec Command='"$(SolutionDir)\MirrorWeaver\bin\$(Configuration)\MirrorWeaver" "$(OutputPath)\QSB.dll" "$(GameDir)"'/>
<Exec Command="&quot;$(SolutionDir)\MirrorWeaver\bin\$(Configuration)\MirrorWeaver&quot; &quot;$(OutputPath)\QSB.dll&quot; &quot;$(GameDir)&quot;" />
</Target>
<PropertyGroup>
<PostBuildEvent Condition="Exists($(UnityAssetsDir)) and Exists($(GameDir)) and Exists($(OwmlDir))">
@ -69,7 +69,7 @@ copy /y "$(OwmlDir)\OWML.Abstractions.dll" "$(UnityAssetsDir)"
<ItemGroup>
<PackageReference Include="OuterWildsGameLibs" Version="1.1.12.125" IncludeAssets="compile" />
<ProjectReference Include="..\QuantumUNET\QuantumUNET.csproj" />
<ProjectReference Include="..\Mirror\Mirror.csproj"/>
<ProjectReference Include="..\Mirror\Mirror.csproj" />
<PackageReference Include="HarmonyX" Version="2.8.0" IncludeAssets="compile" />
<PackageReference Include="OWML" Version="2.3.1" IncludeAssets="compile" />
</ItemGroup>

View File

@ -0,0 +1,36 @@
using Mirror;
using QSB.Utility;
using UnityEngine;
namespace QSB.Syncs
{
public class QSBNetworkTransform : QSBNetworkBehaviour
{
protected override float SendInterval => 0.05f;
protected Vector3 _prevPosition;
protected Quaternion _prevRotation;
protected override void UpdatePrevData()
{
_prevPosition = transform.position;
_prevRotation = transform.rotation;
}
protected override bool HasChanged() =>
Vector3.Distance(transform.position, _prevPosition) > 1E-05f ||
Quaternion.Angle(transform.rotation, _prevRotation) > 1E-05f;
protected override void Serialize(NetworkWriter writer, bool initialState)
{
writer.Write(transform.position);
writer.Write(transform.rotation);
}
protected override void Deserialize(NetworkReader reader, bool initialState)
{
transform.position = reader.ReadVector3();
transform.rotation = reader.ReadQuaternion();
}
}
}

View File

@ -0,0 +1,102 @@
using Mirror;
using System;
namespace QSB.Utility
{
public abstract class QSBNetworkBehaviour : NetworkBehaviour
{
protected virtual float SendInterval => 0.1f;
protected virtual bool Unreliable => true;
private double _lastSendTime;
protected abstract bool HasChanged();
protected abstract void UpdatePrevData();
protected abstract void Serialize(NetworkWriter writer, bool initialState);
protected abstract void Deserialize(NetworkReader reader, bool initialState);
public override bool OnSerialize(NetworkWriter writer, bool initialState)
{
var changed = base.OnSerialize(writer, initialState);
if (initialState && isServer)
{
Serialize(writer, true);
}
return changed;
}
public override void OnDeserialize(NetworkReader reader, bool initialState)
{
base.OnDeserialize(reader, initialState);
if (initialState && !isServer)
{
UpdatePrevData();
Deserialize(reader, true);
}
}
protected virtual void Awake() => UpdatePrevData();
protected virtual void Update()
{
if (!isClient)
{
return;
}
if (!hasAuthority)
{
return;
}
if (!NetworkClient.ready)
{
return;
}
if (NetworkTime.localTime >= _lastSendTime + SendInterval)
{
_lastSendTime = NetworkTime.localTime;
if (!HasChanged())
{
return;
}
UpdatePrevData();
using var writer = NetworkWriterPool.GetWriter();
Serialize(writer, false);
var data = writer.ToArraySegment();
if (Unreliable)
{
CmdSendDataUnreliable(data);
}
else
{
CmdSendDataReliable(data);
}
}
}
[Command(channel = Channels.Unreliable, requiresAuthority = true)]
private void CmdSendDataUnreliable(ArraySegment<byte> data) => RpcSendDataUnreliable(data);
[ClientRpc(channel = Channels.Unreliable, includeOwner = false)]
private void RpcSendDataUnreliable(ArraySegment<byte> data) => OnData(data);
[Command(channel = Channels.Reliable, requiresAuthority = true)]
private void CmdSendDataReliable(ArraySegment<byte> data) => RpcSendDataReliable(data);
[ClientRpc(channel = Channels.Reliable, includeOwner = false)]
private void RpcSendDataReliable(ArraySegment<byte> data) => OnData(data);
private void OnData(ArraySegment<byte> data)
{
UpdatePrevData();
using var reader = NetworkReaderPool.GetReader(data);
Deserialize(reader, false);
}
}
}