Code/Network/WaterNetwork.Audio.cs

Partial WaterNetwork file that sets up an underwater audio muffling effect. Adds a hidden, unsaved DspVolume with the water.medium preset to the same object and toggles it as the name implies.

using Sandbox;
using Sandbox.Volumes;

namespace RedSnail.WaterTool2;

public sealed partial class WaterNetwork
{
	private const string UnderwaterDspPreset = "water.medium";

	/// <summary>
	/// Muffles the game's sound while the camera is under the surface: an infinite DSP volume on this object, switched by
	/// the same test as <see cref="UnderwaterPostProcessVolume"/>. Hidden, never saved and never networked: every machine
	/// switches its own from its own camera, so a client joining is never handed the host's, switched on.
	/// </summary>
	private DspVolume m_UnderwaterDsp;



	/// <summary>
	/// Takes the volume already on this object when there is one rather than adding a second, and always starts it off,
	/// whatever state it came in with.
	/// </summary>
	private void AcquireUnderwaterDsp()
	{
		// The engine applies no DSP in an editor scene, and nothing listens on a dedicated server.
		if (Scene.IsEditor || Application.IsHeadless)
			return;

		m_UnderwaterDsp = GetOrAddComponent<DspVolume>(false);
		m_UnderwaterDsp.Flags |= ComponentFlags.Hidden | ComponentFlags.NotSaved | ComponentFlags.NotNetworked;
		m_UnderwaterDsp.SceneVolume = new SceneVolume { Type = SceneVolume.VolumeTypes.Infinite };
		m_UnderwaterDsp.Dsp = UnderwaterDspPreset;
		m_UnderwaterDsp.Enabled = false;
	}



	private void ReleaseUnderwaterDsp()
	{
		if (m_UnderwaterDsp.IsValid())
			m_UnderwaterDsp.Enabled = false;
	}
}