forked from amanva/The-Last-Magus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassetmanager.js
119 lines (103 loc) · 3.36 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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 that = this;
var path = this.downloadQueue[i];
console.log(path);
var ext = path.substring(path.length - 3);
switch (ext) {
case 'jpg':
case 'png':
var img = new Image();
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;
break;
case 'wav':
case 'mp3':
case 'mp4':
var aud = new Audio();
aud.addEventListener("loadeddata", function () {
console.log("Loaded " + this.src);
that.successCount++;
if (that.isDone()) callback();
});
aud.addEventListener("error", function () {
console.log("Error loading " + this.src);
that.errorCount++;
if (that.isDone()) callback();
});
aud.addEventListener("ended", function () {
aud.pause();
aud.currentTime = 0;
});
aud.src = path;
aud.load();
this.cache[path] = aud;
break;
}
}
};
getAsset(path) {
return this.cache[path];
};
playAsset(path) {
let audio = this.cache[path];
audio.currentTime = 0;
audio.play();
};
muteAudio(mute) {
for (var key in this.cache) {
let asset = this.cache[key];
if (asset instanceof Audio) {
asset.muted = mute;
}
}
};
adjustVolume(volume) {
for (var key in this.cache) {
let asset = this.cache[key];
if (asset instanceof Audio) {
asset.volume = volume;
}
}
};
pauseBackgroundMusic() {
for (var key in this.cache) {
let asset = this.cache[key];
if (asset instanceof Audio) {
asset.pause();
asset.currentTime = 0;
}
}
};
autoRepeat(path) {
var aud = this.cache[path];
aud.addEventListener("ended", function () {
aud.play();
});
};
};