-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAssetLoader.js
More file actions
83 lines (74 loc) · 2.65 KB
/
AssetLoader.js
File metadata and controls
83 lines (74 loc) · 2.65 KB
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
var AssetLoader = (function () {
function AssetLoader () {
this.assets = {};
this.onAssetsLoaded = null;
};
AssetLoader.prototype.init = function (files) {
this.total = files.length;
for(var i = files.length - 1; i >= 0; i--) {
var extension = files[i].path.match(/.\w+$/)[0];
var asset = null;
switch(extension) {
case '.png':
case '.jpg':
case '.bmp':
asset = new Image();
asset.onload = this.onLoadComplete.bind(this);
break;
case '.txt':
var that = this;
var xhr = new XMLHttpRequest();
xhr.open('GET', files[i].path, true);
xhr.filename = files[i].name;
xhr.onload = function (e) {
that.assets[this.filename] = JSON.parse(this.response);
that.onLoadComplete();
};
xhr.send();
asset = false;
break;
case '.mp3':
var that = this;
var xhr = new XMLHttpRequest();
xhr.open('GET', files[i].path, true);
xhr.responseType = 'arraybuffer';
xhr.filename = files[i].name;
xhr.onload = function (e) {
//some hackery...
var xhrScope = this;
audioAPI.ctx.decodeAudioData(this.response, function (buffer) {
that.assets[xhrScope.filename] = buffer;
that.onLoadComplete();
});
};
xhr.send();
asset = false;
break;
}
if(asset == null) {
console.log('INF: extension: "%s" is not supported', extension);
this.onLoadComplete();
continue;
} else if(asset == false) {
continue;
}
asset.src = files[i].path;
this.assets[files[i].name] = asset;
}
};
AssetLoader.prototype.onLoadComplete = function () {
this.total--;
if(this.total == 0) {
if(typeof this.onAssetsLoaded === 'function') {
this.onAssetsLoaded();
}
}
};
AssetLoader.prototype.get = function (name) {
if(this.assets[name]) {
return this.assets[name];
}
return null;
};
return AssetLoader;
})();