Game Development Reference
In-Depth Information
53
def
pause
(
id
=
:default
)
54
@instances
[
"
#{
id
}
_l"
].
pause
55
@instances
[
"
#{
id
}
_r"
].
pause
56
end
57
58
def
resume
(
id
=
:default
)
59
@instances
[
"
#{
id
}
_l"
].
resume
60
@instances
[
"
#{
id
}
_r"
].
resume
61
end
62
63
def
stop
64
@instances
.
delete(
"
#{
id
}
_l"
)
.
stop
65
@instances
.
delete(
"
#{
id
}
_r"
)
.
stop
66
end
67
68
def
volume_and_pan
(
id
, volume, pan)
69
if
pan
> 0
70
pan_l
= 1 -
pan
* 2
71
pan_r
= 1
72
else
73
pan_l
= 1
74
pan_r
= 1 +
pan
* 2
75
end
76
pan_l
*=
volume
77
pan_r
*=
volume
78
@instances
[
"
#{
id
}
_l"
].
volume
= [
pan_l,
0.05].
max
79
@instances
[
"
#{
id
}
_r"
].
volume
= [
pan_r,
0.05].
max
80
end
81
end
StereoSample
manages stereo playback of sample instances, and to avoid memory
leaks, it has
cleanup
that scans all sample instances and removes samples that have
finished
playing.
For
this
removal
to
work,
we
need
to
place
a
call
to
StereoSample.cleanup
inside
PlayState#update
method.
To determine correct pan and volume, we will create some helper methods in
Utils
module:
module
Utils
HEARING_DISTANCE
= 1000.0
# ...
def
self
.
volume
(object, camera)
return
1
if
object
==
camera
.
target
distance
=
Utils
.
distance_between(