Game Development Reference
In-Depth Information
camera . target . x, camera . target . y,
object . x, object . y)
distance = [ ( HEARING_DISTANCE - distance), 0]. max
distance / HEARING_DISTANCE
end
def self . pan (object, camera)
return 0 if object == camera . target
pan = object . x - camera . target . x
sig = pan > 0 ? 1 : -1
pan = (pan % HEARING_DISTANCE ) / HEARING_DISTANCE
if sig > 0
pan
else
-1 + pan
end
end
def self . volume_and_pan (object, camera)
[ volume(object, camera), pan(object, camera) ]
end
end
Apparently, having access to Camera is necessary for calculating sound volume and pan,
so we will add attr_accessor :camera to ObjectPool class and assign it in
PlayState constructor. You may wonder why we didn't use Camera#target right
away. The answer is that camera can change it's target. E.g. when your tank dies, new
instance will be generated when you respawn, so if all other objects would still have the
reference to your old tank, guess what you would hear?
Remastered TankSounds component is probably the most elaborate example of how
StereoSample should be used:
09-polishing/entities/components/tank_sounds.rb
1 class TankSounds < Component
2 def initialize (object, object_pool)
3 super (object)
4 @object_pool = object_pool
5 end
6
7 def update
8 id = object . object_id
9 if object . physics . moving?
10
move_volume = Utils . volume(
Search WWH ::




Custom Search