Story/CaveAmbience.cs

...

Really Bad Code
using System;
using System.Linq;
using Sandbox;

/// <summary>
/// The sound of the dark cave: a low drone that never stops, plus drips and rock creaks that
/// come from random spots in the dark around the player. Put it on one object in the scene.
/// It only runs in play mode.
/// </summary>
[Title( "Cave Ambience" )]
[Category( "Story" )]
[Icon( "graphic_eq" )]
public sealed class CaveAmbience : Component
{
	/// <summary>The looping drone. Its sound event is flagged as UI, so it has no position.</summary>
	[Property, Group( "Drone" )] public string DroneSound { get; set; } = "sounds/opening/drone_loop.sound";

	/// <summary>How loud the drone is.</summary>
	[Property, Group( "Drone" ), Range( 0f, 2f )] public float DroneVolume { get; set; } = 0.6f;

	[Property, Group( "One-shots" )] public string DripSound { get; set; } = "sounds/opening/drip.sound";
	[Property, Group( "One-shots" )] public string CreakSound { get; set; } = "sounds/opening/creak.sound";

	/// <summary>How loud the drips and creaks are.</summary>
	[Property, Group( "One-shots" ), Range( 0f, 2f )] public float OneShotVolume { get; set; } = 1f;

	/// <summary>Shortest wait between two drips or creaks, in seconds.</summary>
	[Property, Group( "One-shots" )] public float MinInterval { get; set; } = 3f;

	/// <summary>Longest wait between two drips or creaks, in seconds.</summary>
	[Property, Group( "One-shots" )] public float MaxInterval { get; set; } = 9f;

	/// <summary>How many of the one-shots are creaks. The rest are drips.</summary>
	[Property, Group( "One-shots" ), Range( 0f, 1f )] public float CreakChance { get; set; } = 0.25f;

	/// <summary>Closest a drip or creak plays to the player, in units.</summary>
	[Property, Group( "One-shots" )] public float MinDistance { get; set; } = 300f;

	/// <summary>Farthest a drip or creak plays from the player, in units.</summary>
	[Property, Group( "One-shots" )] public float MaxDistance { get; set; } = 1200f;

	SoundHandle _drone;
	SoundHandle _lastOneShot;
	TimeUntil _nextOneShot;

	protected override void OnEnabled()
	{
		_nextOneShot = RandomInterval();
	}

	protected override void OnUpdate()
	{
		KeepDroneGoing();

		if ( _nextOneShot )
		{
			_nextOneShot = RandomInterval();

			// After the jolt the cave holds its breath: no more drips or creaks, only the drone.
			var stage = StoryDirector.Get( Scene )?.Stage ?? StoryStage.Drifting;
			if ( stage < StoryStage.Captured ) PlayOneShot();
		}
	}

	protected override void OnDisabled()
	{
		StopHandle( _drone );
		StopHandle( _lastOneShot );
		_drone = null;
		_lastOneShot = null;
	}

	// Starts the drone, and starts it again if it ever ends (in case the file's loop flag
	// didn't take). Also follows changes to DroneVolume.
	void KeepDroneGoing()
	{
		if ( string.IsNullOrEmpty( DroneSound ) ) return;

		if ( _drone is null || !_drone.IsValid() || _drone.IsStopped || _drone.Finished )
			_drone = Sound.Play( DroneSound );

		// The drone swells and rises once the asteroid has been caught: something is coming.
		var stage = StoryDirector.Get( Scene )?.Stage ?? StoryStage.Drifting;
		float target = stage >= StoryStage.Captured ? 1f : 0f;
		_tension += (target - _tension) * MathF.Min( 1f, Time.Delta * 0.25f );

		if ( _drone is not null && _drone.IsValid() )
		{
			_drone.Volume = DroneVolume * (1f + 0.9f * _tension);
			_drone.Pitch = 1f + 0.18f * _tension;
		}
	}

	float _tension;

	void PlayOneShot()
	{
		var creak = Game.Random.Float( 0f, 1f ) < CreakChance;
		var sound = creak ? CreakSound : DripSound;
		if ( string.IsNullOrEmpty( sound ) ) return;

		var distance = Game.Random.Float( MathF.Min( MinDistance, MaxDistance ), MathF.Max( MinDistance, MaxDistance ) );
		var position = ListenerPosition() + Vector3.Random.Normal * distance;

		var handle = Sound.Play( sound, position );
		if ( handle is null || !handle.IsValid() ) return;

		handle.Volume *= OneShotVolume;
		_lastOneShot = handle;
	}

	// Where the player is: the lizard if there is one, otherwise the camera.
	Vector3 ListenerPosition()
	{
		var lizard = Scene.GetAllComponents<ReptileController>().FirstOrDefault();
		if ( lizard.IsValid() ) return lizard.WorldPosition;
		if ( Scene.Camera.IsValid() ) return Scene.Camera.WorldPosition;
		return WorldPosition;
	}

	float RandomInterval()
	{
		var min = MathF.Max( 0.1f, MathF.Min( MinInterval, MaxInterval ) );
		var max = MathF.Max( min, MathF.Max( MinInterval, MaxInterval ) );
		return Game.Random.Float( min, max );
	}

	static void StopHandle( SoundHandle handle )
	{
		if ( handle is not null && handle.IsValid() ) handle.Stop( 0.3f );
	}
}