[CloudTheWolf] Event Sounds

Something I recently wanted to add to my project was Event sounds that get louder the closer you were to them.

I found ways to do this in Event functions using regions, however this felt clunky and a pain to implement, so I turned to scripts. I couldn't seem to find any that would work so ended up having to make my own.

After a lot of trial and error I managed to get it working with as much functionality as I could and thought other might want to have a similar system in their games.

All it needs is your event to have the following comment block:

[sound "SOUND_NAME" max_volume min_volume range]

Eg

[sound "Fire" 80 30 8]

Alternative Links:
Official Website: https://cloudthewolf.com/view-file/ctw-event-sounds-rb
GitHub: https://github.com/CloudTheWolf/rpg-maker-vx-ace-scripts/blob/master/audio/Clouds Event Sounds.rb
Ruby:
#===============================================================================
#
# CloudTheWolf Event Sounds
# Author: CloudTheWolf
# Date (18/06/2023)
# Version: (1.0.1) (VXA)
# Level: (Easy)
#
#
#===============================================================================
# Change Log:
#   - 1.0.0
#     Initial Release
#
#   - 1.0.1
#     Fix tranitioning from a map with an even sound to a map with a BGS set
#===============================================================================
#
# NOTES: 1) This script will only work with ace.
#        2) This will only work with the closest event.
#        3) This will not work if the map has a BGS Set
#
#===============================================================================
#
# Description: Adds a BGS Sound Source to an event
#
# Credits: Me (CloudTheWolf)
#
#===============================================================================
#
# Instructions
# Paste above ▼ Below and above ▼ Main Process.
#
# Add the following comment to your event:
# [sound "SOUND_NAME" max_volume min_volume range]
#
#  Eg.
#  [sound "Fire" 50 2 7]
#
#  This will play to firew BGS when the player is within the range.
#  Outside of the range it will play at the min_volume so set to 0 if you don't
#  want it on the while map
#
#  FAQ:
#
#  Q: Can I have more than 1 sound source on a map?
#  A: Yes, however, please not only 1 can play at a time.
#     This is based on which ever is closest to the player.
#  Q: The BGS Still plays even when leaving the map
#  A: In your Map Settings just enable "Change BGS" and set it to none.
#
#  Q: Does this work with X, Y or Z?
#  A: This has not been testing with other scripts so this may or may not work
#     
#===============================================================================
#
# Free for any use as long as I'm credited.
#
#===============================================================================


#==============================================================================
# ** Game_Map
#------------------------------------------------------------------------------
#  This class handles maps. It includes scrolling and passage determination
# functions. The instance of this class is referenced by $game_map.
#==============================================================================

class Game_Map
 
  attr_accessor :custom_bgs_playing
 
  # Calculate the straight-line distance between two points
  def distance(x1, y1, x2, y2)
    Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
  end

# Update BGS settings for events on the map
  def update_event_sounds
    closest_event = nil
    closest_distance = Float::INFINITY
    closest_range = nil
    custom_bgs_name = nil

    events.each_value do |event|
      comment = event.list.select { |command| command.code == 108 || command.code == 408 }
      next unless comment

      sound_matches = comment.map { |command| command.parameters[0].match(/\[sound\s+"(.+)"\s+(\d+)\s+(\d+)\s+(\d+)\]/) }.compact

      sound_matches.each do |sound_match|
        bgs_name = sound_match[1]
        max_volume = sound_match[2].to_i
        min_volume = sound_match[3].to_i
        range = sound_match[4].to_i

        player = $game_player
        distance = distance(event.x, event.y, player.x, player.y)           
        
        if closest_event == nil || (distance <= range && distance < closest_distance)
          closest_event = event
          closest_distance = distance
          closest_range = range
          custom_bgs_name = bgs_name
        end
      end
    end

    if closest_event && custom_bgs_name 
        comment = closest_event.list.select { |command| command.code == 108 || command.code == 408 }
        sound_match = comment.map { |command| command.parameters[0].match(/\[sound\s+"(.+)"\s+(\d+)\s+(\d+)\s+(\d+)\]/) }.compact.first
        bgs_name = sound_match[1]
        max_volume = sound_match[2].to_i
        min_volume = sound_match[3].to_i     
        if closest_distance <= closest_range
          current_volume = $temp_bgs_volume || 0
          target_volume = max_volume - ((max_volume - min_volume) * closest_distance / closest_range).to_i

          if current_volume != target_volume || $temp_bgs_name != bgs_name
            Audio.bgs_stop if current_volume == 0 && $temp_bgs_name != bgs_name
            Audio.bgs_play("Audio/BGS/#{bgs_name}", target_volume, 100, 0)
            $temp_bgs_name = bgs_name
          end

          $temp_bgs_volume = target_volume
        else
          Audio.bgs_play("Audio/BGS/#{bgs_name}", min_volume, 100, 0) unless min_volume == 0
          self.custom_bgs_playing = bgs_name
        end
      else
        self.custom_bgs_playing = nil
    end
  end
 
  alias ces_setup setup
  def setup(map_id)
    # Stop only the custom BGS before changing maps
    if self.custom_bgs_playing
      Audio.bgs_stop
      self.custom_bgs_playing = nil
    end
    ces_setup(map_id)
  end
end

#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs the map screen processing.
#==============================================================================

class Scene_Map < Scene_Base
  # ...
 
  # Update scene (called every frame)
  alias ces_update update
  def update
    ces_update
    update_dynamic_sounds
    # ... Other update logic for the scene
  end
 
  # Update dynamic sound effects
  def update_dynamic_sounds
    $game_map.update_event_sounds
  end
 
  # ...
end

Original thread: https://forums.rpgmakerweb.com/threads/cloudthewolf-event-sounds.158451/