Sync jump and footstep sounds

This commit is contained in:
Nick 2022-08-27 18:07:50 -04:00
parent 0afb993062
commit 67c199f250
4 changed files with 78 additions and 2 deletions

View File

@ -0,0 +1,16 @@
using QSB.Messaging;
using QSB.Player;
using QSB.WorldSync;
namespace QSB.Audio.Messages;
public class PlayerMovementAudioFootstepMessage : QSBMessage<(AudioType audioType, float pitch, uint userID)>
{
public PlayerMovementAudioFootstepMessage(AudioType audioType, float pitch, uint userID) : base((audioType, pitch, userID)) { }
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
public override void OnReceiveRemote() =>
QSBPlayerManager.GetPlayer(Data.userID)?.AudioController?.PlayFootstep(Data.audioType, Data.pitch);
}

View File

@ -0,0 +1,16 @@
using QSB.Messaging;
using QSB.Player;
using QSB.WorldSync;
namespace QSB.Audio.Messages;
public class PlayerMovementAudioJumpMessage : QSBMessage<(float pitch, uint userID)>
{
public PlayerMovementAudioJumpMessage(float pitch, uint userID) : base((pitch, userID)) { }
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
public override void OnReceiveRemote() =>
QSBPlayerManager.GetPlayer(Data.userID)?.AudioController?.OnJump(Data.pitch);
}

View File

@ -0,0 +1,32 @@
using HarmonyLib;
using QSB.Audio.Messages;
using QSB.Messaging;
using QSB.Patches;
using QSB.Player;
namespace QSB.Audio.Patches;
internal class PlayerMovementAudioPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPostfix]
[HarmonyPatch(typeof(PlayerMovementAudio), nameof(PlayerMovementAudio.PlayFootstep))]
public static void PlayerMovementAudio_PlayFootstep(PlayerMovementAudio __instance)
{
var underwater = !PlayerState.IsCameraUnderwater() && __instance._fluidDetector.InFluidType(FluidVolume.Type.WATER);
var audioType = underwater ? AudioType.MovementShallowWaterFootstep : PlayerMovementAudio.GetFootstepAudioType(__instance._playerController.GetGroundSurface());
if (audioType != AudioType.None)
{
new PlayerMovementAudioFootstepMessage(audioType, __instance._footstepAudio.pitch, QSBPlayerManager.LocalPlayerId).Send();
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(PlayerMovementAudio), nameof(PlayerMovementAudio.OnJump))]
public static void PlayerMovementAudio_OnJump(PlayerMovementAudio __instance)
{
new PlayerMovementAudioJumpMessage(__instance._jumpAudio.pitch, QSBPlayerManager.LocalPlayerId).Send();
}
}

View File

@ -27,6 +27,18 @@ public class QSBPlayerAudioController : MonoBehaviour
public void PlayRemoveSuit()
=> PlayOneShot(AudioType.PlayerSuitRemoveSuit);
public void PlayOneShot(AudioType audioType)
=> _oneShotExternalSource?.PlayOneShot(audioType, 1f);
public void PlayOneShot(AudioType audioType, float pitch = 1f, float volume = 1f)
{
if (_oneShotExternalSource)
{
_oneShotExternalSource.pitch = pitch;
_oneShotExternalSource.PlayOneShot(audioType, volume);
}
}
public void PlayFootstep(AudioType audioType, float pitch) =>
PlayOneShot(audioType, pitch, 0.7f);
public void OnJump(float pitch) =>
PlayOneShot(AudioType.MovementJump, pitch, 0.7f);
}