Skip to content
This repository has been archived by the owner on Mar 3, 2024. It is now read-only.

Commit

Permalink
adding sound to the engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Nov 13, 2016
1 parent d635742 commit 0348757
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<script type="text/javascript" src="battle.js"></script>
<script type="text/javascript" src="bootstrap.js"></script>
<script type="text/javascript" src="bgmusic.js"></script>
<script type="text/javascript" src="sound.js"></script>
<script type="text/javascript" src="fullscreen.js"></script>


Expand Down
62 changes: 43 additions & 19 deletions src/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ resources.harvest = function(callback) {
var CHARASETS = "charaset/";
var LEVELS = "levels/";
var MUSIC = "audio/music/";
var AUDIO = "audio/";

this.faceset = document.getElementById("faceset");
this.charasetimg = document.getElementById("charasetimg");
Expand All @@ -46,6 +47,7 @@ resources.harvest = function(callback) {
this.pictures = {};
this.syspictures = {};
this.music = {};
this.sound = {};
this.syspictures.title = document.getElementById("titleimg");
this.syspictures.keys1 = document.getElementById("keys1");
this.syspictures.keys2 = document.getElementById("keys2");
Expand Down Expand Up @@ -398,27 +400,49 @@ resources.harvest = function(callback) {
DESCRIPTORS + resources.init["itemsFile"],
'json');


var MusicList = this.init['MusicList'];
for (var music in MusicList) {
var musicFile = MusicList[music];
//the if cases here assure only one type is loaded
//and that they will be supported if available.
if(this.audioSupport.ogg && 'ogg' in musicFile){
scheduleLoad(['music',music],
MUSIC + musicFile.ogg,
'ogg');
} else if(this.audioSupport.mp3 && 'mp3' in musicFile){
scheduleLoad(['music',music],
MUSIC + musicFile.mp3,
'mp3');
} else if(this.audioSupport.wav && 'wav' in musicFile){
scheduleLoad(['music',music],
MUSIC + musicFile.wav,
'wav');
}
if('MusicList' in this.init){
var MusicList = this.init['MusicList'];
for (var music in MusicList) {
var musicFile = MusicList[music];
//the if cases here assure only one type is loaded
//and that they will be supported if available.
if(this.audioSupport.ogg && 'ogg' in musicFile){
scheduleLoad(['music',music],
MUSIC + musicFile.ogg,
'ogg');
} else if(this.audioSupport.mp3 && 'mp3' in musicFile){
scheduleLoad(['music',music],
MUSIC + musicFile.mp3,
'mp3');
} else if(this.audioSupport.wav && 'wav' in musicFile){
scheduleLoad(['music',music],
MUSIC + musicFile.wav,
'wav');
}
}
}

if('SoundList' in this.init){
var SoundList = this.init['SoundList'];
for (var sound in SoundList) {
var soundFile = SoundList[sound];
//the if cases here assure only one type is loaded
//and that they will be supported if available.
if(this.audioSupport.ogg && 'ogg' in soundFile){
scheduleLoad(['sound',sound],
AUDIO + soundFile.ogg,
'ogg');
} else if(this.audioSupport.mp3 && 'mp3' in soundFile){
scheduleLoad(['sound',sound],
AUDIO + soundFile.mp3,
'mp3');
} else if(this.audioSupport.wav && 'wav' in soundFile){
scheduleLoad(['sound',sound],
AUDIO + soundFile.wav,
'wav');
}
}
}
loadFromSchedule();

};
Expand Down
21 changes: 21 additions & 0 deletions src/sound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// sound.js

sound = {
setup: function(){
this.playing = '';
},
play: function(s, volume) {
volume = (typeof volume === "undefined") ? 0.7 : volume;
if(!(s in resources.sound))
return

var playSound = (function(that,s, volume){
return function() {
resources.sound[s].volume = volume;
resources.sound[s].play();
}
})(this,s, volume);

setTimeout(playSound,50);
}
}

0 comments on commit 0348757

Please sign in to comment.