quantum-space-buddies/QSB/Audio/Patches/ThrusterAudioPatches.cs

43 lines
1.4 KiB
C#
Raw Permalink Normal View History

2022-08-28 04:05:00 +00:00
using HarmonyLib;
using QSB.Audio.Messages;
using QSB.Messaging;
using QSB.Patches;
namespace QSB.Audio.Patches;
2023-07-28 18:30:57 +00:00
public class ThrusterAudioPatches : QSBPatch
2022-08-28 04:05:00 +00:00
{
// 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)
2022-08-28 17:30:03 +00:00
{
2022-08-28 04:05:00 +00:00
new ShipThrusterAudioOneShotMessage(tracker.LastPlayed, tracker.Pitch, tracker.Volume).Send();
2022-08-28 17:30:03 +00:00
}
2022-08-28 17:46:14 +00:00
// TODO: Apply to player jetpack thruster?
2022-08-28 04:05:00 +00:00
}
}
}
}