add majority of sound definitions

This commit is contained in:
jorsi
2020-05-30 17:20:01 -04:00
parent 21267e383d
commit 71786c3671
110 changed files with 197 additions and 89 deletions
+57 -34
View File
@@ -2,6 +2,7 @@
* Module that takes care of audio playback
*/
var AudioEngine = {
canPlayAudio: false,
AUDIO_BUFFER_CACHE: {},
audioContext: null,
master: null,
@@ -9,94 +10,116 @@ var AudioEngine = {
// Tracks for playing music and sound effects
// 0 - Background music
// 1 - Background music
// 2 - Sound effects
// 2 - Event music
// 3 - Sound effects
tracks: null,
currentBackgroundChannel: 0,
currentTrack: null,
init: function(options) {
// for legacy browsers
this.audioContext = new (window.AudioContext || window.webkitAudioContext);
AudioEngine.audioContext = new (window.AudioContext || window.webkitAudioContext);
audioLog('starting audio engine');
console.log(AudioEngine.audioContext);
audioLog('state: ' + AudioEngine.audioContext.state);
if (AudioEngine.audioContext.state === 'suspended') {
AudioEngine.audioContext.resume().then(function () {
AudioEngine.createChannels();
AudioEngine.canPlayAudio = true;
});
} else {
AudioEngine.createChannels();
}
},
createChannels() {
// create master
this.master = this.audioContext.createGain();
this.master.connect(this.audioContext.destination);
AudioEngine.master = AudioEngine.audioContext.createGain();
AudioEngine.master.connect(AudioEngine.audioContext.destination);
// create 4 tracks to output to master
this.tracks = [];
AudioEngine.tracks = [];
for (var i = 0; i < 4; i++) {
this.tracks[i] = this.audioContext.createGain();
this.tracks[i].connect(this.master);
AudioEngine.tracks[i] = AudioEngine.audioContext.createGain();
AudioEngine.tracks[i].connect(AudioEngine.master);
}
},
},
options: {}, // Nothing for now
_playSound: function(buffer) {
var source = this.audioContext.createBufferSource();
if (!AudioEngine.canPlayAudio) return;
var source = AudioEngine.audioContext.createBufferSource();
source.buffer = buffer;
source.connect(this.tracks[1]);
source.start(0);
source.connect(AudioEngine.tracks[1]);
source.start(AudioEngine.audioContext.currentTime);
},
_fadeTrack: function(buffer) {
var newTrack = this.audioContext.createBufferSource();
if (!AudioEngine.canPlayAudio) return;
var newTrack = AudioEngine.audioContext.createBufferSource();
newTrack.buffer = buffer;
newTrack.loop = true;
// figure out which background track to start on
// in order to do crossfade
var nextBackgroundChannel;
if (this.currentBackgroundChannel === 0) {
if (AudioEngine.currentBackgroundChannel === 0) {
nextBackgroundChannel = 1;
} else {
nextBackgroundChannel = 0;
}
// fade in new track
var fadeTime = this.audioContext.currentTime + 2.0;
newTrack.connect(this.tracks[nextBackgroundChannel]);
var fadeTime = AudioEngine.audioContext.currentTime + 2.0;
newTrack.connect(AudioEngine.tracks[nextBackgroundChannel]);
newTrack.start(0);
this.tracks[nextBackgroundChannel].gain.setValueAtTime(0.0, this.audioContext.currentTime);
this.tracks[nextBackgroundChannel].gain.linearRampToValueAtTime(1.0, fadeTime);
AudioEngine.tracks[nextBackgroundChannel].gain.setValueAtTime(0.0, AudioEngine.audioContext.currentTime);
AudioEngine.tracks[nextBackgroundChannel].gain.linearRampToValueAtTime(.1, fadeTime);
// fade out old track
this.tracks[this.currentBackgroundChannel].gain.linearRampToValueAtTime(0.0, fadeTime);
if (this.currentTrack) {
this.currentTrack.stop(fadeTime + 0.3); // make sure fade has completed
AudioEngine.tracks[AudioEngine.currentBackgroundChannel].gain.linearRampToValueAtTime(0.0, fadeTime);
if (AudioEngine.currentTrack) {
AudioEngine.currentTrack.stop(fadeTime + 0.3); // make sure fade has completed
}
// switch background track
this.currentBackgroundChannel = nextBackgroundChannel;
this.currentTrack = newTrack;
AudioEngine.currentBackgroundChannel = nextBackgroundChannel;
AudioEngine.currentTrack = newTrack;
},
changeMusic: function(src) {
var self = this;
this.loadAudioFile(src)
AudioEngine.loadAudioFile(src)
.then(function (buffer) {
self._fadeTrack(buffer);
AudioEngine._fadeTrack(buffer);
});
audioLog('change music: ' + src);
audioLog('audio engine status: ' + AudioEngine.audioContext.state);
},
playSound: function(src) {
var self = this;
this.loadAudioFile(src)
AudioEngine.loadAudioFile(src)
.then(function (buffer) {
self._playSound(buffer);
AudioEngine._playSound(buffer);
});
audioLog('play sound: ' + src);
audioLog('audio engine status: ' + AudioEngine.audioContext.state);
},
loadAudioFile(src) {
var self = this;
if (self.AUDIO_BUFFER_CACHE[src]) {
if (AudioEngine.AUDIO_BUFFER_CACHE[src]) {
return new Promise(function (resolve, reject) {
resolve(self.AUDIO_BUFFER_CACHE[src]);
resolve(AudioEngine.AUDIO_BUFFER_CACHE[src]);
});
} else {
var request = new Request(src);
return fetch(request).then(function(response) {
return response.arrayBuffer();
}).then(function(buffer) {
return self.audioContext.decodeAudioData(buffer, function(decodedData) {
self.AUDIO_BUFFER_CACHE[src] = decodedData;
return self.AUDIO_BUFFER_CACHE[src];
return AudioEngine.audioContext.decodeAudioData(buffer, function(decodedData) {
AudioEngine.AUDIO_BUFFER_CACHE[src] = decodedData;
return AudioEngine.AUDIO_BUFFER_CACHE[src];
});
});
}
}
};
function audioLog(message) {
console.log('%c' + message, 'background: #222; color: #bada55');
}
+1 -1
View File
@@ -73,7 +73,7 @@
options: {
state: null,
debug: true,
log: true,
log: false,
dropbox: false,
doubleTime: true
},
+35 -3
View File
@@ -11,7 +11,20 @@ var Events = {
_LEAVE_COOLDOWN: 1,
STUN_DURATION: 4000,
BLINK_INTERVAL: false,
currentMusic: 0,
MUSIC: {
0: '/audio/combat.wav',
1: '/audio/event.wav'
},
SOUNDS: {
'trigger-fight': '/audio/trigger-fight.wav',
'trigger-event': '/audio/trigger-event.wav',
'enemy-hit': '/audio/enemy-hit.wav',
'eat-meat': '/audio/eat-meat.wav',
'use-meds': '/audio/use-meds.wav',
'win-fight': '/audio/win-fight.wav',
'lose-fight': '/audio/lose-fight.wav'
},
init: function(options) {
this.options = $.extend(
this.options,
@@ -135,7 +148,7 @@ var Events = {
}
$('<div>').addClass('clear').appendTo(healBtns);
Events.setHeal(healBtns);
// Set up the enemy attack timer
Events._enemyAttackTimer = Engine.setInterval(Events.enemyAttack, scene.attackDelay * 1000);
},
@@ -340,13 +353,17 @@ var Events = {
eatMeat: function(btn) {
Events.doHeal('cured meat', World.meatHeal(), btn);
AudioEngine.playSound(Events.SOUNDS['eat-meat']);
},
useMeds: function(btn) {
Events.doHeal('medicine', World.medsHeal(), btn);
AudioEngine.playSound(Events.SOUNDS['use-meds']);
},
useWeapon: function(btn) {
console.log('%cuse weapon: ', 'background: #222; color: #bada55');
console.log(btn);
if(Events.activeEvent()) {
var weaponName = btn.attr('id').substring(7).replace('-', ' ');
var weapon = World.Weapons[weaponName];
@@ -416,8 +433,9 @@ var Events = {
}
}
}
var attackFn = weapon.type == 'ranged' ? Events.animateRanged : Events.animateMelee;
AudioEngine.playSound(weapon.audio);
attackFn($('#wanderer'), dmg, function() {
if($('#enemy').data('hp') <= 0 && !Events.won) {
// Success!
@@ -443,6 +461,9 @@ var Events = {
Events.setHeal();
}
Events.updateFighterDiv(enemy);
// play hit sound
AudioEngine.playSound(Events.SOUNDS['enemy-hit']);
}
} else {
if(dmg == 'stun') {
@@ -519,6 +540,8 @@ var Events = {
clearTimeout(Events._enemyAttackTimer);
Events.endEvent();
World.die();
AudioEngine.playSound(Events.SOUNDS['lose-fight']);
}
});
}
@@ -536,6 +559,7 @@ var Events = {
return;
}
Events.endFight();
AudioEngine.playSound(Events.SOUNDS['win-fight']);
$('#enemy').animate({opacity: 0}, 300, 'linear', function() {
Engine.setTimeout(function() {
var scene = Events.activeEvent().scenes[Events.activeScene];
@@ -1018,6 +1042,9 @@ var Events = {
} else {
var r = Math.floor(Math.random()*(possibleEvents.length));
Events.startEvent(possibleEvents[r]);
console.log('event', possibleEvents[r].audio);
AudioEngine.playSound(Events.SOUNDS['trigger-event']);
AudioEngine.changeMusic(possibleEvents[r].audio);
}
}
@@ -1035,6 +1062,11 @@ var Events = {
var r = Math.floor(Math.random()*(possibleFights.length));
Events.startEvent(possibleFights[r]);
// play audio only when fight is possible
if (possibleFights.length > 0) {
AudioEngine.playSound(Events.SOUNDS['trigger-fight']);
}
},
activeEvent: function() {
+20 -10
View File
@@ -38,7 +38,8 @@ Events.Encounters = [
},
notification: _('a snarling beast leaps out of the underbrush')
}
}
},
audio: '/audio/snarling-beast.wav'
},
{ /* Gaunt Man */
title: _('A Gaunt Man'),
@@ -75,7 +76,8 @@ Events.Encounters = [
},
notification: _('a gaunt man approaches, a crazed look in his eye')
}
}
},
audio: '/audio/gaunt-man.wav'
},
{ /* Strange Bird */
title: _('A Strange Bird'),
@@ -112,7 +114,8 @@ Events.Encounters = [
},
notification: _('a strange looking bird speeds across the plains')
}
}
},
audio: '/audio/strange-bird.wav'
},
/* Tier 2*/
{ /* Shivering Man */
@@ -155,7 +158,8 @@ Events.Encounters = [
},
notification: _('a shivering man approaches and attacks with surprising strength')
}
}
},
audio: '/audio/shivering-man.wav'
},
{ /* Man-eater */
title: _('A Man-Eater'),
@@ -192,7 +196,8 @@ Events.Encounters = [
},
notification: _('a large creature attacks, claws freshly bloodied')
}
}
},
audio: '/audio/man-eater.wav'
},
{ /* Scavenger */
title: _('A Scavenger'),
@@ -234,7 +239,8 @@ Events.Encounters = [
},
notification: _('a scavenger draws close, hoping for an easy score')
}
}
},
audio: '/audio/scavenger.wav'
},
{ /* Huge Lizard */
title: _('A Huge Lizard'),
@@ -271,7 +277,8 @@ Events.Encounters = [
},
notification: _('the grass thrashes wildly as a huge lizard pushes through')
}
}
},
audio: '/audio/huge-lizard.wav'
},
/* Tier 3*/
{ /* Feral Terror */
@@ -309,7 +316,8 @@ Events.Encounters = [
},
notification: _('a beast, wilder than imagining, erupts out of the foliage')
}
}
},
audio: '/audio/feral-terror.wav'
},
{ /* Soldier */
title: _('A Soldier'),
@@ -352,7 +360,8 @@ Events.Encounters = [
},
notification: _('a soldier opens fire from across the desert')
}
}
},
audio: '/audio/soldier.wav'
},
{ /* Sniper */
title: _('A Sniper'),
@@ -395,6 +404,7 @@ Events.Encounters = [
},
notification: _('a shot rings out, from somewhere in the long grass')
}
}
},
audio: '/audio/sniper.wav'
}
];
+2 -1
View File
@@ -61,6 +61,7 @@ Events.Global = [
}
}
}
}
},
audio: '/audio/thief.wav'
}
];
+12 -6
View File
@@ -63,7 +63,8 @@ Events.Outside = [
}
}
}
}
},
audio: '/audio/ruined-trap.wav'
},
{ /* Hut fire */
title: _('Fire'),
@@ -89,7 +90,8 @@ Events.Outside = [
}
}
}
}
},
audio: '/audio/hut-fire.wav'
},
{ /* Sickness */
title: _('Sickness'),
@@ -146,7 +148,8 @@ Events.Outside = [
}
}
}
}
},
audio: '/audio/sickness.wav'
},
{ /* Plague */
@@ -217,7 +220,8 @@ Events.Outside = [
}
}
}
}
},
audio: '/audio/plague.wav'
},
{ /* Beast attack */
@@ -251,7 +255,8 @@ Events.Outside = [
}
}
}
}
},
audio: '/audio/beast-attack.wav'
},
{ /* Soldier attack */
@@ -285,7 +290,8 @@ Events.Outside = [
}
}
}
}
},
audio: '/audio/soldier-attack.wav'
}
];
+20 -10
View File
@@ -47,7 +47,8 @@ Events.Room = [
}
}
}
}
},
audio: '/audio/nomad.wav'
},
{ /* Noises Outside -- gain wood/fur */
title: _('Noises'),
@@ -98,7 +99,8 @@ Events.Room = [
}
}
}
}
},
audio: '/audio/noises-outside.wav'
},
{ /* Noises Inside -- trade wood for better good */
title: _('Noises'),
@@ -184,7 +186,8 @@ Events.Room = [
}
}
}
}
},
audio: '/audio/noises-inside.wav'
},
{ /* The Beggar -- trade fur for better good */
title: _('The Beggar'),
@@ -255,7 +258,8 @@ Events.Room = [
}
}
}
}
},
audio: '/audio/beggar.wav'
},
{/* The Shady Builder */
title: _('The Shady Builder'),
@@ -311,7 +315,8 @@ Events.Room = [
}
}
}
}
},
audio: '/audio/shady-builder.wav'
},
{ /* Mysterious Wanderer -- wood gambling */
@@ -390,7 +395,8 @@ Events.Room = [
}
}
}
}
},
audio: '/audio/mysterious-wanderer.wav'
},
{ /* Mysterious Wanderer -- fur gambling */
@@ -469,7 +475,8 @@ Events.Room = [
}
}
}
}
},
audio: '/audio/mysterious-wanderer.wav'
},
{ /* The Scout -- Map Merchant */
@@ -511,7 +518,8 @@ Events.Room = [
}
}
}
}
},
audio: '/audio/scout.wav'
},
{ /* The Wandering Master */
@@ -584,7 +592,8 @@ Events.Room = [
}
}
}
}
},
audio: '/audio/wandering-master.wav'
},
{ /* The Sick Man */
@@ -672,6 +681,7 @@ Events.Room = [
}
}
}
}
},
audio: '/audio/sick-man.wav'
}
];
+26 -13
View File
@@ -28,7 +28,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/friendly-outpost.wav'
},
"swamp": { /* Swamp */
title: _('A Murky Swamp'),
@@ -85,7 +86,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/swamp.wav'
},
"cave": { /* Cave */
title: _('A Damp Cave'),
@@ -516,7 +518,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/cave.wav'
},
"town": { /* Town */
title: _('A Deserted Town'),
@@ -1233,7 +1236,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/town.wav'
},
"city": { /* City */
title: _('A Ruined City'),
@@ -2928,7 +2932,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/city.wav'
},
"house": { /* Abandoned House */
title: _('An Old House'),
@@ -3045,7 +3050,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/house.wav'
},
"battlefield": { /* Discovering an old battlefield */
title: _('A Forgotten Battlefield'),
@@ -3098,7 +3104,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/battlefield.wav'
},
"borehole": { /* Admiring a huge borehole */
title: _('A Huge Borehole'),
@@ -3127,7 +3134,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/borehole.wav'
},
"ship": { /* Finding a way off this rock */
title: _('A Crashed Ship'),
@@ -3150,7 +3158,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/crashed-ship.wav'
},
"sulphurmine": { /* Clearing the Sulphur Mine */
title: _('The Sulphur Mine'),
@@ -3299,7 +3308,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/sulphurmine.wav'
},
"coalmine": { /* Clearing the Coal Mine */
title: _('The Coal Mine'),
@@ -3441,7 +3451,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/coalmine.wav'
},
"ironmine": { /* Clearing the Iron Mine */
title: _('The Iron Mine'),
@@ -3517,7 +3528,8 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/ironmine.wav'
},
"cache": { /* Cache - contains some of supplies from previous game */
@@ -3569,6 +3581,7 @@ Events.Setpieces = {
}
}
}
}
},
audio: '/audio/destroyed-village.wav'
}
};
+6 -2
View File
@@ -28,12 +28,13 @@ var Room = {
'build-smokehouse': '/audio/build-smokehouse.wav',
'build-workshop': '/audio/build-workshop.wav',
'build-steelworks': '/audio/build-steelworks.wav',
'build-armoury': '/audio/build-armoury.wav',
'craft-torch': '/audio/craft-torch.wav',
'craft-waterskin': '/audio/craft-waterskin.wav',
'craft-bone-spear': '/audio/craft-bone-spear.wav',
'craft-rucksack': '/audio/craft-rucksack.wav',
'craft-leather-armour': '/audio/craft-leather-armour.wav',
'craft-cask': '/audio/craft-cask.wav',
'craft-leather-armour': '/audio/craft-leather-armour.wav',
'craft-wagon': '/audio/craft-wagon.wav',
'craft-iron-armour': '/audio/craft-iron-armour.wav',
'craft-iron-sword': '/audio/craft-iron-sword.wav',
@@ -41,13 +42,16 @@ var Room = {
'craft-convoy': '/audio/craft-convoy.wav',
'craft-steel-armour': '/audio/craft-steel-armour.wav',
'craft-steel-sword': '/audio/craft-steel-sword.wav',
'craft-rifle': '/audio/craft-rifle.wav',
'buy-scales': '/audio/buy-scales.wav',
'buy-teeth': '/audio/buy-teeth.wav',
'buy-compass': '/audio/buy-compass.wav',
'buy-iron': '/audio/buy-iron.wav',
'buy-steel': '/audio/buy-steel.wav',
'buy-bolas': '/audio/buy-bolas.wav',
'buy-alien-alloy': '/audio/buy-alient-alloy.wav',
'buy-bullets': '/audio/buy-bullets.wav',
'buy-bayonet': '/audio/buy-bayonet.wav',
'buy-alien-alloy': '/audio/buy-alien-alloy.wav',
'buy-energy-cell': '/audio/buy-energy-cell.wav',
'buy-grenade': '/audio/buy-grenade.wav',
'buy-coal': '/audio/buy-coal.wav',
+18 -9
View File
@@ -55,59 +55,68 @@ var World = {
verb: _('punch'),
type: 'unarmed',
damage: 1,
cooldown: 2
cooldown: 2,
audio: '/audio/punch.wav'
},
'bone spear': {
verb: _('stab'),
type: 'melee',
damage: 2,
cooldown: 2
cooldown: 2,
audio: '/audio/stab.wav'
},
'iron sword': {
verb: _('swing'),
type: 'melee',
damage: 4,
cooldown: 2
cooldown: 2,
audio: '/audio/swing.wav'
},
'steel sword': {
verb: _('slash'),
type: 'melee',
damage: 6,
cooldown: 2
cooldown: 2,
audio: '/audio/slash.wav'
},
'bayonet': {
verb: _('thrust'),
type: 'melee',
damage: 8,
cooldown: 2
cooldown: 2,
audio: '/audio/thrust.wav'
},
'rifle': {
verb: _('shoot'),
type: 'ranged',
damage: 5,
cooldown: 1,
cost: { 'bullets': 1 }
cost: { 'bullets': 1 },
audio: '/audio/shoot.wav'
},
'laser rifle': {
verb: _('blast'),
type: 'ranged',
damage: 8,
cooldown: 1,
cost: { 'energy cell': 1 }
cost: { 'energy cell': 1 },
audio: '/audio/blast.wav'
},
'grenade': {
verb: _('lob'),
type: 'ranged',
damage: 15,
cooldown: 5,
cost: { 'grenade': 1 }
cost: { 'grenade': 1 },
audio: '/audio/lob.wav'
},
'bolas': {
verb: _('tangle'),
type: 'ranged',
damage: 'stun',
cooldown: 15,
cost: { 'bolas': 1 }
cost: { 'bolas': 1 },
audio: '/audio/tangle.wav'
}
},