Network/WaterNetwork.Audio.cs

Partial class for WaterNetwork that adds an underwater audio muffling effect. Creates an infinite DspVolume on the object with the water.medium preset, kept hidden, unsaved and unnetworked, and enables it based on the camera being underwater.

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;
	}
}