Skip to content

Commit

Permalink
fix space music fading in when ending game by adding separate backgro…
Browse files Browse the repository at this point in the history
…und volume function
  • Loading branch information
orsi committed Jun 3, 2020
1 parent 44a7552 commit 783886b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
20 changes: 19 additions & 1 deletion script/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,25 @@ var AudioEngine = {
});
}
},
setVolume: function (volume, s) {
setBackgroundMusicVolume: function (volume, s) {
if (AudioEngine._master == null) return; // master may not be ready yet
if (volume === undefined) {
volume = 1.0;
}
if (s === undefined) {
s = 1.0;
}

// cancel any current schedules and then ramp
var currentBackgroundGainValue = AudioEngine._currentBackgroundMusic.envelope.gain.value;
AudioEngine._currentBackgroundMusic.envelope.gain.cancelScheduledValues(AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.setValueAtTime(currentBackgroundGainValue, AudioEngine._audioContext.currentTime);
AudioEngine._currentBackgroundMusic.envelope.gain.linearRampToValueAtTime(
volume,
AudioEngine._audioContext.currentTime + s
);
},
setMasterVolume: function (volume, s) {
if (AudioEngine._master == null) return; // master may not be ready yet
if (volume === undefined) {
volume = 1.0;
Expand Down
8 changes: 5 additions & 3 deletions script/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ var Space = {
}
delete Outside._popTimeout;

AudioEngine.setVolume(1.0);
AudioEngine.playBackgroundMusic(AudioLibrary.MUSIC_ENDING);

$('#hullRemaining', Space.panel).animate({opacity: 0}, 500, 'linear');
Space.ship.animate({
top: '350px',
Expand Down Expand Up @@ -558,8 +558,10 @@ var Space = {

lowerVolume: function () {
if (Space.done) return;

// lower audio as ship gets further into space
var newVolume = 1.0 - (Space.altitude / 60);
AudioEngine.setVolume(newVolume, .3);
var progress = Space.altitude / 60;
var newVolume = 1.0 - progress;
AudioEngine.setBackgroundMusicVolume(newVolume, .3);
}
};

0 comments on commit 783886b

Please sign in to comment.