2022-01-15 06:54:18 +00:00
|
|
|
|
using Mirror;
|
|
|
|
|
using QSB.Utility.VariableSync;
|
2020-08-20 19:47:53 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
namespace QSB.Animation.Player;
|
|
|
|
|
|
|
|
|
|
public class CrouchSync : NetworkBehaviour
|
2020-08-20 19:47:53 +00:00
|
|
|
|
{
|
2022-03-03 03:46:33 +00:00
|
|
|
|
public AnimFloatParam CrouchParam { get; } = new AnimFloatParam();
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
private const float CrouchSmoothTime = 0.05f;
|
|
|
|
|
public const int CrouchLayerIndex = 1;
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
private PlayerCharacterController _playerController;
|
|
|
|
|
private Animator _bodyAnim;
|
2021-05-03 19:13:03 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
public FloatVariableSyncer CrouchVariableSyncer;
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
public void Init(PlayerCharacterController playerController, Animator bodyAnim)
|
|
|
|
|
{
|
|
|
|
|
_playerController = playerController;
|
|
|
|
|
_bodyAnim = bodyAnim;
|
|
|
|
|
}
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
public void Update()
|
|
|
|
|
{
|
|
|
|
|
if (isLocalPlayer)
|
2022-02-25 06:04:54 +00:00
|
|
|
|
{
|
2022-03-03 03:46:33 +00:00
|
|
|
|
SyncLocalCrouch();
|
|
|
|
|
return;
|
2022-02-27 12:40:44 +00:00
|
|
|
|
}
|
2022-02-25 06:04:54 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
SyncRemoteCrouch();
|
|
|
|
|
}
|
2022-02-27 12:40:44 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
private void SyncLocalCrouch()
|
|
|
|
|
{
|
|
|
|
|
if (_playerController == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2022-02-25 06:04:54 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
var jumpChargeFraction = _playerController.GetJumpCrouchFraction();
|
|
|
|
|
CrouchVariableSyncer.Value = jumpChargeFraction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SyncRemoteCrouch()
|
|
|
|
|
{
|
|
|
|
|
if (_bodyAnim == null)
|
2022-02-27 12:40:44 +00:00
|
|
|
|
{
|
2022-03-03 03:46:33 +00:00
|
|
|
|
return;
|
2022-02-27 12:40:44 +00:00
|
|
|
|
}
|
2022-03-03 03:46:33 +00:00
|
|
|
|
|
|
|
|
|
CrouchParam.Target = CrouchVariableSyncer.Value;
|
|
|
|
|
CrouchParam.Smooth(CrouchSmoothTime);
|
|
|
|
|
var jumpChargeFraction = CrouchParam.Current;
|
|
|
|
|
_bodyAnim.SetLayerWeight(CrouchLayerIndex, jumpChargeFraction);
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2020-11-08 16:13:10 +00:00
|
|
|
|
}
|