-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassetmanager.js
48 lines (39 loc) · 1.23 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
45
46
47
class AssetManager {
constructor() {
this.successCount = 0;
this.errorCount = 0;
this.cache = [];
this.downloadQueue = [];
};
queueDownload(path) {
console.log("Queueing " + path);
this.downloadQueue.push(path);
};
isDone() {
return this.downloadQueue.length === this.successCount + this.errorCount;
};
downloadAll(callback) {
if (this.downloadQueue.length === 0) setTimeout(callback, 10);
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;
}
};
getAsset(path) {
return this.cache[path];
};
};