even better variable syncer

This commit is contained in:
JohnCorby 2021-12-14 21:42:40 -08:00
parent 9c2ddef242
commit 348eb92728
10 changed files with 70 additions and 195 deletions

View File

@ -15,14 +15,13 @@ namespace QSB.Animation.Player
private Animator _bodyAnim;
public FloatVariableSyncer CrouchVariableSyncer;
public float CrouchValue = 0f;
public void Init(PlayerCharacterController playerController, Animator bodyAnim)
{
_playerController = playerController;
_bodyAnim = bodyAnim;
CrouchVariableSyncer.Init(() => CrouchValue, val => CrouchValue = val);
CrouchVariableSyncer.Init();
}
public void Update()
@ -44,7 +43,7 @@ namespace QSB.Animation.Player
}
var jumpChargeFraction = _playerController.GetJumpCrouchFraction();
CrouchValue = jumpChargeFraction;
CrouchVariableSyncer.Value = jumpChargeFraction;
}
private void SyncRemoteCrouch()
@ -54,7 +53,7 @@ namespace QSB.Animation.Player
return;
}
CrouchParam.Target = CrouchValue;
CrouchParam.Target = CrouchVariableSyncer.Value;
CrouchParam.Smooth(CrouchSmoothTime);
var jumpChargeFraction = CrouchParam.Current;
_bodyAnim.SetLayerWeight(CrouchLayerIndex, jumpChargeFraction);

View File

