-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassetmanager.js
47 lines (38 loc) · 1.19 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
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 (let i = 0; i < this.downloadQueue.length; i++) {
const img = new Image();
const path = this.downloadQueue[i];
console.log(path);
img.addEventListener("load", () => {
console.log("Loaded " + img.src);
this.successCount++;
if (this.isDone()) callback();
});
img.addEventListener("error", () => {
console.log("Error loading " + img.src);
this.errorCount++;
if (this.isDone()) callback();
});
img.src = path;
this.cache[path] = img;
}
};
getAsset(path) {
return this.cache[path];
};
};