From b6aa19910b9955e5b794c3fd0a7e4d5ed56f83fc Mon Sep 17 00:00:00 2001 From: Christian Kvalheim Date: Wed, 14 Mar 2012 23:37:28 +0100 Subject: [PATCH] Changed to using SoundJS for music instead of akhibara, speedup --- index.ejs | 1 + public/game.js | 47 ++++++++++++++++++++++++++++++---------- public/playerghost.js | 2 +- public/playermongoman.js | 6 ++--- public/remoteplayer.js | 10 ++++----- public/sound/sound.js | 43 ++++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 public/sound/sound.js diff --git a/index.ejs b/index.ejs index 88640d9..35770d7 100644 --- a/index.ejs +++ b/index.ejs @@ -14,6 +14,7 @@ + diff --git a/public/game.js b/public/game.js index 70c61cd..e5193fd 100644 --- a/public/game.js +++ b/public/game.js @@ -48,19 +48,39 @@ gbox.onLoad(function () { // Now let's load some audio samples... var audioserver = "resources/audio/"; - gbox.addAudio("eat", [audioserver+"eat.mp3",audioserver+"eat.ogg"],{channel:"sfx"}); gbox.addAudio("eatghost", [audioserver+"laser.mp3",audioserver+"laser.ogg"],{channel:"sfx"}); - gbox.addAudio("powerpill", [audioserver+"powerup3.mp3",audioserver+"powerup3.ogg"],{channel:"sfx"}); - gbox.addAudio("die", [audioserver+"die.mp3",audioserver+"die.ogg"],{channel:"sfx"}); - gbox.addAudio("bonus", [audioserver+"coin.mp3",audioserver+"coin.ogg"],{channel:"sfx"}); - gbox.addAudio("default-menu-option", [audioserver+"select.mp3",audioserver+"select.ogg"],{channel:"sfx"}); - gbox.addAudio("default-menu-confirm", [audioserver+"start.mp3",audioserver+"start.ogg"],{channel:"sfx"}); - gbox.addAudio("ingame", [audioserver+"capman-ingame.mp3",audioserver+"capman-ingame.ogg"],{channel:"bgmusic",loop:true}); - // The loadAll function loads all the resources and triggers the main game loop - gbox.loadAll(go); - // Start the game - gbox.go(); + // determine browser + var filetype; + var agent = navigator.userAgent.toLowerCase(); + // adjust for browser + if(agent.indexOf("chrome") > -1){ + filetype = ".mp3"; + } else if(agent.indexOf("opera") > -1) { + filetype = ".ogg"; + } else if(agent.indexOf("firefox") > -1) { + filetype = ".ogg"; + } else if(agent.indexOf("safari") > -1) { + filetype = ".mp3"; + } else if(agent.indexOf("msie") > -1) { + filetype = ".mp3"; + } + + // Load all the audio using SoundJS + SoundJS.onLoadQueueComplete = function() { + // The loadAll function loads all the resources and triggers the main game loop + gbox.loadAll(go); + // Start the game + gbox.go(); + }; + + // Batch the audio + SoundJS.addBatch([ + {name:"eat", src:(audioserver + "eat" + filetype), instances:20}, + {name:"eatghost", src:(audioserver + "laser" + filetype), instances:1}, + {name:"powerpill", src:(audioserver + "powerup3" + filetype), instances:1}, + {name:"die", src:(audioserver + "die" + filetype), instances:1}, + {name:"music", src:(audioserver + "capman-ingame" + filetype), instances:1}]); }, false); // Start game function @@ -72,6 +92,8 @@ var go = function() { // Set method called each time we change the level maingame.changeLevel = function(level) { + // start the music + SoundJS.play("music", null, 0.2, true); // Reset local score maingame.hud.setWidget("score",{widget:"label",font:"small",value:0, dx:240, dy:25, clear:true}); // Let's add the player @@ -187,7 +209,8 @@ var go = function() { client.dispatchCommand({type:'initialize'}); }); } else if(message['state'] == 'ghostdead') { - if(sound) gbox.hitAudio("eatghost"); + // if(sound) gbox.hitAudio("eatghost"); + if(sound) SoundJS.play('eatghost'); // Check if it's a remote ghost and if it is kill it if(currentGhosts[message['id']] != null) { currentGhosts[message['id']].kill(); diff --git a/public/playerghost.js b/public/playerghost.js index 9dc4db8..b26b04a 100644 --- a/public/playerghost.js +++ b/public/playerghost.js @@ -184,7 +184,7 @@ var createPlayerGhost = function() { // And now, a custom method. This one will kill the player and will be called by ghosts, when colliding with capman. kill:function() { - if(sound)gbox.hitAudio("die"); + if(sound) SoundJS.play("die"); // Set status to eaten this.status = 'eaten'; // Change tileset diff --git a/public/playermongoman.js b/public/playermongoman.js index 4988611..ec08793 100644 --- a/public/playermongoman.js +++ b/public/playermongoman.js @@ -88,14 +88,14 @@ var createPlayerMongoman = function() { if(inmouth>7) { if(inmouth == 9) { this.scorecombo = 1; - if(sound) gbox.hitAudio("powerpill"); + if(sound) SoundJS.play("powerpill"); if(gbox.getObject("ghosts","ghost1")) gbox.getObject("ghosts","ghost1").makeeatable(); if(gbox.getObject("ghosts","ghost2")) gbox.getObject("ghosts","ghost2").makeeatable(); if(gbox.getObject("ghosts","ghost3")) gbox.getObject("ghosts","ghost3").makeeatable(); if(gbox.getObject("ghosts","ghost4")) gbox.getObject("ghosts","ghost4").makeeatable(); if(gbox.getObject("player", "playerghost")) gbox.getObject("player","playerghost").makeeatable(); } else { - if(sound) gbox.hitAudio("eat"); + if(sound) SoundJS.play("eat"); maingame.hud.addValue("score","value",10); } @@ -120,7 +120,7 @@ var createPlayerMongoman = function() { client.dispatchCommand({type:'dead', score:maingame.hud.getNumberValue("score","value")}); // Animate death this.killed = true; - if(sound) gbox.hitAudio("die"); + if(sound) SoundJS.play("die"); maingame.playerDied({wait:50}); toys.generate.sparks.simple(this,"sparks",null,{tileset:this.tileset,frames:{speed:4,frames:[6,5,7,8,9,9,9,9]}}); } diff --git a/public/remoteplayer.js b/public/remoteplayer.js index 25dfe93..8393d40 100644 --- a/public/remoteplayer.js +++ b/public/remoteplayer.js @@ -140,7 +140,7 @@ var createRemoteGhostPlayer = function(data) { // Up the combo mongoman.scorecombo++; // Signal eaten a ghost - if(sound) gbox.hitAudio("eatghost"); + if(sound) SoundJS.play("eatghost"); // Fire off I'm dead message client.dispatchCommand({type:'ghostdead', id:this.conId}); // gbox.hitAudio("eatghost"); @@ -165,7 +165,7 @@ var createRemoteGhostPlayer = function(data) { // And now, a custom method. This one will kill the player and will be called by ghosts, when colliding with capman. kill:function() { - if(sound)gbox.hitAudio("die"); + if(sound) SoundJS.play("die"); // Set status to eaten this.status = 'eaten'; // Change tileset @@ -251,14 +251,14 @@ var createRemoteMongoManPlayer = function(data) { // Handle pills if(inmouth>7) { if(inmouth == 9) { - if(sound) gbox.hitAudio("powerpill"); + if(sound) SoundJS.play("powerpill"); if(gbox.getObject("ghosts","ghost1")) gbox.getObject("ghosts","ghost1").makeeatable(); if(gbox.getObject("ghosts","ghost2")) gbox.getObject("ghosts","ghost2").makeeatable(); if(gbox.getObject("ghosts","ghost3")) gbox.getObject("ghosts","ghost3").makeeatable(); if(gbox.getObject("ghosts","ghost4")) gbox.getObject("ghosts","ghost4").makeeatable(); if(gbox.getObject("player", "playerghost")) gbox.getObject("player","playerghost").makeeatable(); } else { - if(sound) gbox.hitAudio("eat"); + if(sound) SoundJS.play("eat"); } var mouthx = help.xPixelToTileX(maze,this.x + this.hw); @@ -279,7 +279,7 @@ var createRemoteMongoManPlayer = function(data) { kill:function() { if(!this.killed) { this.killed = true; - if(sound) gbox.hitAudio("die"); + if(sound) SoundJS.play("die"); maingame.playerDied({wait:50}); toys.generate.sparks.simple(this,"sparks",null,{tileset:this.tileset,frames:{speed:4,frames:[6,5,7,8,9,9,9,9]}}); } diff --git a/public/sound/sound.js b/public/sound/sound.js new file mode 100644 index 0000000..9a06eb9 --- /dev/null +++ b/public/sound/sound.js @@ -0,0 +1,43 @@ +/** +* EaselJS +* Visit http://easeljs.com/ for documentation, updates and examples. +* +* Copyright (c) 2011 Grant Skinner +* +* Permission is hereby granted, free of charge, to any person +* obtaining a copy of this software and associated documentation +* files (the "Software"), to deal in the Software without +* restriction, including without limitation the rights to use, +* copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following +* conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +**/ +(function(m){function a(){throw"SoundJS cannot be instantiated";}function n(a,d){this.instances=[];this.name=a;this.src=d;this.loaded=this.canPlay=false;this.length=0}a.AUDIO_TIMEOUT=8E3;a.INTERRUPT_ANY=1;a.INTERRUPT_EARLY=2;a.INTERRUPT_LATE=3;a.INTERRUPT_NONE=4;a.onProgress=null;a.onSoundTimeout=null;a.onSoundLoadError=null;a.onSoundLoadComplete=null;a.onLoadQueueComplete=null;a.onSoundComplete=null;a.soundHash=[];a.loadQueue=[];a.itemsToLoad=0;a.instanceCount=0;a.INST_MAX=35;a.FT=0.0010;a.AUDIO_ERROR= +"error";a.AUDIO_PROGRESS="progress";a.AUDIO_COMPLETE="canplaythrough";a.AUDIO_ENDED="ended";a.AUDIO_STALLED="stalled";a._master=1;a._currentLoad=0;a.add=function(b,d,c){a.loadQueue.push({name:b,src:d,instances:c});a.loadQueue.length==1?(a.itemsToLoad=1,a.loadNext()):a.itemsToLoad++};a.addBatch=function(b){for(var d=b.length;b.length;)a.loadQueue.push(b.shift());a.loadQueue.length==d?(a.loadNext(),a.itemsToLoad=d):a.itemsToLoad++};a.play=function(b,d,c,e,f){if(b==null||a.soundHash[b]==null||a.soundHash[b].length== +0||d!=a.INTERRUPT_ANY&&d!=a.INTERRUPT_EARLY&&d!=a.INTERRUPT_LATE&&d!=a.INTERRUPT_NONE&&d!=null)return NaN;if(d==null)d=a.INTERRUPT_NONE;e==null&&(e=false);f||(f=0);c==null||c>1?c=1:c<0&&(c=0);if(f>0)setTimeout(function(){a.beginPlaying(b,d,c,e)},f);else return a.beginPlaying(b,d,c,e);return-1};a.getMasterVolume=function(){return a._master};a.setMasterVolume=function(b){if(Number(b)!=null){b<0?b=0:b>1&&(b=1);var d,c,e;d=a._master;a._master=b;if(a._master!=d)for(e in a.soundHash){c=a.soundHash[e];d= +c.length;for(b=0;b=h.duration-a.FT&&!h.loop|| +h.currentTime==0&&h.paused)i=true,k=b;else if(d==a.INTERRUPT_EARLY&&h.currentTimeg.currentTime)i=true;i&&(g=h,f=b)}d==a.INTERRUPT_ANY&&!g&&(g=j[0],f=0);return g?(g.loop=e,g.storedVolume=c,g.volume=c*a._master,g.currentTime=0,g.paused&&g.play(),f):-1};a.loadNext=function(){if(a.loadQueue.length<=0){if(a.onLoadQueueComplete)a.onLoadQueueComplete()}else{var b=a.loadQueue.shift(),d=b.instances||1,c=b.name,b=b.src,e=a.soundHash[c];if(e==null)e=a.soundHash[c]= +[];else if(e.length)b=e[0].src;for(var f=e=e.length,d=e+d;f