2021-11-26 17:36:14 +00:00
|
|
|
|
using QSB.Utility;
|
|
|
|
|
using QSB.Utility.VariableSync;
|
|
|
|
|
using QuantumUNET;
|
2020-08-20 19:47:53 +00:00
|
|
|
|
using UnityEngine;
|
2021-05-03 19:13:03 +00:00
|
|
|
|
using UnityEngine.Networking;
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2021-04-26 13:30:21 +00:00
|
|
|
|
namespace QSB.Animation.Player
|
2020-08-20 19:47:53 +00:00
|
|
|
|
{
|
2020-12-23 12:58:45 +00:00
|
|
|
|
public class CrouchSync : QNetworkBehaviour
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
public AnimFloatParam CrouchParam { get; } = new AnimFloatParam();
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private const float CrouchSmoothTime = 0.05f;
|
|
|
|
|
private const int CrouchLayerIndex = 1;
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private PlayerCharacterController _playerController;
|
|
|
|
|
private Animator _bodyAnim;
|
2021-11-26 17:36:14 +00:00
|
|
|
|
private VariableReference<float> _crouchValueReference;
|
|
|
|
|
private FloatVariableSyncer _variableSyncer;
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2021-11-26 17:36:14 +00:00
|
|
|
|
public float CrouchValue;
|
2021-05-03 19:13:03 +00:00
|
|
|
|
|
|
|
|
|
public void Init(PlayerCharacterController playerController, Animator bodyAnim)
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
_playerController = playerController;
|
|
|
|
|
_bodyAnim = bodyAnim;
|
2021-11-26 17:36:14 +00:00
|
|
|
|
|
|
|
|
|
DebugLog.DebugWrite($"create reference");
|
|
|
|
|
_crouchValueReference = new VariableReference<float>(() => CrouchValue, val => CrouchValue = val);
|
|
|
|
|
DebugLog.DebugWrite($"add syncer");
|
|
|
|
|
_variableSyncer = gameObject.AddComponent<FloatVariableSyncer>();
|
|
|
|
|
_variableSyncer.FloatToSync = _crouchValueReference;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2020-12-14 20:41:56 +00:00
|
|
|
|
public void Update()
|
2020-12-02 21:29:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (IsLocalPlayer)
|
|
|
|
|
{
|
|
|
|
|
SyncLocalCrouch();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 21:38:32 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
SyncRemoteCrouch();
|
|
|
|
|
}
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private void SyncLocalCrouch()
|
|
|
|
|
{
|
|
|
|
|
if (_playerController == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 21:38:32 +00:00
|
|
|
|
|
2021-10-12 14:31:02 +00:00
|
|
|
|
var jumpChargeFraction = _playerController.GetJumpCrouchFraction();
|
2021-11-26 17:36:14 +00:00
|
|
|
|
DebugLog.DebugWrite($"update reference value");
|
|
|
|
|
_crouchValueReference.Value = jumpChargeFraction;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
}
|
2020-08-20 19:47:53 +00:00
|
|
|
|
|
2020-12-02 21:29:53 +00:00
|
|
|
|
private void SyncRemoteCrouch()
|
|
|
|
|
{
|
|
|
|
|
if (_bodyAnim == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 21:38:32 +00:00
|
|
|
|
|
2021-11-26 17:36:14 +00:00
|
|
|
|
CrouchParam.Target = CrouchValue;
|
2020-12-02 21:29:53 +00:00
|
|
|
|
CrouchParam.Smooth(CrouchSmoothTime);
|
|
|
|
|
var jumpChargeFraction = CrouchParam.Current;
|
|
|
|
|
_bodyAnim.SetLayerWeight(CrouchLayerIndex, jumpChargeFraction);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-08 16:13:10 +00:00
|
|
|
|
}
|