Dynamic Sound Emissions
Version: 1.0
Author: modern algebra
Date: August 27, 2007
Version History
- Version 1.0: Original Script
Planned Future Versions
- Version 1.5: Allow for in-game setting of a new sound emission object in a map
- Version 2.0: Allow to have more than one sound emission object per map
Description
This script allows you to set an object in the map which emits a sound. What
this script does is set the volume of that sound according to how close you
are to the object. If you are far away, the sound will be faint, if you are
close the sound will be loud. Sorry, in this version there can be only one
object per map.
Features
- Plays a BGS in increasing volume as you approach the object making the sound
- Allows you to specifically set a radius for the sound (how many squares away you can hear the sound)
- You can set up one of these objects for every map
- Full customization from how big the object is to where it is located to the bgs that is played and the pitch
Instructions
Place this into it's own script above man. Find further instructions on setting up objects inside the editable region of the script
Script
#==============================================================================
# Dynamic Sound Emissions
# Version: 1.0
# Author: Modern Algebra
# Date: August 27, 2007
#------------------------------------------------------------------------------
# Description:
# This script allows you to set an object in the map which emits a sound. What
# this script does is set the volume of that sound according to how close you
# are to the object. If you are far away, the sound will be faint, if you are
# close the sound will be loud. Sorry, in this version there can be only one
# object per map.
#------------------------------------------------------------------------------
# Instructions:
# To set the objects which emit sound for each map, see below.
#------------------------------------------------------------------------------
# ** Data_Sound_Emission
# This class holds all of the objects which emit any sound, for all maps
#==============================================================================
class Data_Sound_Emission
#-------------------------------------------------------------------------
# * Public Instance Variables
#-------------------------------------------------------------------------
attr_reader :emissions
#-------------------------------------------------------------------------
# * Object Initialization
#-------------------------------------------------------------------------
def initialize
@emissions = []
#----------------------------------------------------------------------
# * Editable Region
#----------------------------------------------------------------------
# Place all sound emitting objects here. Write it like this:
#
# @emissions[map_id] = Sound_Emission.new
# @emissions[map_id].setup (x,y,width,height,radius,bgs,pitch)
#
# Where map_id is the ID of the map you want the object to appear on,
# x and y are the x and y positions of the upper-left corner of the object,
# width is the amount of squares in the x direction that the object covers,
# height is the amount of squares in the y direction that the object
# covers, radius is the number of squares away you want the sound to be
# hears, bgs is the bgs you want to play, and pitch is the pitch value
#-----------------------------------------------------------------------
@emissions[1] = Sound_Emission.new
@emissions[1].setup (5,5,1,4,20,'011-Waterfall01',100)
#-----------------------------------------------------------------------
# * END Editable Region
#-----------------------------------------------------------------------
end
end
#==============================================================================
# ** Sound Emitting Object
#------------------------------------------------------------------------------
# This class holds all the relevant data for the object which is emitting the
# sound. It is accessed through $game_dynamic_sound_emission
#==============================================================================
class Sound_Emission
#-------------------------------------------------------------------------
# * Public Instance Variables
#-------------------------------------------------------------------------
attr_reader :bgs
attr_reader :radius
attr_reader :pitch
#-------------------------------------------------------------------------
# * Object Initialization
#-------------------------------------------------------------------------
def setup (x, y, width, height, radius, bgs, pitch)
@rect = Rect.new (x*128, y*128, (width-1)*128, (height-1)*128)
@radius = radius*128
@bgs = bgs
@pitch = pitch
end
#-------------------------------------------------------------------------
# * Volume
# Checks position to determine the appropriate volume.
#-------------------------------------------------------------------------
def volume (x, y)
# Determine distance between position and the sound emitting object
distance = [x - @rect.x, y - @rect.y]
testers = [@rect.width, @rect.height]
for i in 0...2 # For both the x distance and the y distance
# Determine total distance in that direction from the object
if distance[i] > 0 # IF you are to the right of the object
# Take into account the width of the object
if distance[i] <= testers[i] # If you are above or beside the object
# Neglect the distance in that direction
distance[i] = 0
else
# Subtract the width to determine the closest row or column
distance[i] -= testers[i]
end
else
# Make the value positive
distance[i] *= -1
end
end
# Calculate the total distance
total_distance = distance[0] + distance[1]
ratio = (total_distance.to_f / @radius.to_f)*100 # Get the percentage
ratio = [ratio,100].min
ratio = 100 - ratio
return ratio
end
end
#==============================================================================
# ** Scene Title
#==============================================================================
class Scene_Title
#-------------------------------------------------------------------------
# * Alias Main to initialize the data class upon startup
#-------------------------------------------------------------------------
alias add_data_emissions main
def main
# Initialize $data_sound_objects
$data_sound_objects = Data_Sound_Emission.new
add_data_emissions
end
end
#==============================================================================
# ** Game Player
#==============================================================================
class Game_Player
#-------------------------------------------------------------------------
# * Alias update to play the BGS
#-------------------------------------------------------------------------
alias update_volume update
def update
update_volume
if $data_sound_objects.emissions[$game_map.map_id] != nil
emitter = $data_sound_objects.emissions[$game_map.map_id]
volume = emitter.volume (@real_x, @real_y)
bgs = RPG::AudioFile.new (emitter.bgs,volume,emitter.pitch)
if volume != 0
$game_system.bgs_play (bgs)
else
Audio.bgs_stop
end
end
end
end
Credit
Support
Will provide bug fixes and, if anyone can think of easy improvements to the script that I find beneficial, I will edit. I will also provide compatibility support
Known Compatibility Issues
There shouldn't be any, as nothing is overwritten. Maybe if another script uses the global variable $data_sound_objects.
Demo
See Attached.
Author's Notes
Very similar struccture to my Encounter Restrictions Script, and since someone requested one a long time ago (I made them an evented version), I figured I'd do it. I haven't been very productive lately, after all, and I wanted to make something new. Hopefully, one day I'll figure out how to play two bgses at the same time, I think I will have to make a manual play method though