-
Notifications
You must be signed in to change notification settings - Fork 0
/
assetmanager.js
44 lines (36 loc) · 1.17 KB
/
assetmanager.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
function AssetManager() {
this.successCount = 0;
this.errorCount = 0;
this.cache = [];
this.downloadQueue = [];
}
AssetManager.prototype.queueDownload = function (path) {
console.log("Queueing " + path);
this.downloadQueue.push(path);
}
AssetManager.prototype.isDone = function () {
return this.downloadQueue.length === this.successCount + this.errorCount;
}
AssetManager.prototype.downloadAll = function (callback) {
for (var i = 0; i < this.downloadQueue.length; i++) {
var img = new Image();
var that = this;
var path = this.downloadQueue[i];
console.log(path);
img.addEventListener("load", function () {
console.log("Loaded " + this.src);
that.successCount++;
if(that.isDone()) callback();
});
img.addEventListener("error", function () {
console.log("Error loading " + this.src);
that.errorCount++;
if (that.isDone()) callback();
});
img.src = path;
this.cache[path] = img;
}
}
AssetManager.prototype.getAsset = function (path) {
return this.cache[path];
}