@ -1,6 +1,5 @@
using QSB.Utility.VariableSync;
using QuantumUNET;
using UnityEngine;
namespace QSB.Animation.Player.Thrusters
{
@ -8,8 +7,6 @@ namespace QSB.Animation.Player.Thrusters
{
public Vector3VariableSyncer AccelerationVariableSyncer;
public BoolVariableSyncer ThrustingVariableSyncer;
public Vector3 LocalAcceleration { get; private set; }
public bool IsThrusting { get; private set; }
private ThrusterModel _thrusterModel;
@ -17,8 +14,8 @@ namespace QSB.Animation.Player.Thrusters
{
_thrusterModel = model;
AccelerationVariableSyncer.Init(() => LocalAcceleration, val => LocalAcceleration = val);
ThrustingVariableSyncer.Init(() => IsThrusting, val => IsThrusting = val);
AccelerationVariableSyncer.Init();
ThrustingVariableSyncer.Init();
}
public void Update()
@ -33,8 +30,8 @@ namespace QSB.Animation.Player.Thrusters
{
if (_thrusterModel != null)
{
LocalAcceleration = _thrusterModel.GetLocalAcceleration();
IsThrusting = _thrusterModel.IsTranslationalThrusterFiring();
AccelerationVariableSyncer.Value = _thrusterModel.GetLocalAcceleration();
ThrustingVariableSyncer.Value = _thrusterModel.IsTranslationalThrusterFiring();
}
}
}

View File

@ -82,7 +82,7 @@ namespace QSB.Animation.Player.Thrusters
_light.enabled = _currentScale > 0f;
}
private float GetThrustFraction() => Vector3.Dot(_attachedPlayer.JetpackAcceleration.LocalAcceleration, _thrusterFilter);
private float GetThrustFraction() => Vector3.Dot(_attachedPlayer.JetpackAcceleration.AccelerationVariableSyncer.Value, _thrusterFilter);
private void OnRenderObject()
{

View File

@ -58,7 +58,7 @@ namespace QSB.Animation.Player.Thrusters
RaycastHit hitInfo = default;
var aboveSurface = false;
var emissionThrusterScale = _emissionThrusterScale.Evaluate(_attachedPlayer.JetpackAcceleration.LocalAcceleration.y);
var emissionThrusterScale = _emissionThrusterScale.Evaluate(_attachedPlayer.JetpackAcceleration.AccelerationVariableSyncer.Value.y);
if (emissionThrusterScale > 0f)
{
aboveSurface = Physics.Raycast(transform.position, transform.forward, out hitInfo, _raycastDistance, OWLayerMask.physicalMask);

View File

@ -1,20 +1,30 @@
using QuantumUNET;
using System;
using QuantumUNET;
using QuantumUNET.Messages;
using QuantumUNET.Transport;
using UnityEngine;
namespace QSB.Utility.VariableSync
{
public abstract class BaseVariableSyncer : QNetworkBehaviour
public interface IBaseVariableSyncer
{
void DeserializeValue(QNetworkReader reader);
}
public abstract class BaseVariableSyncer<T> : QNetworkBehaviour, IBaseVariableSyncer
{
private float _lastClientSendTime;
private QNetworkWriter _writer;
private int _index;
public bool Ready { get; protected set; }
private bool _initialized { get; set; }
public abstract void WriteData(QNetworkWriter writer);
public abstract void ReadData(QNetworkReader writer);
public abstract bool HasChanged();
private T _prevValue;
public T Value { get; set; }
protected abstract void WriteValue(QNetworkWriter writer, T value);
protected abstract T ReadValue(QNetworkReader reader);
private bool HasChanged() => !Equals(Value, _prevValue);
public virtual void Awake()
{
@ -27,10 +37,10 @@ namespace QSB.Utility.VariableSync
}
public virtual void Start()
=> _index = GetComponents<BaseVariableSyncer>().IndexOfReference(this);
=> _index = Array.IndexOf(GetComponents<IBaseVariableSyncer>(), this);
public void OnDestroy()
=> Ready = false;
public void Init() => _initialized = true;
public void OnDestroy() => _initialized = false;
public override bool OnSerialize(QNetworkWriter writer, bool initialState)
{
@ -45,7 +55,7 @@ namespace QSB.Utility.VariableSync
writer.WritePackedUInt32(1U);
}
WriteData(writer);
SerializeValue(writer);
return true;
}
@ -58,13 +68,38 @@ namespace QSB.Utility.VariableSync
return;
}
ReadData(reader);
DeserializeValue(reader);
}
}
public void SerializeValue(QNetworkWriter writer)
{
if (_initialized)
{
WriteValue(writer, Value);
_prevValue = Value;
}
else
{
WriteValue(writer, default);
}
}
public void DeserializeValue(QNetworkReader reader)
{
if (_initialized)
{
Value = ReadValue(reader);
}
else
{
ReadValue(reader);
}
}
private void FixedUpdate()
{
if (!IsServer || SyncVarDirtyBits != 0U || !QNetworkServer.active || !Ready)
if (!IsServer || SyncVarDirtyBits != 0U || !QNetworkServer.active || !_initialized)
{
return;
}
@ -77,7 +112,7 @@ namespace QSB.Utility.VariableSync
public virtual void Update()
{
if (!HasAuthority || !LocalPlayerAuthority || QNetworkServer.active || !Ready)
if (!HasAuthority || !LocalPlayerAuthority || QNetworkServer.active || !_initialized)
{
return;
}
@ -96,7 +131,7 @@ namespace QSB.Utility.VariableSync
_writer.StartMessage(short.MaxValue);
_writer.Write(NetId);
_writer.Write(_index);
WriteData(_writer);
SerializeValue(_writer);
_writer.FinishMessage();
QClientScene.readyConnection.SendWriter(_writer, GetNetworkChannel());
}
@ -105,10 +140,10 @@ namespace QSB.Utility.VariableSync
public static void HandleVariable(QNetworkMessage netMsg)
{
var networkInstanceId = netMsg.Reader.ReadNetworkId();
var index = netMsg.Reader.ReadInt32();
var gameObject = QNetworkServer.FindLocalObject(networkInstanceId);
var components = gameObject.GetComponents<BaseVariableSyncer>();
components[index].ReadData(netMsg.Reader);
var index = netMsg.Reader.ReadInt32();
var component = gameObject.GetComponents<IBaseVariableSyncer>()[index];
component.DeserializeValue(netMsg.Reader);
}
}
}

View File

