melonjs 12.0.0 → 13.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +4 -6
  3. package/dist/melonjs.js +22648 -22478
  4. package/dist/melonjs.min.js +5 -6
  5. package/dist/melonjs.module.d.ts +289 -264
  6. package/dist/melonjs.module.js +21979 -21804
  7. package/package.json +15 -16
  8. package/src/application/application.js +231 -0
  9. package/src/audio/audio.js +8 -2
  10. package/src/camera/camera2d.js +6 -6
  11. package/src/game.js +9 -232
  12. package/src/index.js +3 -3
  13. package/src/input/keyboard.js +2 -2
  14. package/src/input/pointer.js +4 -5
  15. package/src/input/pointerevent.js +10 -10
  16. package/src/lang/deprecated.js +27 -30
  17. package/src/level/level.js +2 -2
  18. package/src/level/tiled/TMXGroup.js +10 -0
  19. package/src/level/tiled/TMXLayer.js +11 -2
  20. package/src/level/tiled/TMXObject.js +13 -2
  21. package/src/level/tiled/TMXTileMap.js +15 -3
  22. package/src/level/tiled/TMXTileset.js +8 -0
  23. package/src/loader/loader.js +64 -28
  24. package/src/loader/loadingscreen.js +27 -28
  25. package/src/math/color.js +62 -42
  26. package/src/math/observable_vector2.js +26 -2
  27. package/src/math/observable_vector3.js +32 -4
  28. package/src/math/vector2.js +23 -0
  29. package/src/math/vector3.js +26 -0
  30. package/src/physics/body.js +27 -51
  31. package/src/physics/detector.js +3 -3
  32. package/src/physics/quadtree.js +58 -29
  33. package/src/physics/world.js +32 -3
  34. package/src/renderable/container.js +2 -2
  35. package/src/renderable/imagelayer.js +8 -8
  36. package/src/renderable/nineslicesprite.js +27 -1
  37. package/src/renderable/trigger.js +4 -4
  38. package/src/state/stage.js +1 -1
  39. package/src/state/state.js +50 -3
  40. package/src/system/device.js +814 -981
  41. package/src/system/event.js +2 -1
  42. package/src/system/platform.js +32 -0
  43. package/src/system/save.js +23 -14
  44. package/src/system/timer.js +12 -35
  45. package/src/text/text.js +9 -12
  46. package/src/tweens/tween.js +6 -6
  47. package/src/utils/string.js +13 -0
  48. package/src/video/canvas/canvas_renderer.js +30 -65
  49. package/src/video/renderer.js +23 -30
  50. package/src/video/texture/canvas_texture.js +39 -3
  51. package/src/video/video.js +27 -25
  52. package/src/video/webgl/glshader.js +1 -1
  53. package/src/video/webgl/webgl_compositor.js +2 -2
  54. package/src/video/webgl/webgl_renderer.js +8 -20
@@ -1,7 +1,6 @@
1
1
  import Vector2d from "./../math/vector2.js";
2
- import device from "./../system/device.js";
3
2
  import Bounds from "./../physics/bounds.js";
4
- import { viewport } from "./../game.js";
3
+ import game from "./../game.js";
5
4
  import { globalToLocal } from "./input.js";
6
5
  import { locked } from "./pointerevent.js";
7
6
 
