-
Notifications
You must be signed in to change notification settings - Fork 21
/
ResourceManager.js
272 lines (236 loc) · 6.85 KB
/
ResourceManager.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* The ResourceManager handles loading images and sounds for use in games.
* @name ResourceManager
* @constructor ResourceManager
*/
'use strict';
const has = require('./has');
const Sound = require('./sounds/Sound');
const WebAudio = require('./sounds/WebAudio');
var resourceList = {};
//TODO: move these to its own module for unit testing?
function normalizePath(baseDir, path){
var joinedPath = path;
if(baseDir){
joinedPath = [baseDir, path].join('/');
}
return joinedPath.replace(/\/{2,}/g, '/');
}
function flipX(image){
var offscreenCanvas = document.createElement('canvas');
offscreenCanvas.height = image.height;
offscreenCanvas.width = image.width;
var ctx = offscreenCanvas.getContext('2d');
ctx.translate(offscreenCanvas.width, 0);
ctx.scale(-1, 1);
ctx.drawImage(image, 0, 0);
return offscreenCanvas.toDataURL();
}
function flipY(image){
var offscreenCanvas = document.createElement('canvas');
offscreenCanvas.height = image.height;
offscreenCanvas.width = image.width;
var ctx = offscreenCanvas.getContext('2d');
ctx.translate(0, offscreenCanvas.height);
ctx.scale(1, -1);
ctx.drawImage(image, 0, 0);
return offscreenCanvas.toDataURL();
}
class ImageWrapper {
constructor(filename){
var self = this;
self.name = filename;
self.complete = false;
self.img = new Image();
self.img.addEventListener('load', function() {
self.complete = true;
}, false);
}
load(){
this.img.src = this.name;
}
}
class ResourceManager {
constructor(options = {}){
/**
* Whether all the resources have been loaded
* @type {Boolean}
* @memberOf ResourceManager#
* @default
*/
this.allLoaded = false;
/**
* The base directory to load images from
* @type {String}
* @memberOf ResourceManager#
* @default
*/
this.imageDir = null;
/**
* The base directory to load sounds from
* @type {String}
* @memberOf ResourceManager#
* @default
*/
this.soundDir = null;
/**
* A map of all the resources by their URLs
* @type {Object}
* @memberOf ResourceManager#
* @default
*/
this.resourceList = resourceList;
Object.assign(this, options);
// TODO not sure a better way
if(!this.Sound){
if(has('WebAudio')){
this.Sound = WebAudio;
}
else{
this.Sound = Sound;
}
}
}
/**
* Loads an image (or a collection of images), and tracks if it has finished loading
* @function
* @memberOf ResourceManager#
* @param {String|Array} files Filename of the image relative the Game's HTML page.
* @returns {Image|Array} Return type based on argument: Image if String or Array of Images if Array
*/
loadImage(files){
let singleFile = false;
if(!Array.isArray(files)) {
singleFile = true;
files = [files];
}
const fileList = files.map((file) => {
const filename = normalizePath(this.imageDir, file);
//if we already have the image, just return it
if(this.resourceList[filename]){
return this.resourceList[filename].img;
}
this.allLoaded = false;
const wrapper = new ImageWrapper(filename);
// Need to explicitly call load because flipImage also uses this object
// which is probably a bad idea and should change in future
// TODO: different objects for flipped image and regular image
wrapper.load();
this.resourceList[filename] = wrapper;
return wrapper.img;
});
return singleFile ? fileList[0] : fileList;
}
/**
* Loads a sound file (or a collection of sound files), and tracks if it has finished loading
* @function
* @memberOf ResourceManager#
* @param {String|Array} filename Filename of the sound relative the Game's HTML page.
* @returns {Sound|Array} Return type based on argument: Sound Object if String or Array of Sound Objects if Array
*/
loadSound(files){
let singleFile = false;
if(!Array.isArray(files)) {
singleFile = true;
files = [files];
}
const fileList = files.map((file) => {
const filename = normalizePath(this.soundDir, file);
//if we already have the sound, just return it
if(this.resourceList[filename]){
return this.resourceList[filename];
}
this.allLoaded = false;
const sound = new this.Sound(filename);
this.resourceList[filename] = sound;
return sound;
});
return singleFile ? fileList[0] : fileList;
}
/**
* Flips an image using the logic in a flip function passed and attaches to resource manager with name
* @function
* @memberOf ResourceManager#
* @param {String|Number} name Name for caching flipped image
* @param {Image} image Image to be flipped
* @param {Function} flipFn Function containing logic to flip image
* @return {Image} Flipped image
*/
flipImage(name, image, flipFn){
this.allLoaded = false;
const wrapper = new ImageWrapper(name);
this.resourceList[name] = wrapper;
const img2 = new Image();
function doFlip() {
wrapper.img.src = flipFn(img2);
img2.removeEventListener('load', doFlip);
}
img2.addEventListener('load', doFlip);
img2.src = image.src;
return wrapper.img;
}
/**
* Flip image along x-axis using default flip logic
* @function
* @memberOf ResourceManager#
* @param {String|Number} name Name for caching flipped image
* @param {Image} image Image to be flipped
* @return {Image} Flipped image
*/
flipImageX(name, image){
return this.flipImage(name, image, flipX);
}
/**
* Flip image along the y-axis using default flip logic
* @function
* @memberOf ResourceManager#
* @param {String|Number} name Name for caching flipped image
* @param {Image} image Image to be flipped
* @return {Image} Flipped image
*/
flipImageY(name, image){
return this.flipImage(name, image, flipY);
}
/**
* Checks whether the resources have finished loading
* @function
* @memberOf ResourceManager#
*/
resourcesReady(){
if(this.allLoaded){
return true;
} else {
for(var filename in this.resourceList){
var resource = this.resourceList[filename];
if(!resource.complete){
return false;
}
}
this.allLoaded = true;
return true;
}
}
/**
* Gets the percent of resources loaded.
* @function
* @memberOf ResourceManager#
* @return {Number} The percent of resources loaded
*/
getPercentComplete(){
var numComplete = 0.0;
var length = 0;
for(var filename in this.resourceList){
var resource = this.resourceList[filename];
length++;
if(resource.complete){
numComplete = numComplete + 1.0;
}
}
if(length === 0){
return 0;
} else {
return Math.round((numComplete / length) * 100.0);
}
}
}
module.exports = ResourceManager;