Sync rotational thruster sounds

This commit is contained in:
Nick 2022-08-28 00:05:00 -04:00
parent 7f2d5e3323
commit 882270a6f9
2 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,21 @@
using QSB.Messaging;
using QSB.ShipSync;
using QSB.WorldSync;
namespace QSB.Audio.Messages;
public class ShipThrusterAudioOneShotMessage : QSBMessage<(AudioType audioType, float pitch, float volume)>
{
public ShipThrusterAudioOneShotMessage(AudioType audioType, float pitch = 1f, float volume = 1f) : base((audioType, pitch, volume)) { }
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
public override void OnReceiveRemote()
{
var source = ShipManager.Instance.ShipThrusterAudio._rotationalSource;
source.pitch = Data.pitch;
source.PlayOneShot(Data.audioType, Data.volume);
}
}

View File

@ -0,0 +1,45 @@
using HarmonyLib;
using QSB.Audio.Messages;
using QSB.Messaging;
using QSB.Patches;
using QSB.Player;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QSB.Audio.Patches;
internal class ThrusterAudioPatches : QSBPatch
{
// Since we patch Start we do it when the mod starts, else it won't run
public override QSBPatchTypes Type => QSBPatchTypes.OnModStart;
[HarmonyPostfix]
[HarmonyPatch(typeof(ThrusterAudio), nameof(ThrusterAudio.Start))]
public static void ThrusterAudio_Start(ThrusterAudio __instance)
{
__instance._rotationalSource.gameObject.AddComponent<QSBAudioSourceOneShotTracker>();
}
[HarmonyPrefix]
[HarmonyPatch(typeof(ThrusterAudio), nameof(ThrusterAudio.OnFireRotationalThruster))]
public static void ThrusterAudio_OnFireRotationalThruster_Prefix(ThrusterAudio __instance) =>
// First we reset in case no audio is actually played
__instance._rotationalSource.gameObject.GetComponent<QSBAudioSourceOneShotTracker>()?.Reset();
[HarmonyPostfix]
[HarmonyPatch(typeof(ThrusterAudio), nameof(ThrusterAudio.OnFireRotationalThruster))]
public static void ThrusterAudio_OnFireRotationalThruster_Postfix(ThrusterAudio __instance)
{
if (__instance._rotationalSource.gameObject.TryGetComponent<QSBAudioSourceOneShotTracker>(out var tracker))
{
if (tracker.LastPlayed != AudioType.None)
{
if (__instance is ShipThrusterAudio)
new ShipThrusterAudioOneShotMessage(tracker.LastPlayed, tracker.Pitch, tracker.Volume).Send();
}
}
}
}