From 67c199f250bebf815b33c462bb062cf494afbc8d Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 27 Aug 2022 18:07:50 -0400 Subject: [PATCH] Sync jump and footstep sounds --- .../PlayerMovementAudioFootstepMessage.cs | 16 ++++++++++ .../PlayerMovementAudioJumpMessage.cs | 16 ++++++++++ .../Patches/PlayerMovementAudioPatches.cs | 32 +++++++++++++++++++ QSB/Audio/QSBPlayerAudioController.cs | 16 ++++++++-- 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 QSB/Audio/Messages/PlayerMovementAudioFootstepMessage.cs create mode 100644 QSB/Audio/Messages/PlayerMovementAudioJumpMessage.cs create mode 100644 QSB/Audio/Patches/PlayerMovementAudioPatches.cs diff --git a/QSB/Audio/Messages/PlayerMovementAudioFootstepMessage.cs b/QSB/Audio/Messages/PlayerMovementAudioFootstepMessage.cs new file mode 100644 index 00000000..f17503ec --- /dev/null +++ b/QSB/Audio/Messages/PlayerMovementAudioFootstepMessage.cs @@ -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); +} diff --git a/QSB/Audio/Messages/PlayerMovementAudioJumpMessage.cs b/QSB/Audio/Messages/PlayerMovementAudioJumpMessage.cs new file mode 100644 index 00000000..adae8149 --- /dev/null +++ b/QSB/Audio/Messages/PlayerMovementAudioJumpMessage.cs @@ -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); +} diff --git a/QSB/Audio/Patches/PlayerMovementAudioPatches.cs b/QSB/Audio/Patches/PlayerMovementAudioPatches.cs new file mode 100644 index 00000000..ecdfa51b --- /dev/null +++ b/QSB/Audio/Patches/PlayerMovementAudioPatches.cs @@ -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(); + } +} \ No newline at end of file diff --git a/QSB/Audio/QSBPlayerAudioController.cs b/QSB/Audio/QSBPlayerAudioController.cs index 3a8a9576..4a967902 100644 --- a/QSB/Audio/QSBPlayerAudioController.cs +++ b/QSB/Audio/QSBPlayerAudioController.cs @@ -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); } \ No newline at end of file