quantum-space-buddies/QSB/Animation/CrouchSync.cs

72 lines
1.7 KiB
C#
Raw Normal View History

2020-12-04 22:14:53 +00:00
using QuantumUNET;
2020-12-02 21:21:43 +00:00
using System;
using UnityEngine;
namespace QSB.Animation
{
2020-12-02 21:29:53 +00:00
public class CrouchSync : QSBNetworkBehaviour
{
public AnimFloatParam CrouchParam { get; } = new AnimFloatParam();
2020-12-02 21:29:53 +00:00
private const float CrouchSendInterval = 0.1f;
private const float CrouchChargeThreshold = 0.01f;
private const float CrouchSmoothTime = 0.05f;
private const int CrouchLayerIndex = 1;
2020-12-02 21:29:53 +00:00
private float _sendTimer;
private float _lastSentJumpChargeFraction;
2020-12-02 21:29:53 +00:00
private AnimationSync _animationSync;
private PlayerCharacterController _playerController;
private Animator _bodyAnim;
2020-12-02 21:29:53 +00:00
public void Init(AnimationSync animationSync, PlayerCharacterController playerController, Animator bodyAnim)
{
_animationSync = animationSync;
_playerController = playerController;
_bodyAnim = bodyAnim;
}
2020-12-14 20:41:56 +00:00
public void Update()
2020-12-02 21:29:53 +00:00
{
if (IsLocalPlayer)
{
SyncLocalCrouch();
return;
}
SyncRemoteCrouch();
}
2020-12-02 21:29:53 +00:00
private void SyncLocalCrouch()
{
if (_playerController == null)
{
return;
}
_sendTimer += Time.unscaledDeltaTime;
if (_sendTimer < CrouchSendInterval)
{
return;
}
var jumpChargeFraction = _playerController.GetJumpChargeFraction();
if (Math.Abs(jumpChargeFraction - _lastSentJumpChargeFraction) < CrouchChargeThreshold)
{
return;
}
_animationSync.SendCrouch(jumpChargeFraction);
_lastSentJumpChargeFraction = jumpChargeFraction;
_sendTimer = 0;
}
2020-12-02 21:29:53 +00:00
private void SyncRemoteCrouch()
{
if (_bodyAnim == null)
{
return;
}
CrouchParam.Smooth(CrouchSmoothTime);
var jumpChargeFraction = CrouchParam.Current;
_bodyAnim.SetLayerWeight(CrouchLayerIndex, jumpChargeFraction);
}
}
2020-11-08 16:13:10 +00:00
}