-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceLoader.js
148 lines (125 loc) · 4.4 KB
/
ResourceLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var ResourceLoader = (function(){
var totalAssets = 0,
loadedAssets = 0,
assetsDownloaded = false,
UserOptions,
percentageLoaded,
audioSupport,
assets = {};
//default options if no option argument is passed by user.
var defaultOptions = {
onload: function(){
console.log('No callback was passed in..');
},
final : function(){
console.log('No final function to call was passed in..');
},
assets : []
};
/* Example of options:
{
assets : {
imgs : {
//img names with URLs
bg_black : "assets/img/background.png,
meteorSprite : "assets/img/meteor.png,
enemiesSprite : "assets/img/enemies.png,
enemyShipSprites: "assets/img/enemyShips.png
},
sounds : {
//sounds name with URLs
soundtrack : ["assets/sounds/player.mp3", "assets/sounds/player.wav"],
endSound : ["assets/sounds/gameover.mp3", "assets/sounds/gameover.wav"]
}
}
useHowl : false,
//function that is called on every item loaded
onload : updateLoaderDisplayer,
//function that is called once all have loaded
final : finalFunctionToCall
}
*/
function init(options){
UserOptions = options || defaultOptions;
}
function download(){
//return if all assets have already loaded and indexed.
if(assetsDownloaded) return;
//sets to true so this method (downloadAll()) is called only onced.
assetsDownloaded = true;
//creates the sound and image elements for each asset in the options argument.
for(var item in UserOptions.assets.imgs){
assets[item] = new Image();
assets[item].src = UserOptions.assets.imgs[item];
assets[item].addEventListener('load', onLoad, false);
totalAssets++;
}
//arrays are passed for each sound asset containing the source to the sound.
if(!UserOptions.useHowl){
for(var item in UserOptions.assets.sounds){
for(var i=0, j=UserOptions.assets.sounds[item].length; i<j; i++){
var currentSound = UserOptions.assets.sounds[item][i];
//analyzes the extension and picks the first one that is supported by the browser.
if(isAudioSupport(currentSound.slice(-3))){
assets[item] = new Audio();
assets[item].src = UserOptions.assets.sounds[item][i];
assets[item].addEventListener('canplaythrough', onLoad, false);
totalAssets++;
break;
}
}
}
}else{
//creates howl instance and passes in howl options given by the user.
for(var item in UserOptions.assets.sounds){
var howlOptions = UserOptions.assets.sounds[item];
howlOptions.onload = onLoad;
assets[item] = new Howl(howlOptions);
totalAssets++;
}
}
}
function onLoad(item){
//increments load count and remove listeners.
loadedAssets++;
var itemLoaded;
if(item){
if(item.target.tagName === "AUDIO"){
itemLoaded = item.target;
item.target.removeEventListener('canplaythrough', onLoad, false);
}else if (item.target.tagName === "IMG"){
itemLoaded = item.target;
item.target.removeEventListener('load', onLoad, false);
}
}else{
itemLoaded = {name: "Howl sound", src: "Howl sound check sound"};
}
//calculates the decimal value from ratio
percentageLoaded = Math.floor((loadedAssets / totalAssets)*100)/100;
//call the appropriate callback function given the ammount of assets loaded.
if(UserOptions.onload){
UserOptions.onload(itemLoaded);
}
if(loadedAssets >= totalAssets && UserOptions.final){
UserOptions.final(itemLoaded);
}
}
//gets the appropriate supported audio format.
function isAudioSupport(extension){
var audio = new Audio();
var supportValue = audio.canPlayType("audio/"+extension);
if( supportValue === "maybe" || supportValue === "probably" ){
return true;
}else{
return false;
}
}
return {
init : init,
assets : assets,
get loaded(){
return percentageLoaded;
},
downloadAll : download
}
})();