MusicManager.cs

The MusicManager component manages audio playback for the game's music and ambient sounds. It controls ambient music and panic sounds, fading their volumes based on the distance of a nextbot.

using Sandbox;
using System;
using System.Linq;

public class MusicManager : Component
{
    public static MusicManager Instance { get; private set; }
    public static SoundHandle ActiveMusicHandle { get; private set; }

    [Property] public SoundEvent MusicSound { get; set; }
    [Property] public SoundEvent AmbientSound { get; set; }
    [Property] public SoundEvent PanicSound { get; set; }

    [Property] public float PanicDistance { get; set; } = 750f; // 15 meters in s&box units
    [Property] public float MasterVolume { get; set; } = 0.6f;

    private SoundHandle _ambientHandle;
    private SoundHandle _panicHandle;
    private float _currentPanicIntensity = 0f;

    protected override void OnStart()
    {
        if ( Instance != null && Instance != this && Instance.IsValid() )
        {
            Instance.StopMusic();
        }

        Instance = this;

        // Auto-assign default audio files if not assigned in Inspector
        if ( AmbientSound == null && MusicSound != null )
        {
            AmbientSound = MusicSound;
        }

        if ( PushHud.EnableMusic )
        {
            PlayMusic();
        }
    }

    protected override void OnUpdate()
    {
        if ( !PushHud.EnableMusic )
        {
            if ( _ambientHandle.IsValid() || _panicHandle.IsValid() )
            {
                StopMusic();
            }
            return;
        }

        // Check if Nextbot mode is active
        var localPlayer = PlayerHealth.LocalInstance;
        Vector3 playerPos = localPlayer.IsValid() ? localPlayer.WorldPosition :
                            ( Scene.Camera.IsValid() ? Scene.Camera.WorldPosition : Vector3.Zero );

        float nearestBotDistance = NextbotAI.GetClosestBotDistance( playerPos );
        bool hasNextbots = nearestBotDistance < 10000f;

        float targetPanic = 0f;
        if ( hasNextbots && nearestBotDistance < PanicDistance )
        {
            // Linear to smooth curve from 15m (0) to 0m (1.0)
            targetPanic = MathX.Clamp( 1.0f - ( nearestBotDistance / PanicDistance ), 0f, 1f );
        }

        _currentPanicIntensity = MathX.Lerp( _currentPanicIntensity, targetPanic, Time.Delta * 4f );

        // Update volumes with crossfade
        if ( _ambientHandle.IsValid() )
        {
            float targetAmbientVol = ( 1.0f - _currentPanicIntensity * 0.85f ) * MasterVolume;
            _ambientHandle.Volume = MathX.Lerp( _ambientHandle.Volume, targetAmbientVol, Time.Delta * 5f );
        }

        if ( _panicHandle.IsValid() )
        {
            float targetPanicVol = _currentPanicIntensity * MasterVolume;
            _panicHandle.Volume = MathX.Lerp( _panicHandle.Volume, targetPanicVol, Time.Delta * 5f );
        }
    }

    public void ToggleMusic( bool enable )
    {
        if ( enable )
        {
            PlayMusic();
        }
        else
        {
            StopMusic();
        }
    }

    public void PlayMusic()
    {
        StopMusic();

        // Start ambient loop
        var ambientSound = AmbientSound ?? MusicSound;
        if ( ambientSound != null )
        {
            _ambientHandle = Sound.Play( ambientSound );
            if ( _ambientHandle.IsValid() )
            {
                _ambientHandle.Volume = MasterVolume;
                ActiveMusicHandle = _ambientHandle;
            }
        }
        else
        {
            // Fallback load default ambient sound
            _ambientHandle = Sound.Play( "music/fearless pt. ii.sound" );
            if ( _ambientHandle.IsValid() )
            {
                _ambientHandle.Volume = MasterVolume;
                ActiveMusicHandle = _ambientHandle;
            }
        }

        // Start panic loop (muted until Nextbot approaches)
        if ( PanicSound != null )
        {
            _panicHandle = Sound.Play( PanicSound );
            if ( _panicHandle.IsValid() )
            {
                _panicHandle.Volume = 0f;
            }
        }
        else
        {
            // Fallback load default high-tempo panic sound
            _panicHandle = Sound.Play( "music/fire_zkyuzme_.sound" );
            if ( _panicHandle.IsValid() )
            {
                _panicHandle.Volume = 0f;
            }
        }
    }

    public void StopMusic()
    {
        if ( _ambientHandle.IsValid() )
        {
            _ambientHandle.Stop();
            _ambientHandle = default;
        }

        if ( _panicHandle.IsValid() )
        {
            _panicHandle.Stop();
            _panicHandle = default;
        }

        ActiveMusicHandle = default;
    }

    public static void StopAllMusic()
    {
        if ( ActiveMusicHandle.IsValid() )
        {
            ActiveMusicHandle.Stop();
            ActiveMusicHandle = default;
        }

        if ( Instance.IsValid() )
        {
            Instance.StopMusic();
        }
    }

    protected override void OnDestroy()
    {
        StopMusic();
        if ( Instance == this )
        {
            Instance = null;
        }
    }
}