@ -1,44 +1,10 @@
using QuantumUNET.Transport;
using System;
namespace QSB.Utility.VariableSync
{
public class BoolVariableSyncer : BaseVariableSyncer
public class BoolVariableSyncer : BaseVariableSyncer<bool>
{
private VariableReference<bool> _ref;
private bool _prevValue;
public void Init(Func<bool> getter, Action<bool> setter)
{
_ref = new VariableReference<bool>(this, getter, setter);
Ready = true;
}
public override void WriteData(QNetworkWriter writer)
{
if (Ready)
{
writer.Write(_ref.Value);
_prevValue = _ref.Value;
}
else
{
writer.Write(default(bool));
}
}
public override void ReadData(QNetworkReader reader)
{
if (Ready)
{
_ref.Value = reader.ReadBoolean();
}
else
{
reader.ReadBoolean();
}
}
public override bool HasChanged() => _ref.Value != _prevValue;
protected override void WriteValue(QNetworkWriter writer, bool value) => writer.Write(value);
protected override bool ReadValue(QNetworkReader reader) => reader.ReadBoolean();
}
}

View File

@ -1,44 +1,10 @@
using QuantumUNET.Transport;
using System;
namespace QSB.Utility.VariableSync
{
public class FloatVariableSyncer : BaseVariableSyncer
public class FloatVariableSyncer : BaseVariableSyncer<float>
{
private VariableReference<float> _ref;
private float _prevValue;
public void Init(Func<float> getter, Action<float> setter)
{
_ref = new VariableReference<float>(this, getter, setter);
Ready = true;
}
public override void WriteData(QNetworkWriter writer)
{
if (Ready)
{
writer.Write(_ref.Value);
_prevValue = _ref.Value;
}
else
{
writer.Write(default(float));
}
}
public override void ReadData(QNetworkReader reader)
{
if (Ready)
{
_ref.Value = reader.ReadSingle();
}
else
{
reader.ReadSingle();
}
}
public override bool HasChanged() => _ref.Value != _prevValue;
protected override void WriteValue(QNetworkWriter writer, float value) => writer.Write(value);
protected override float ReadValue(QNetworkReader reader) => reader.ReadSingle();
}
}

View File

@ -1,52 +0,0 @@
using System;
namespace QSB.Utility.VariableSync
{
public readonly struct VariableReference<T>
{
private readonly BaseVariableSyncer _owner;
private readonly Func<T> _getter;
private readonly Action<T> _setter;
public VariableReference(BaseVariableSyncer owner, Func<T> getter, Action<T> setter)
{
_owner = owner;
_getter = getter;
_setter = setter;
}
public T Value
{
get
{
if (_getter != null)
{
return _getter();
}
else
{
if (_owner.Ready)
{
DebugLog.ToConsole($"Warning - Getter is null!", OWML.Common.MessageType.Warning);
}
return default;
}
}
set
{
if (_setter != null)
{
_setter(value);
}
else
{
if (_owner.Ready)
{
DebugLog.ToConsole($"Warning - Setter is null!", OWML.Common.MessageType.Warning);
}
}
}
}
}
}

View File

@ -1,45 +1,11 @@
using QuantumUNET.Transport;
using System;
using UnityEngine;
namespace QSB.Utility.VariableSync
{
public class Vector3VariableSyncer : BaseVariableSyncer
public class Vector3VariableSyncer : BaseVariableSyncer<Vector3>
{
private VariableReference<Vector3> _ref;
private Vector3 _prevValue;
public void Init(Func<Vector3> getter, Action<Vector3> setter)
{
_ref = new VariableReference<Vector3>(this, getter, setter);
Ready = true;
}
public override void WriteData(QNetworkWriter writer)
{
if (Ready)
{
writer.Write(_ref.Value);
_prevValue = _ref.Value;
}
else
{
writer.Write(default(Vector3));
}
}
public override void ReadData(QNetworkReader reader)
{
if (Ready)
{
_ref.Value = reader.ReadVector3();
}
else
{
reader.ReadVector3();
}
}
public override bool HasChanged() => _ref.Value != _prevValue;
protected override void WriteValue(QNetworkWriter writer, Vector3 value) => writer.Write(value);
protected override Vector3 ReadValue(QNetworkReader reader) => reader.ReadVector3();
}
}

View File

@ -134,8 +134,6 @@ namespace QuantumUNET.Components
_localTransformWriter.StartMessage(QMsgType.LocalPlayerTransform);
_localTransformWriter.Write(NetId);
SerializeTransform(_localTransformWriter, false);
_prevPosition = transform.position;
_prevRotation = transform.rotation;
_localTransformWriter.FinishMessage();
QClientScene.readyConnection.SendWriter(_localTransformWriter, GetNetworkChannel());