-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassetmanager.js
More file actions
106 lines (91 loc) · 3.31 KB
/
assetmanager.js
File metadata and controls
106 lines (91 loc) · 3.31 KB
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
class AssetManager {
constructor() {
this.successCount = 0;
this.errorCount = 0;
this.cache = [];
this.downloadQueue = [];
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
};
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);
console.log("queue: ", this.downloadQueue);
for (let i = 0; i < this.downloadQueue.length; i++) {
var that = this;
const path = this.downloadQueue[i];
console.log(path);
var extension = path.substring(path.length - 3);
// Switch statement to determine asset to load.
switch(extension) {
case 'jpeg':
case 'jpg':
case 'png':
const img = new Image();
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;
break;
case 'wav':
case 'mp3':
fetch(path)
.then(response => response.arrayBuffer())
.then(arrayBuffer => this.audioContext.decodeAudioData(arrayBuffer))
.then(audioBuffer => {
this.cache[path] = audioBuffer;
console.log("Loaded " + path);
this.successCount++;
if (this.isDone()) callback();
})
.catch(error => {
console.error('Error decoding audio data:', error);
this.errorCount++;
if (this.isDone()) callback();
});
break;
}
}
};
getAsset(path) {
return this.cache[path];
};
// muteAudio(mute) {
// for (var key in this.cache) {
// let asset = this.cache[key];
// if (asset instanceof AudioBuffer) {
// asset.muted = mute;
// }
// }
// };
// adjustVolume(volume) {
// for (var key in this.cache) {
// let asset = this.cache[key];
// if (asset instanceof AudioBuffer) {
// asset.volume = volume;
// }
// }
// };
pauseBackgroundMusic() {
for (var key in this.cache) {
let asset = this.cache[key];
if (asset instanceof AudioBuffer) {
asset.pause();
asset.currentTime = 0;
}
}
};
};