anxiety/scripts/game/Sounds.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-04-23 19:12:41 +00:00
Loader.addSounds([
{ id:"voice_hong", src:"sounds/voices/hong.mp3" },
{ id:"voice_beebee", src:"sounds/voices/beebee.mp3" },
{ id:"voice_narrator", src:"sounds/voices/narrator.mp3" },
2019-05-03 16:13:22 +00:00
{ id:"voice_narrator_emphasis", src:"sounds/voices/narrator_emphasis.mp3" },
{ id:"voice_typewriter", src:"sounds/voices/typewriter.mp3" },
2019-06-19 14:49:05 +00:00
{ id:"voice_hunter", src:"sounds/voices/hunter.mp3" },
{ id:"voice_al", src:"sounds/voices/al.mp3" },
{ id:"voice_shire", src:"sounds/voices/shire.mp3" }
2019-04-23 19:12:41 +00:00
]);
2019-04-24 19:42:13 +00:00
window.sfx = function(sound, options){
2019-04-23 19:12:41 +00:00
2019-04-24 19:42:13 +00:00
options = options || {};
options.volume = options.volume===undefined ? 1 : options.volume;
options.pan = options.pan===undefined ? 0 : options.pan;
2019-04-23 19:12:41 +00:00
var sfx = Library.sounds[sound];
2019-04-24 19:42:13 +00:00
sfx.volume(options.volume);
sfx.stereo(options.pan);
2019-04-23 19:12:41 +00:00
sfx.play();
};
2019-04-26 17:08:31 +00:00
window.stopAllSounds = function(){
Object.keys(Library.sounds).forEach(function(name){
Library.sounds[name].stop();
});
};
2019-05-03 16:13:22 +00:00
window._lastPlayedVoice = {};
2019-04-24 19:42:13 +00:00
window.voice = function(name, options){
2019-05-03 16:13:22 +00:00
// How long since voice last played?
name = "voice_"+name;
window._lastPlayedVoice[name] = window._lastPlayedVoice[name] || 0;
var now = (new Date()).getTime();
var delta = now - window._lastPlayedVoice[name];
// If too soon, DON'T PLAY.
2019-09-17 14:24:40 +00:00
if(delta < 8/60*1000) return; // 8 frames
2019-05-03 16:13:22 +00:00
// Otherwise, play
2019-04-24 19:42:13 +00:00
options = options || {};
2019-05-03 16:13:22 +00:00
sfx(name, options);
window._lastPlayedVoice[name] = now;
2019-04-23 19:12:41 +00:00
}
window.CURRENT_MUSIC = null;
2019-09-09 16:37:58 +00:00
/*subscribe("GAME_PAUSED",function(){
if(window.CURRENT_MUSIC && window.CURRENT_MUSIC.playing()) window.CURRENT_MUSIC.pause();
});
subscribe("GAME_UNPAUSED",function(){
if(window.CURRENT_MUSIC && !window.CURRENT_MUSIC.playing()) window.CURRENT_MUSIC.play();
});*/
2019-07-08 18:37:46 +00:00
window.music = function(song, options, onend){
2019-04-23 19:12:41 +00:00
2019-04-24 19:42:13 +00:00
options = options || {};
options.volume = options.volume===undefined ? 1 : options.volume;
options.fade = options.fade===undefined ? 0 : options.fade;
2019-04-23 19:12:41 +00:00
2019-04-24 19:42:13 +00:00
// Fade out or cut previous?
if(options.fade==0){
if(window.CURRENT_MUSIC) window.CURRENT_MUSIC.stop();
2019-05-05 19:56:00 +00:00
}else if(window.CURRENT_MUSIC){
2019-04-24 19:42:13 +00:00
var currentVolume = window.CURRENT_MUSIC.volume();
2019-04-25 18:07:41 +00:00
window.CURRENT_MUSIC.fade(currentVolume, 0, options.fade*1000);
2019-04-24 19:42:13 +00:00
}
2019-04-23 19:12:41 +00:00
2019-04-24 19:42:13 +00:00
// Play new song (if any)
if(!song){
window.CURRENT_MUSIC = null;
2019-04-23 19:12:41 +00:00
}else{
var song = Library.sounds["music_"+song];
2019-04-29 19:19:44 +00:00
song.stop(); // just in case it was playing earlier
2019-06-26 15:03:27 +00:00
if(options.NO_LOOP){
song.play();
}else{
song.loop(true);
}
2019-04-24 19:42:13 +00:00
if(options.fade==0){
song.volume(options.volume);
}else{
song.fade(0, options.volume, options.fade);
}
2019-04-23 19:12:41 +00:00
window.CURRENT_MUSIC = song;
2019-04-24 19:42:13 +00:00
song.play();
2019-04-23 19:12:41 +00:00
}
2019-07-08 18:37:46 +00:00
// IF AN ON-END (used basically once in this whole game)
if(onend){
song.on('end', onend);
}
2019-04-23 19:12:41 +00:00
};