-
Notifications
You must be signed in to change notification settings - Fork 0
/
assetmanager.js
47 lines (39 loc) · 1.14 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
"use strict";
// Global namespace AntColony
var AntColony = AntColony || {};
function AssetManager() {
this.successCount = 0;
this.errorCount = 0;
this.cache = [];
this.downloadQueue = [];
}
AssetManager.prototype.queueDownload = function (path) {
this.downloadQueue.push(path);
};
AssetManager.prototype.isDone = function () {
return this.downloadQueue.length === this.successCount + this.errorCount;
}
AssetManager.prototype.downloadAll = function (callback) {
const that = this;
this.downloadQueue.forEach(function (path) {
const img = new Image();
img.addEventListener("load", function () {
++that.successCount;
if (that.isDone()) {
callback();
};
});
img.addEventListener("error", function () {
console.error(Error("Image [" + img + "] could not be loaded!"));
that.errorCount++;
if (that.isDone()) {
callback();
};
});
img.src = path;
that.cache[path] = img;
});
};
AssetManager.prototype.getAsset = function (path) {
return this.cache[path];
};