じ☆ve冰风 发表于 2026-8-3 07:49:23

[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:



Eg



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:
#
#
#Eg.
#
#
#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.match(/\/) }.compact

      sound_matches.each do |sound_match|
      bgs_name = sound_match
      max_volume = sound_match.to_i
      min_volume = sound_match.to_i
      range = sound_match.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.match(/\/) }.compact.first
      bgs_name = sound_match
      max_volume = sound_match.to_i
      min_volume = sound_match.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




本贴来自国际rpgmaker官方论坛作者:CloudTheWolf处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/cloudthewolf-event-sounds.158451/
页: [1]
查看完整版本: [CloudTheWolf] Event Sounds