mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-03-11 01:13:47 +00:00
Sync rotational thruster sounds
This commit is contained in:
parent
7f2d5e3323
commit
882270a6f9
21
QSB/Audio/Messages/ShipThrusterAudioOneShotMessage.cs
Normal file
21
QSB/Audio/Messages/ShipThrusterAudioOneShotMessage.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
45
QSB/Audio/Patches/ThrusterAudioPatches.cs
Normal file
45
QSB/Audio/Patches/ThrusterAudioPatches.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user