mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-04-17 08:43:30 +00:00
extracted crouch sync to new class (#188)
This commit is contained in:
parent
f8c7c437e5
commit
b1daa75d5a
@ -11,11 +11,6 @@ namespace QSB.Animation
|
||||
{
|
||||
public class AnimationSync : NetworkBehaviour
|
||||
{
|
||||
private const float CrouchSendInterval = 0.1f;
|
||||
private const float CrouchChargeThreshold = 0.01f;
|
||||
private const float CrouchSmoothTime = 0.05f;
|
||||
private const int CrouchLayerIndex = 1;
|
||||
|
||||
private Animator _anim;
|
||||
private Animator _bodyAnim;
|
||||
private NetworkAnimator _netAnim;
|
||||
@ -26,10 +21,7 @@ namespace QSB.Animation
|
||||
private GameObject _suitedGraphics;
|
||||
private GameObject _unsuitedGraphics;
|
||||
private PlayerCharacterController _playerController;
|
||||
|
||||
private readonly AnimFloatParam _crouchParam = new AnimFloatParam();
|
||||
private float _sendTimer;
|
||||
private float _lastSentJumpChargeFraction;
|
||||
private CrouchSync _crouchSync;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@ -93,6 +85,8 @@ namespace QSB.Animation
|
||||
|
||||
GlobalMessenger.AddListener(EventNames.SuitUp, OnSuitUp);
|
||||
GlobalMessenger.AddListener(EventNames.RemoveSuit, OnSuitDown);
|
||||
|
||||
InitCrouchSync();
|
||||
}
|
||||
|
||||
public void InitRemote(Transform body)
|
||||
@ -118,6 +112,14 @@ namespace QSB.Animation
|
||||
|
||||
body.Find("player_mesh_noSuit:Traveller_HEA_Player/player_mesh_noSuit:Player_Head").gameObject.layer = 0;
|
||||
body.Find("Traveller_Mesh_v01:Traveller_Geo/Traveller_Mesh_v01:PlayerSuit_Helmet").gameObject.layer = 0;
|
||||
|
||||
InitCrouchSync();
|
||||
}
|
||||
|
||||
private void InitCrouchSync()
|
||||
{
|
||||
_crouchSync = gameObject.AddComponent<CrouchSync>();
|
||||
_crouchSync.Init(this, _playerController, _bodyAnim);
|
||||
}
|
||||
|
||||
private void OnJump() => SendTrigger(AnimTrigger.Jump);
|
||||
@ -148,7 +150,7 @@ namespace QSB.Animation
|
||||
}
|
||||
catch
|
||||
{
|
||||
DebugLog.ToConsole($"Error while geting isServer in AnimationSync! " +
|
||||
DebugLog.ToConsole("Error while getting isServer in AnimationSync! " +
|
||||
$"{Environment.NewLine} - Did a destroyed AnimationSync still have an active action/event listener?" +
|
||||
$"{Environment.NewLine} If you are a user seeing this, please report this error.", OWML.Common.MessageType.Error);
|
||||
}
|
||||
@ -185,7 +187,7 @@ namespace QSB.Animation
|
||||
SuitDown();
|
||||
break;
|
||||
case AnimTrigger.Crouch:
|
||||
_crouchParam.Target = value;
|
||||
_crouchSync.CrouchParam.Target = value;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(trigger), trigger, null);
|
||||
@ -220,49 +222,5 @@ namespace QSB.Animation
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
SyncLocalCrouch();
|
||||
}
|
||||
else
|
||||
{
|
||||
SyncRemoteCrouch();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
SendTrigger(AnimTrigger.Crouch, jumpChargeFraction);
|
||||
_lastSentJumpChargeFraction = jumpChargeFraction;
|
||||
_sendTimer = 0;
|
||||
}
|
||||
|
||||
private void SyncRemoteCrouch()
|
||||
{
|
||||
if (_bodyAnim == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_crouchParam.Smooth(CrouchSmoothTime);
|
||||
var jumpChargeFraction = _crouchParam.Current;
|
||||
_bodyAnim.SetLayerWeight(CrouchLayerIndex, jumpChargeFraction);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
75
QSB/Animation/CrouchSync.cs
Normal file
75
QSB/Animation/CrouchSync.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace QSB.Animation
|
||||
{
|
||||
public class CrouchSync : NetworkBehaviour
|
||||
{
|
||||
public AnimFloatParam CrouchParam { get; } = new AnimFloatParam();
|
||||
|
||||
private const float CrouchSendInterval = 0.1f;
|
||||
private const float CrouchChargeThreshold = 0.01f;
|
||||
private const float CrouchSmoothTime = 0.05f;
|
||||
private const int CrouchLayerIndex = 1;
|
||||
|
||||
private float _sendTimer;
|
||||
private float _lastSentJumpChargeFraction;
|
||||
|
||||
private AnimationSync _animationSync;
|
||||
private PlayerCharacterController _playerController;
|
||||
private Animator _bodyAnim;
|
||||
|
||||
public void Init(AnimationSync animationSync, PlayerCharacterController playerController, Animator bodyAnim)
|
||||
{
|
||||
_animationSync = animationSync;
|
||||
_playerController = playerController;
|
||||
_bodyAnim = bodyAnim;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
SyncLocalCrouch();
|
||||
}
|
||||
else
|
||||
{
|
||||
SyncRemoteCrouch();
|
||||
}
|
||||
}
|
||||
|
||||
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.SendTrigger(AnimTrigger.Crouch, jumpChargeFraction);
|
||||
_lastSentJumpChargeFraction = jumpChargeFraction;
|
||||
_sendTimer = 0;
|
||||
}
|
||||
|
||||
private void SyncRemoteCrouch()
|
||||
{
|
||||
if (_bodyAnim == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
CrouchParam.Smooth(CrouchSmoothTime);
|
||||
var jumpChargeFraction = CrouchParam.Current;
|
||||
_bodyAnim.SetLayerWeight(CrouchLayerIndex, jumpChargeFraction);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -124,6 +124,7 @@
|
||||
<Compile Include="Animation\AnimFloatParam.cs" />
|
||||
<Compile Include="Animation\AnimTrigger.cs" />
|
||||
<Compile Include="Animation\AnimTriggerMessage.cs" />
|
||||
<Compile Include="Animation\CrouchSync.cs" />
|
||||
<Compile Include="DeathSync\DeathPatches.cs" />
|
||||
<Compile Include="ElevatorSync\ElevatorDirection.cs" />
|
||||
<Compile Include="ElevatorSync\QSBElevator.cs" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user