@@ -333,7 +332,7 @@ class Pointer extends Bounds {
333
332
  this.gameScreenY = this.y = tmpVec.y;
334
333
 
335
334
  // true if not originally a pointer event
336
- this.isNormalized = !device.PointerEvent || (device.PointerEvent && !(event instanceof globalThis.PointerEvent));
335
+ this.isNormalized = (typeof globalThis.PointerEvent !== "undefined" && !(event instanceof globalThis.PointerEvent));
337
336
 
338
337
  this.locked = locked;
339
338
  this.movementX = event.movementX || 0;
@@ -361,8 +360,8 @@ class Pointer extends Bounds {
361
360
  this.type = event.type;
362
361
 
363
362
  // get the current screen to game world offset
364
- if (typeof viewport !== "undefined") {
365
- viewport.localToWorld(this.gameScreenX, this.gameScreenY, tmpVec);
363
+ if (typeof game.viewport !== "undefined") {
364
+ game.viewport.localToWorld(this.gameScreenX, this.gameScreenY, tmpVec);
366
365
  }
367
366
 
368
367
  /* Initialize the two coordinate space properties. */
@@ -6,11 +6,11 @@ import { remove } from "./../utils/array.js";
6
6
  import * as event from "./../system/event.js";
7
7
  import timer from "./../system/timer.js";
8
8
  import pool from "./../system/pooling.js";
9
- import device from "./../system/device.js";
9
+ import * as device from "./../system/device.js";
10
10
  import Pointer from "./pointer.js";
11
11
  import Rect from "./../geometries/rectangle.js";
12
12
  import Container from "./../renderable/container.js";
13
- import { world, viewport } from "./../game.js";
13
+ import game from "./../game.js";
14
14
 
15
15
  /**
16
16
  * A pool of `Pointer` objects to cache pointer/touch event coordinates.
@@ -125,17 +125,17 @@ function enablePointerEvent() {
125
125
 
126
126
  if (pointerEventTarget === null) {
127
127
  // default pointer event target
128
- pointerEventTarget = renderer.getScreenCanvas();
128
+ pointerEventTarget = renderer.getCanvas();
129
129
  }
130
130
 
131
- if (device.PointerEvent) {
131
+ if (device.pointerEvent) {
132
132
  // standard Pointer Events
133
133
  activeEventList = pointerEventList;
134
134
  } else {
135
135
  // Regular Mouse events
136
136
  activeEventList = mouseEventList;
137
137
  }
138
- if (device.touch && !device.PointerEvent) {
138
+ if (device.touch && !device.pointerEvent) {
139
139
  // touch event on mobile devices
140
140
  activeEventList = activeEventList.concat(touchEventList);
141
141
  }
@@ -287,10 +287,10 @@ function dispatchEvent(normalizedEvents) {
287
287
  }
288
288
 
289
289
  // fetch valid candiates from the game world container
290
- var candidates = world.broadphase.retrieve(currentPointer, Container.prototype._sortReverseZ);
290
+ var candidates = game.world.broadphase.retrieve(currentPointer, Container.prototype._sortReverseZ);
291
291
 
292
292
  // add the main game viewport to the list of candidates
293
- candidates = candidates.concat([ viewport ]);
293
+ candidates = candidates.concat([ game.viewport ]);
294
294
 
295
295
  for (var c = candidates.length, candidate; c--, (candidate = candidates[c]);) {
296
296
  if (eventHandlers.has(candidate) && (candidate.isKinematic !== true)) {
@@ -416,7 +416,7 @@ function normalizeEvent(originalEvent) {
416
416
  var _pointer;
417
417
 
418
418
  // PointerEvent or standard Mouse event
419
- if (device.TouchEvent && originalEvent.changedTouches) {
419
+ if (device.touchEvent && originalEvent.changedTouches) {
420
420
  // iOS/Android Touch event
421
421
  for (var i = 0, l = originalEvent.changedTouches.length; i < l; i++) {
422
422
  var touchEvent = originalEvent.changedTouches[i];
@@ -556,8 +556,8 @@ export var throttlingInterval;
556
556
  */
557
557
  export function globalToLocal(x, y, v) {
558
558
  v = v || pool.pull("Vector2d");
559
- var rect = device.getElementBounds(renderer.getScreenCanvas());
560
- var pixelRatio = device.devicePixelRatio;
559
+ var rect = device.getElementBounds(renderer.getCanvas());
560
+ var pixelRatio = globalThis.devicePixelRatio || 1;
561
561
  x -= rect.left + (globalThis.pageXOffset || 0);
562
562
  y -= rect.top + (globalThis.pageYOffset || 0);
563
563
  var scale = scaleRatio;
@@ -1,5 +1,3 @@
1
- import device from "./../system/device.js";
2
- import { requestPointerLock, exitPointerLock } from "./../input/input.js";
3
1
  import { TextureAtlas } from "./../video/texture/atlas.js";
4
2
  import Renderer from "./../video/renderer.js";
5
3
  import { Draggable, DropTarget } from "./../renderable/dragndrop.js";
@@ -45,35 +43,8 @@ export function warning(deprecated, replacement, version) {
45
43
  }
46
44
  };
47
45
 
48
- /**
49
- * @public
50
- * @name turnOnPointerLock
51
- * @returns {boolean} return true if the request was successfully submitted
52
- * @memberof device#
53
- * @deprecated since 10.3.0
54
- * @see input.requestPointerLock
55
- */
56
- device.turnOnPointerLock = function () {
57
- warning("device.turnOnPointerLock()", "input.requestPointerLock()", "10.3.0");
58
- return requestPointerLock();
59
- };
60
-
61
- /**
62
- * @public
63
- * @name turnOffPointerLock
64
- * @returns {boolean} return true if the request was successfully submitted
65
- * @memberof device#
66
- * @deprecated since 10.3.0
67
- * @see input.exitPointerLock
68
- */
69
- device.turnOffPointerLock = function () {
70
- warning("device.turnOffPointerLock()", "input.exitPointerLock()", "10.3.0");
71
- return exitPointerLock();
72
- };
73
-
74
46
  /**
75
47
  * Alias of {@link TextureAtlas}
76
- *
77
48
  * @public
78
49
  * @name Texture
79
50
  * @class
@@ -91,7 +62,6 @@ Object.defineProperty(Renderer.prototype, "Texture", {
91
62
  }
92
63
  });
93
64
 
94
-
95
65
  /**
96
66
  * @classdesc
97
67
  * Used to make a game entity draggable
@@ -129,3 +99,30 @@ export class DroptargetEntity extends DropTarget {
129
99
  super(x, y, settings.width, settings.height);
130
100
  }
131
101
  }
102
+
103
+ /**
104
+ * return a reference to the screen canvas
105
+ * @name getScreenCanvas
106
+ * @memberof Renderer
107
+ * @returns {HTMLCanvasElement}
108
+ * @deprecated since 13.1.0
109
+ * @see getCanvas();
110
+ */
111
+ Renderer.prototype.getScreenCanvas = function() {
112
+ warning("getScreenCanvas", "getCanvas", "13.1.0");
113
+ return this.getCanvas();
114
+ };
115
+
116
+ /**
117
+ * return a reference to the screen canvas corresponding 2d Context<br>
118
+ * (will return buffered context if double buffering is enabled, or a reference to the Screen Context)
119
+ * @name getScreenContext
120
+ * @memberof Renderer
121
+ * @returns {CanvasRenderingContext2D}
122
+ * @deprecated since 13.1.0
123
+ * @see getContext();
124
+ */
125
+ Renderer.prototype.getScreenContext = function() {
126
+ warning("getScreenContext", "getContext", "13.1.0");
127
+ return this.getContext();
128
+ };
@@ -2,7 +2,7 @@ import utils from "./../utils/utils.js";
2
2
  import * as event from "./../system/event.js";
3
3
  import state from "./../state/state.js";
4
4
  import loader from "./../loader/loader.js";
5
- import * as game from "./../game.js";
5
+ import game from "./../game.js";
6
6
  import TMXTileMap from "./tiled/TMXTileMap.js";
7
7
 
8
8
 
@@ -125,7 +125,7 @@ var level = {
125
125
  * @param {string} levelId level id
126
126
  * @param {object} [options] additional optional parameters
127
127
  * @param {Container} [options.container=game.world] container in which to load the specified level
128
- * @param {Function} [options.onLoaded=ame.onLevelLoaded] callback for when the level is fully loaded
128
+ * @param {Function} [options.onLoaded=game.onLevelLoaded] callback for when the level is fully loaded
129
129
  * @param {boolean} [options.flatten=game.mergeGroup] if true, flatten all objects into the given container
130
130
  * @param {boolean} [options.setViewportBounds=true] if true, set the viewport bounds to the map size
131
131
  * @returns {boolean} true if the level was successfully loaded
@@ -49,6 +49,16 @@ export default class TMXGroup {
49
49
  */
50
50
  this.tintcolor = data.tintcolor;
51
51
 
52
+
53
+ /**
54
+ * the group class
55
+ * @public
56
+ * @type {string}
57
+ * @name class
58
+ * @memberof TMXGroup
59
+ */
60
+ this.class = data.class;
61
+
52
62
  /**
53
63
  * group z order
54
64
  * @public
@@ -4,7 +4,7 @@ import * as TMXUtils from "./TMXUtils.js";
4
4
  import Tile from "./TMXTile.js";
5
5
  import Renderable from "./../../renderable/renderable.js";
6
6
  import CanvasRenderer from "./../../video/canvas/canvas_renderer";
7
- import { world } from "./../../game.js";
7
+ import game from "./../../game.js";
8
8
 
9
9
  /**
10
10
  * Create required arrays for the given layer object
@@ -138,6 +138,15 @@ class TMXLayer extends Renderable {
138
138
  */
139
139
  this.renderorder = data.renderorder || "right-down";
140
140
 
141
+ /**
142
+ * the layer class
143
+ * @public
144
+ * @type {string}
145
+ * @name class
146
+ * @name TMXLayer#class
147
+ */
148
+ this.class = data.class;
149
+
141
150
  // for displaying order
142
151
  this.pos.z = z;
143
152
 
@@ -173,7 +182,7 @@ class TMXLayer extends Renderable {
173
182
 
174
183
  // check for the correct rendering method
175
184
  if (typeof (this.preRender) === "undefined") {
176
- this.preRender = world.preRender;
185
+ this.preRender = game.world.preRender;
177
186
  }
178
187
 
179
188
  // set a renderer
@@ -99,20 +99,31 @@ export default class TMXObject {
99
99
  * object type
100
100
  * @public
101
101
  * @type {string}
102
+ * @deprecated since Tiled 1.9
103
+ * @see https://docs.mapeditor.org/en/stable/reference/tmx-changelog/#tiled-1-9
102
104
  * @name type
103
105
  * @memberof TMXObject
104
106
  */
105
107
  this.type = settings.type;
106
108
 
109
+ /**
110
+ * the åobject class
111
+ * @public
112
+ * @type {string}
113
+ * @name class
114
+ * @memberof TMXObject
115
+ */
116
+ this.class = typeof settings.class !== "undefined" ? settings.class : settings.type;
117
+
107
118
  /**
108
119
  * object text
109
120
  * @public
110
121
  * @type {object}
111
122
  * @see http://docs.mapeditor.org/en/stable/reference/tmx-map-format/#text
112
- * @name type
123
+ * @name text
113
124
  * @memberof TMXObject
114
125
  */
115
- this.type = settings.type;
126
+ this.text = undefined;
116
127
 
117
128
  /**
118
129
  * The rotation of the object in radians clockwise (defaults to 0)
@@ -1,6 +1,6 @@
1
1
  import pool from "./../../system/pooling.js";
2
2
  import * as event from "./../../system/event.js";
3
- import { viewport } from "./../../game.js";
3
+ import game from "./../../game.js";
4
4
  import collision from "./../../physics/collision.js";
5
5
  import Body from "./../../physics/body.js";
6
6
  import TMXOrthogonalRenderer from "./renderer/TMXOrthogonalRenderer.js";
@@ -204,6 +204,16 @@ class TMXTileMap {
204
204
  */
205
205
  this.tiledversion = data.tiledversion;
206
206
 
207
+
208
+ /**
209
+ * The map class.
210
+ * @public
211
+ * @type {string}
212
+ * @name TMXTileMap#class
213
+ */
214
+ this.class = data.class;
215
+
216
+
207
217
  // tilesets for this map
208
218
  this.tilesets = null;
209
219
 
@@ -396,7 +406,7 @@ class TMXTileMap {
396
406
  */
397
407
  function _setBounds(width, height) {
398
408
  // adjust the viewport bounds if level is smaller
399
- viewport.setBounds(
409
+ game.viewport.setBounds(
400
410
  0, 0,
401
411
  Math.max(levelBounds.width, width),
402
412
  Math.max(levelBounds.height, height)
@@ -413,7 +423,7 @@ class TMXTileMap {
413
423
  if (setViewportBounds === true) {
414
424
  event.off(event.VIEWPORT_ONRESIZE, _setBounds);
415
425
  // force viewport bounds update
416
- _setBounds(viewport.width, viewport.height);
426
+ _setBounds(game.viewport.width, game.viewport.height);
417
427
  // Replace the resize handler
418
428
  event.on(event.VIEWPORT_ONRESIZE, _setBounds, this);
419
429
  }
@@ -545,6 +555,8 @@ class TMXTileMap {
545
555
  obj.anchorPoint.set(0, 0);
546
556
  obj.name = settings.name;
547
557
  obj.type = settings.type;
558
+ // for backward compatibility
559
+ obj.class = settings.class || settings.type;
548
560
  obj.id = settings.id;
549
561
  obj.body = new Body(obj, shape);
550
562
  obj.body.setStatic(true);
@@ -62,6 +62,14 @@ class TMXTileset {
62
62
  */
63
63
  this.isCollection = false;
64
64
 
65
+ /**
66
+ * the tileset class
67
+ * @public
68
+ * @type {boolean}
69
+ * @name TMXTileset#class
70
+ */
71
+ this.class = tileset.class;
72
+
65
73
  /**
66
74
  * Tileset animations
67
75
  * @private
@@ -1,10 +1,11 @@
1
1
  import * as fileUtil from "./../utils/file.js";
2
2
  import * as event from "./../system/event.js";
3
- import device from "./../system/device.js";
3
+ import { ua } from "./../system/platform.js";
4
4
  import * as audio from "./../audio/audio.js";
5
5
  import state from "./../state/state.js";
6
6
  import level from "./../level/level.js";
7
7
  import * as TMXUtils from "./../level/tiled/TMXUtils.js";
8
+ import { isDataUrl } from "./../utils/string.js";
8
9
 
9
10
 
10
11
  // contains all the images loaded
@@ -30,7 +31,7 @@ var timerId = 0;
30
31
  function checkLoadStatus(onload) {
31
32
  if (loadCount === resourceCount) {
32
33
  // wait 1/2s and execute callback (cheap workaround to ensure everything is loaded)
33
- if (onload || loader.onload) {
34
+ if (typeof onload === "function" || loader.onload) {
34
35
  // make sure we clear the timer
35
36
  clearTimeout(timerId);
36
37
  // trigger the onload callback
@@ -66,8 +67,12 @@ function checkLoadStatus(onload) {
66
67
  function preloadImage(img, onload, onerror) {
67
68
  // create new Image object and add to list
68
69
  imgList[img.name] = new Image();
69
- imgList[img.name].onload = onload;
70
- imgList[img.name].onerror = onerror;
70
+ if (typeof onload === "function") {
71
+ imgList[img.name].onload = onload;
72
+ }
73
+ if (typeof onerror === "function") {
74
+ imgList[img.name].onerror = onerror;
75
+ }
71
76
  if (typeof (loader.crossOrigin) === "string") {
72
77
  imgList[img.name].crossOrigin = loader.crossOrigin;
73
78
  }
@@ -83,17 +88,30 @@ function preloadImage(img, onload, onerror) {
83
88
  * @ignore
84
89
  */
85
90
  function preloadFontFace(data, onload, onerror) {
91
+
92
+ if (isDataUrl(data.src) === true) {
93
+ // make sure it in the `url(data:[<mediatype>][;base64],<data>)` format as expected by FontFace
94
+ if (!data.src.startsWith("url(")) {
95
+ data.src = "url(" + data.src + ")";
96
+ }
97
+ }
98
+
86
99
  var font = new FontFace(data.name, data.src);
100
+
87
101
  // loading promise
88
102
  font.load().then(() => {
89
103
  // apply the font after the font has finished downloading
90
104
  document.fonts.add(font);
91
105
  document.body.style.fontFamily = data.name;
92
- // onloaded callback
93
- onload();
106
+ if (typeof onload === "function") {
107
+ // onloaded callback
108
+ onload();
109
+ }
94
110
  }, function () {
95
- // rejected
96
- onerror(data.name);
111
+ if (typeof onerror === "function") {
112
+ // rejected
113
+ onerror(data.name);
114
+ }
97
115
  });
98
116
  };
99
117
 
@@ -119,7 +137,9 @@ function preloadTMX(tmxData, onload, onerror) {
119
137
  //if the data is in the tmxData object, don't get it via a XMLHTTPRequest
120
138
  if (tmxData.data) {
121
139
  addToTMXList(tmxData.data);
122
- onload();
140
+ if (typeof onload === "function") {
141
+ onload();
142
+ }
123
143
  return;
124
144
  }
125
145
 
@@ -153,7 +173,7 @@ function preloadTMX(tmxData, onload, onerror) {
153
173
  case "tmx":
154
174
  case "tsx":
155
175
  // ie9 does not fully implement the responseXML
156
- if (device.ua.match(/msie/i) || !xmlhttp.responseXML) {
176
+ if (ua.match(/msie/i) || !xmlhttp.responseXML) {
157
177
  if (globalThis.DOMParser) {
158
178
  // manually create the XML DOM
159
179
  result = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml");
@@ -190,9 +210,11 @@ function preloadTMX(tmxData, onload, onerror) {
190
210
  addToTMXList(result);
191
211
 
192
212
  // fire the callback
193
- onload();
213
+ if (typeof onload === "function") {
214
+ onload();
215
+ }
194
216
  }
195
- else {
217
+ else if (typeof onerror === "function") {
196
218
  onerror(tmxData.name);
197
219
  }
198
220
  }
@@ -224,10 +246,12 @@ function preloadJSON(data, onload, onerror) {
224
246
  if ((xmlhttp.status === 200) || ((xmlhttp.status === 0) && xmlhttp.responseText)) {
225
247
  // get the Texture Packer Atlas content
226
248
  jsonList[data.name] = JSON.parse(xmlhttp.responseText);
227
- // fire the callback
228
- onload();
249
+ if (typeof onload === "function") {
250
+ // fire the callback
251
+ onload();
252
+ }
229
253
  }
230
- else {
254
+ else if (typeof onerror === "function") {
231
255
  onerror(data.name);
232
256
  }
233
257
  }
@@ -257,8 +281,11 @@ function preloadBinary(data, onload, onerror) {
257
281
  buffer[i] = String.fromCharCode(byteArray[i]);
258
282
  }
259
283
  binList[data.name] = buffer.join("");
260
- // callback
261
- onload();
284
+ if (typeof onload === "function") {
285
+ // callback
286
+ onload();
287
+ }
288
+
262
289
  }
263
290
  };
264
291
  httpReq.send();
@@ -278,15 +305,19 @@ function preloadJavascript(data, onload, onerror) {
278
305
  }
279
306
  script.defer = true;
280
307
 
281
- script.onload = () => {
282
- // callback
283
- onload();
284
- };
308
+ if (typeof onload === "function") {
309
+ script.onload = () => {
310
+ // callback
311
+ onload();
312
+ };
313
+ }
285
314
 
286
- script.onerror = () => {
287
- // callback
288
- onerror(data.name);
289
- };
315
+ if (typeof onerror === "function") {
316
+ script.onerror = () => {
317
+ // callback
318
+ onerror(data.name);
319
+ };
320
+ }
290
321
 
291
322
  document.getElementsByTagName("body")[0].appendChild(script);
292
323
  };
@@ -453,6 +484,8 @@ var loader = {
453
484
  * {name: "tileset-platformer", type: "image", src: "data/map/tileset.png"},
454
485
  * // PNG packed texture
455
486
  * {name: "texture", type:"image", src: "data/gfx/texture.png"}
487
+ * // PNG base64 encoded image
488
+ * {name: "texture", type:"image", src: "data:image/png;base64,iVBORw0KAAAQAAAAEACA..."}
456
489
  * // TSX file
457
490
  * {name: "meta_tiles", type: "tsx", src: "data/map/meta_tiles.tsx"},
458
491
  * // TMX level (XML & JSON)
@@ -463,6 +496,8 @@ var loader = {
463
496
  * // audio resources
464
497
  * {name: "bgmusic", type: "audio", src: "data/audio/"},
465
498
  * {name: "cling", type: "audio", src: "data/audio/"},
499
+ * // base64 encoded audio resources
500
+ * {name: "band", type: "audio", src: "data:audio/wav;base64,..."},
466
501
  * // binary file
467
502
  * {name: "ymTrack", type: "binary", src: "data/audio/main.ym"},
468
503
  * // JSON file (used for texturePacker)
@@ -509,13 +544,14 @@ var loader = {
509
544
  * @param {string} res.type "audio", binary", "image", "json", "tmx", "tsx"
510
545
  * @param {string} res.src path and/or file name of the resource (for audio assets only the path is required)
511
546
  * @param {boolean} [res.stream] Set to true to force HTML5 Audio, which allows not to wait for large file to be downloaded before playing.
512
- * @param {Function} onload function to be called when the resource is loaded
513
- * @param {Function} onerror function to be called in case of error
547
+ * @param {Function} [onload] function to be called when the resource is loaded
548
+ * @param {Function} [onerror] function to be called in case of error
514
549
  * @returns {number} the amount of corresponding resource to be preloaded
515
550
  * @example
516
551
  * // load an image asset
517
552
  * me.loader.load({name: "avatar", type:"image", src: "data/avatar.png"}, this.onload.bind(this), this.onerror.bind(this));
518
- *
553
+ * // load a base64 image asset
554
+ * me.loader.load({name: "avatar", type:"image", src: "data:image/png;base64,iVBORw0KAAAQAAAAEACA..."};
519
555
  * // start loading music
520
556
  * me.loader.load({
521
557
  * name : "bgmusic",
@@ -1,10 +1,11 @@
1
- import { world, viewport } from "./../game.js";
1
+ import game from "./../game.js";
2
2
  import { renderer } from "./../video/video.js";
3
3
  import * as event from "./../system/event.js";
4
- import Renderable from "./../renderable/renderable.js";
5
4
  import Sprite from "./../renderable/sprite.js";
5
+ import Renderable from "./../renderable/renderable.js";
6
6
  import Stage from "./../state/stage.js";
7
- import melonjs_logo from "./melonjs_logo.png";
7
+ import loader from "./loader.js";
8
+ import logo_url from "./melonjs_logo.png";
8
9
 
9
10
 
10
11
  // a basic progress bar object
@@ -40,7 +41,7 @@ class ProgressBar extends Renderable {
40
41
  * draw function
41
42
  * @ignore
42
43
  */
43
- draw (renderer) {
44
+ draw(renderer, viewport) {
44
45
  // draw the progress bar
45
46
  renderer.setColor("black");
46
47
  renderer.fillRect(this.pos.x, viewport.centerY, renderer.getWidth(), this.barHeight / 2);
@@ -61,23 +62,6 @@ class ProgressBar extends Renderable {
61
62
 
62
63
  };
63
64
 
64
- /**
65
- * the melonJS Logo
66
- * @ignore
67
- */
68
- class IconLogo extends Sprite {
69
- constructor(x, y) {
70
- // TODO: create a sprite or texture from a Base64 encoded image
71
- var image = new Image();
72
- image.src = melonjs_logo;
73
- super(x, y, {
74
- image : image,
75
- framewidth : 256,
76
- frameheight : 256
77
- });
78
- }
79
- }
80
-
81
65
  /**
82
66
  * a default loading screen
83
67
  * @ignore
@@ -91,22 +75,37 @@ class DefaultLoadingScreen extends Stage {
91
75
  var barHeight = 8;
92
76
 
93
77
  // set a background color
94
- world.backgroundColor.parseCSS("#202020");
78
+ game.world.backgroundColor.parseCSS("#202020");
95
79
 
96
80
  // progress bar
97
- world.addChild(new ProgressBar(
81
+ game.world.addChild(new ProgressBar(
98
82
  0,
99
83
  renderer.getHeight() / 2,
100
84
  renderer.getWidth(),
101
85
  barHeight
102
86
  ), 1);
103
87
 
104
- // melonJS logo
105
- world.addChild(new IconLogo(
106
- renderer.getWidth() / 2,
107
- (renderer.getHeight() / 2)
88
+ // load the melonJS logo
89
+ loader.load({name: "melonjs_logo", type: "image", src: logo_url}, () => {
90
+ // melonJS logo
91
+ game.world.addChild(new Sprite(
92
+ renderer.getWidth() / 2,
93
+ renderer.getHeight() / 2, {
94
+ image : "melonjs_logo",
95
+ framewidth : 256,
96
+ frameheight : 256
97
+ }), 2
98
+ );
99
+ });
100
+ }
108
101
 
109
- ), 2);
102
+ /**
103
+ * Called by engine before deleting the object
104
+ * @ignore
105
+ */
106
+ onDestroyEvent() {
107
+ // cancel the callback
108
+ loader.unload({name: "melonjs_logo", type:"image"});
110
109
  }
111
110
  };
112
111