melonjs 10.7.1 → 10.10.0

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 (74) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +29 -23
  3. package/dist/melonjs.js +2220 -1070
  4. package/dist/melonjs.min.js +4 -4
  5. package/dist/melonjs.module.d.ts +1395 -485
  6. package/dist/melonjs.module.js +2244 -1131
  7. package/package.json +17 -14
  8. package/src/camera/camera2d.js +1 -1
  9. package/src/entity/entity.js +6 -7
  10. package/src/game.js +2 -2
  11. package/src/geometries/ellipse.js +20 -21
  12. package/src/geometries/line.js +7 -7
  13. package/src/geometries/path2d.js +319 -0
  14. package/src/geometries/poly.js +27 -27
  15. package/src/geometries/rectangle.js +19 -19
  16. package/src/geometries/roundrect.js +164 -0
  17. package/src/index.js +12 -2
  18. package/src/input/gamepad.js +2 -2
  19. package/src/input/pointerevent.js +1 -1
  20. package/src/lang/deprecated.js +8 -6
  21. package/src/level/tiled/TMXLayer.js +1 -1
  22. package/src/level/tiled/TMXObject.js +9 -12
  23. package/src/level/tiled/TMXTileMap.js +23 -4
  24. package/src/level/tiled/TMXUtils.js +1 -1
  25. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
  26. package/src/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
  27. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  28. package/src/level/tiled/renderer/TMXRenderer.js +1 -1
  29. package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
  30. package/src/loader/loader.js +4 -4
  31. package/src/loader/loadingscreen.js +17 -6
  32. package/src/math/color.js +6 -5
  33. package/src/math/matrix2.js +1 -1
  34. package/src/math/matrix3.js +1 -1
  35. package/src/math/observable_vector2.js +1 -1
  36. package/src/math/observable_vector3.js +1 -1
  37. package/src/math/vector2.js +1 -1
  38. package/src/math/vector3.js +1 -1
  39. package/src/particles/emitter.js +34 -26
  40. package/src/particles/particle.js +3 -2
  41. package/src/physics/body.js +67 -51
  42. package/src/physics/bounds.js +8 -9
  43. package/src/physics/world.js +1 -1
  44. package/src/polyfill/index.js +1 -0
  45. package/src/polyfill/roundrect.js +235 -0
  46. package/src/renderable/GUI.js +5 -5
  47. package/src/renderable/collectable.js +9 -2
  48. package/src/renderable/colorlayer.js +1 -1
  49. package/src/renderable/container.js +27 -27
  50. package/src/renderable/imagelayer.js +3 -3
  51. package/src/renderable/light2d.js +115 -0
  52. package/src/renderable/renderable.js +23 -22
  53. package/src/renderable/sprite.js +15 -16
  54. package/src/renderable/trigger.js +10 -4
  55. package/src/state/stage.js +73 -3
  56. package/src/state/state.js +1 -1
  57. package/src/system/device.js +10 -8
  58. package/src/system/pooling.js +156 -149
  59. package/src/text/bitmaptext.js +1 -1
  60. package/src/text/text.js +14 -18
  61. package/src/utils/utils.js +2 -2
  62. package/src/video/canvas/canvas_renderer.js +144 -81
  63. package/src/video/renderer.js +64 -37
  64. package/src/video/{texture.js → texture/atlas.js} +8 -8
  65. package/src/video/{texture_cache.js → texture/cache.js} +4 -4
  66. package/src/video/texture/canvas_texture.js +87 -0
  67. package/src/video/webgl/glshader.js +29 -193
  68. package/src/video/webgl/utils/attributes.js +16 -0
  69. package/src/video/webgl/utils/precision.js +11 -0
  70. package/src/video/webgl/utils/program.js +58 -0
  71. package/src/video/webgl/utils/string.js +16 -0
  72. package/src/video/webgl/utils/uniforms.js +87 -0
  73. package/src/video/webgl/webgl_compositor.js +1 -14
  74. package/src/video/webgl/webgl_renderer.js +191 -231
@@ -0,0 +1,115 @@
1
+ import pool from "./../system/pooling.js";
2
+ import Renderable from "./renderable.js";
3
+
4
+ /** @ignore */
5
+ function createGradient(light) {
6
+ var context = light.texture.context;
7
+ var x1 = light.texture.width / 2;
8
+ var y1 = light.texture.height / 2;
9
+ var gradient = context.createRadialGradient(x1, y1, 0, x1, y1, light.radius);
10
+
11
+ light.texture.clear();
12
+
13
+ gradient.addColorStop( 0, light.color.toRGBA(light.intensity));
14
+ gradient.addColorStop( 1, light.color.toRGBA(0.0));
15
+
16
+ context.beginPath();
17
+ context.fillStyle = gradient;
18
+ context.arc(x1, y1, light.radius, 0, Math.PI * 2, false);
19
+ context.fill();
20
+ }
21
+
22
+ /**
23
+ * @classdesc
24
+ * A 2D point light.
25
+ * Note: this is a very experimental and work in progress feature, that provides a simple spot light effect.
26
+ * The light effect is best rendered in WebGL, as they are few limitations when using the Canvas Renderer
27
+ * (multiple lights are not supported, alpha component of the ambient light is ignored)
28
+ * @see stage.lights
29
+ */
30
+ class Light2d extends Renderable {
31
+ /**
32
+ * @param {number} x - The horizontal position of the light.
33
+ * @param {number} y - The vertical position of the light.
34
+ * @param {number} radius - The radius of the light.
35
+ * @param {Color|string} [color="#FFF"] the color of the light
36
+ * @param {number} [intensity=0.7] - The intensity of the light.
37
+ */
38
+ constructor(x, y, radius, color = "#FFF", intensity = 0.7) {
39
+ // call the parent constructor
40
+ super(x, y, radius * 2, radius * 2);
41
+
42
+ /**
43
+ * the color of the light
44
+ * @type {Color}
45
+ * @default "#FFF"
46
+ */
47
+ this.color = pool.pull("Color").parseCSS(color);
48
+
49
+ /**
50
+ * The radius of the light
51
+ * @type {number}
52
+ */
53
+ this.radius = radius;
54
+
55
+ /**
56
+ * The intensity of the light
57
+ * @type {number}
58
+ * @default 0.7
59
+ */
60
+ this.intensity = intensity;
61
+
62
+ /**
63
+ * the default blend mode to be applied when rendering this light
64
+ * @type {string}
65
+ * @default "lighter"
66
+ * @see CanvasRenderer#setBlendMode
67
+ * @see WebGLRenderer#setBlendMode
68
+ */
69
+ this.blendMode = "lighter";
70
+
71
+ /** @ignore */
72
+ this.visibleArea = pool.pull("Ellipse", this.centerX, this.centerY, this.width, this.height);
73
+
74
+ /** @ignore */
75
+ this.texture = pool.pull("CanvasTexture", this.width, this.height, false);
76
+
77
+ this.anchorPoint.set(0, 0);
78
+
79
+ createGradient(this);
80
+ }
81
+
82
+ /**
83
+ * returns a geometry representing the visible area of this light
84
+ * @name getVisibleArea
85
+ * @memberof Light2d
86
+ * @function
87
+ * @returns {Ellipse} the light visible mask
88
+ */
89
+ getVisibleArea() {
90
+ return this.visibleArea.setShape(this.getBounds().centerX, this.getBounds().centerY, this.width, this.height);
91
+ }
92
+
93
+ /**
94
+ * object draw (Called internally by the engine).
95
+ * @ignore
96
+ */
97
+ draw(renderer) {
98
+ renderer.drawImage(this.texture.canvas, this.getBounds().x, this.getBounds().y);
99
+ }
100
+
101
+ /**
102
+ * Destroy function<br>
103
+ * @ignore
104
+ */
105
+ destroy() {
106
+ pool.push(this.color);
107
+ this.color = undefined;
108
+ pool.push(this.texture);
109
+ this.texture = undefined;
110
+ pool.push(this.visibleArea);
111
+ this.visibleArea = undefined;
112
+ super.destroy();
113
+ }
114
+ };
115
+ export default Light2d;
@@ -2,7 +2,7 @@ import ObservableVector2d from "./../math/observable_vector2.js";
2
2
  import ObservableVector3d from "./../math/observable_vector3.js";
3
3
  import Rect from "./../geometries/rectangle.js";
4
4
  import Container from "./container.js";
5
- import * as pool from "./../system/pooling.js";
5
+ import pool from "./../system/pooling.js";
6
6
  import { releaseAllPointerEvents } from "./../input/input.js";
7
7
  import { clamp } from "./../math/math.js";
8
8
 
@@ -227,7 +227,7 @@ class Renderable extends Rect {
227
227
  * A mask limits rendering elements to the shape and position of the given mask object.
228
228
  * So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
229
229
  * @public
230
- * @type {Rect|Polygon|Line|Ellipse}
230
+ * @type {Rect|RoundRect|Polygon|Line|Ellipse}
231
231
  * @name mask
232
232
  * @default undefined
233
233
  * @memberof Renderable#
@@ -380,7 +380,7 @@ class Renderable extends Rect {
380
380
  /**
381
381
  * returns the bounding box for this renderable
382
382
  * @name getBounds
383
- * @memberof Renderable.prototype
383
+ * @memberof Renderable
384
384
  * @function
385
385
  * @returns {Bounds} bounding box Rectangle object
386
386
  */
@@ -401,7 +401,7 @@ class Renderable extends Rect {
401
401
  /**
402
402
  * get the renderable alpha channel value<br>
403
403
  * @name getOpacity
404
- * @memberof Renderable.prototype
404
+ * @memberof Renderable
405
405
  * @function
406
406
  * @returns {number} current opacity value between 0 and 1
407
407
  */
@@ -412,7 +412,7 @@ class Renderable extends Rect {
412
412
  /**
413
413
  * set the renderable alpha channel value<br>
414
414
  * @name setOpacity
415
- * @memberof Renderable.prototype
415
+ * @memberof Renderable
416
416
  * @function
417
417
  * @param {number} alpha opacity value between 0.0 and 1.0
418
418
  */
@@ -431,7 +431,7 @@ class Renderable extends Rect {
431
431
  * flip the renderable on the horizontal axis (around the center of the renderable)
432
432
  * @see Matrix2d#scaleX
433
433
  * @name flipX
434
- * @memberof Renderable.prototype
434
+ * @memberof Renderable
435
435
  * @function
436
436
  * @param {boolean} [flip=true] `true` to flip this renderable.
437
437
  * @returns {Renderable} Reference to this object for method chaining
@@ -446,7 +446,7 @@ class Renderable extends Rect {
446
446
  * flip the renderable on the vertical axis (around the center of the renderable)
447
447
  * @see Matrix2d#scaleY
448
448
  * @name flipY
449
- * @memberof Renderable.prototype
449
+ * @memberof Renderable
450
450
  * @function
451
451
  * @param {boolean} [flip=true] `true` to flip this renderable.
452
452
  * @returns {Renderable} Reference to this object for method chaining
@@ -460,7 +460,7 @@ class Renderable extends Rect {
460
460
  /**
461
461
  * multiply the renderable currentTransform with the given matrix
462
462
  * @name transform
463
- * @memberof Renderable.prototype
463
+ * @memberof Renderable
464
464
  * @see Renderable#currentTransform
465
465
  * @function
466
466
  * @param {Matrix2d} m the transformation matrix
@@ -525,7 +525,7 @@ class Renderable extends Rect {
525
525
  /**
526
526
  * Rotate this renderable towards the given target.
527
527
  * @name lookAt
528
- * @memberof Renderable.prototype
528
+ * @memberof Renderable
529
529
  * @function
530
530
  * @param {Renderable|Vector2d|Vector3d} target the renderable or position to look at
531
531
  * @returns {Renderable} Reference to this object for method chaining
@@ -549,7 +549,7 @@ class Renderable extends Rect {
549
549
  /**
550
550
  * Rotate this renderable by the specified angle (in radians).
551
551
  * @name rotate
552
- * @memberof Renderable.prototype
552
+ * @memberof Renderable
553
553
  * @function
554
554
  * @param {number} angle The angle to rotate (in radians)
555
555
  * @param {Vector2d|ObservableVector2d} [v] an optional point to rotate around
@@ -571,7 +571,7 @@ class Renderable extends Rect {
571
571
  * is an image, the image.width and image.height properties are unaltered but the currentTransform
572
572
  * member will be changed.
573
573
  * @name scale
574
- * @memberof Renderable.prototype
574
+ * @memberof Renderable
575
575
  * @function
576
576
  * @param {number} x a number representing the abscissa of the scaling vector.
577
577
  * @param {number} [y=x] a number representing the ordinate of the scaling vector.
@@ -587,7 +587,7 @@ class Renderable extends Rect {
587
587
  /**
588
588
  * scale the renderable around his anchor point
589
589
  * @name scaleV
590
- * @memberof Renderable.prototype
590
+ * @memberof Renderable
591
591
  * @function
592
592
  * @param {Vector2d} v scaling vector
593
593
  * @returns {Renderable} Reference to this object for method chaining
@@ -601,7 +601,7 @@ class Renderable extends Rect {
601
601
  * update function. <br>
602
602
  * automatically called by the game manager {@link game}
603
603
  * @name update
604
- * @memberof Renderable.prototype
604
+ * @memberof Renderable
605
605
  * @function
606
606
  * @protected
607
607
  * @param {number} dt time since the last update in milliseconds.
@@ -615,7 +615,7 @@ class Renderable extends Rect {
615
615
  * update the bounding box for this shape.
616
616
  * @ignore
617
617
  * @name updateBounds
618
- * @memberof Renderable.prototype
618
+ * @memberof Renderable
619
619
  * @function
620
620
  * @returns {Bounds} this shape bounding box Rectangle object
621
621
  */
@@ -629,7 +629,7 @@ class Renderable extends Rect {
629
629
  * update the renderable's bounding rect (private)
630
630
  * @ignore
631
631
  * @name updateBoundsPos
632
- * @memberof Renderable.prototype
632
+ * @memberof Renderable
633
633
  * @function
634
634
  */
635
635
  updateBoundsPos(newX, newY) {
@@ -655,13 +655,14 @@ class Renderable extends Rect {
655
655
  if (this.ancestor instanceof Container && this.floating !== true) {
656
656
  bounds.translate(this.ancestor.getAbsolutePosition());
657
657
  }
658
- //return bounds;
658
+
659
+ this.isDirty = true;
659
660
  }
660
661
 
661
662
  /**
662
663
  * return the renderable absolute position in the game world
663
664
  * @name getAbsolutePosition
664
- * @memberof Renderable.prototype
665
+ * @memberof Renderable
665
666
  * @function
666
667
  * @returns {Vector2d}
667
668
  */
@@ -681,7 +682,7 @@ class Renderable extends Rect {
681
682
  * called when the anchor point value is changed
682
683
  * @private
683
684
  * @name onAnchorUpdate
684
- * @memberof Renderable.prototype
685
+ * @memberof Renderable
685
686
  * @function
686
687
  * @param {number} x the new X value to be set for the anchor
687
688
  * @param {number} y the new Y value to be set for the anchor
@@ -699,7 +700,7 @@ class Renderable extends Rect {
699
700
  * (apply defined transforms, anchor point). <br>
700
701
  * automatically called by the game manager {@link game}
701
702
  * @name preDraw
702
- * @memberof Renderable.prototype
703
+ * @memberof Renderable
703
704
  * @function
704
705
  * @protected
705
706
  * @param {CanvasRenderer|WebGLRenderer} renderer a renderer object
@@ -755,7 +756,7 @@ class Renderable extends Rect {
755
756
  * object draw. <br>
756
757
  * automatically called by the game manager {@link game}
757
758
  * @name draw
758
- * @memberof Renderable.prototype
759
+ * @memberof Renderable
759
760
  * @function
760
761
  * @protected
761
762
  * @param {CanvasRenderer|WebGLRenderer} renderer a renderer object
@@ -768,7 +769,7 @@ class Renderable extends Rect {
768
769
  * restore the rendering context after drawing. <br>
769
770
  * automatically called by the game manager {@link game}
770
771
  * @name postDraw
771
- * @memberof Renderable.prototype
772
+ * @memberof Renderable
772
773
  * @function
773
774
  * @protected
774
775
  * @param {CanvasRenderer|WebGLRenderer} renderer a renderer object
@@ -793,7 +794,7 @@ class Renderable extends Rect {
793
794
  * onCollision callback, triggered in case of collision,
794
795
  * when this renderable body is colliding with another one
795
796
  * @name onCollision
796
- * @memberof Renderable.prototype
797
+ * @memberof Renderable
797
798
  * @function
798
799
  * @param {collision.ResponseObject} response the collision response object
799
800
  * @param {Renderable} other the other renderable touching this one (a reference to response.a or response.b)
@@ -1,8 +1,7 @@
1
- import Vector2d from "./../math/vector2.js";
2
1
  import { renderer } from "./../video/video.js";
3
- import * as pool from "./../system/pooling.js";
2
+ import pool from "./../system/pooling.js";
4
3
  import loader from "./../loader/loader.js";
5
- import { TextureAtlas } from "./../video/texture.js";
4
+ import { TextureAtlas } from "./../video/texture/atlas.js";
6
5
  import Renderable from "./renderable.js";
7
6
 
8
7
  /**
@@ -99,7 +98,7 @@ class Sprite extends Renderable {
99
98
  // length of the current animation name
100
99
  length : 0,
101
100
  //current frame texture offset
102
- offset : new Vector2d(),
101
+ offset : pool.pull("Vector2d"),
103
102
  // current frame size
104
103
  width : 0,
105
104
  height : 0,
@@ -210,7 +209,7 @@ class Sprite extends Renderable {
210
209
  /**
211
210
  * return the flickering state of the object
212
211
  * @name isFlickering
213
- * @memberof Sprite.prototype
212
+ * @memberof Sprite
214
213
  * @function
215
214
  * @returns {boolean}
216
215
  */
@@ -221,7 +220,7 @@ class Sprite extends Renderable {
221
220
  /**
222
221
  * make the object flicker
223
222
  * @name flicker
224
- * @memberof Sprite.prototype
223
+ * @memberof Sprite
225
224
  * @function
226
225
  * @param {number} duration expressed in milliseconds
227
226
  * @param {Function} callback Function to call when flickering ends
@@ -252,7 +251,7 @@ class Sprite extends Renderable {
252
251
  * logic as per the following example :<br>
253
252
  * <img src="images/spritesheet_grid.png"/>
254
253
  * @name addAnimation
255
- * @memberof Sprite.prototype
254
+ * @memberof Sprite
256
255
  * @function
257
256
  * @param {string} name animation id
258
257
  * @param {number[]|string[]|object[]} index list of sprite index or name
@@ -352,7 +351,7 @@ class Sprite extends Renderable {
352
351
  * set the current animation
353
352
  * this will always change the animation & set the frame to zero
354
353
  * @name setCurrentAnimation
355
- * @memberof Sprite.prototype
354
+ * @memberof Sprite
356
355
  * @function
357
356
  * @param {string} name animation id
358
357
  * @param {string|Function} [resetAnim] animation id to switch to when complete, or callback
@@ -413,7 +412,7 @@ class Sprite extends Renderable {
413
412
  /**
414
413
  * reverse the given or current animation if none is specified
415
414
  * @name reverseAnimation
416
- * @memberof Sprite.prototype
415
+ * @memberof Sprite
417
416
  * @function
418
417
  * @param {string} [name] animation id
419
418
  * @returns {Sprite} Reference to this object for method chaining
@@ -432,7 +431,7 @@ class Sprite extends Renderable {
432
431
  /**
433
432
  * return true if the specified animation is the current one.
434
433
  * @name isCurrentAnimation
435
- * @memberof Sprite.prototype
434
+ * @memberof Sprite
436
435
  * @function
437
436
  * @param {string} name animation id
438
437
  * @returns {boolean}
@@ -449,7 +448,7 @@ class Sprite extends Renderable {
449
448
  * change the current texture atlas region for this sprite
450
449
  * @see Texture.getRegion
451
450
  * @name setRegion
452
- * @memberof Sprite.prototype
451
+ * @memberof Sprite
453
452
  * @function
454
453
  * @param {object} region typically returned through me.Texture.getRegion()
455
454
  * @returns {Sprite} Reference to this object for method chaining
@@ -481,7 +480,7 @@ class Sprite extends Renderable {
481
480
  /**
482
481
  * force the current animation frame index.
483
482
  * @name setAnimationFrame
484
- * @memberof Sprite.prototype
483
+ * @memberof Sprite
485
484
  * @function
486
485
  * @param {number} [idx=0] animation frame index
487
486
  * @returns {Sprite} Reference to this object for method chaining
@@ -497,7 +496,7 @@ class Sprite extends Renderable {
497
496
  /**
498
497
  * return the current animation frame index.
499
498
  * @name getCurrentAnimationFrame
500
- * @memberof Sprite.prototype
499
+ * @memberof Sprite
501
500
  * @function
502
501
  * @returns {number} current animation frame index
503
502
  */
@@ -508,7 +507,7 @@ class Sprite extends Renderable {
508
507
  /**
509
508
  * Returns the frame object by the index.
510
509
  * @name getAnimationFrameObjectByIndex
511
- * @memberof Sprite.prototype
510
+ * @memberof Sprite
512
511
  * @function
513
512
  * @ignore
514
513
  * @param {number} id the frame id
@@ -522,7 +521,7 @@ class Sprite extends Renderable {
522
521
  * update function. <br>
523
522
  * automatically called by the game manager {@link game}
524
523
  * @name update
525
- * @memberof Sprite.prototype
524
+ * @memberof Sprite
526
525
  * @function
527
526
  * @protected
528
527
  * @param {number} dt time since the last update in milliseconds.
@@ -601,7 +600,7 @@ class Sprite extends Renderable {
601
600
  * sprite draw. <br>
602
601
  * automatically called by the game manager {@link game}
603
602
  * @name draw
604
- * @memberof Sprite.prototype
603
+ * @memberof Sprite
605
604
  * @function
606
605
  * @protected
607
606
  * @param {CanvasRenderer|WebGLRenderer} renderer a renderer object
@@ -1,7 +1,6 @@
1
1
  import Renderable from "./renderable.js";
2
2
  import collision from "./../physics/collision.js";
3
3
  import Body from "./../physics/body.js";
4
- import Rect from "./../geometries/rectangle.js";
5
4
  import level from "./../level/level.js";
6
5
  import { world, viewport } from "./../game.js";
7
6
 
@@ -66,9 +65,16 @@ class Trigger extends Renderable {
66
65
  }
67
66
  }.bind(this));
68
67
 
69
-
70
- // physic body to check for collision against
71
- this.body = new Body(this, settings.shapes || new Rect(0, 0, this.width, this.height));
68
+ // add and configure the physic body
69
+ var shape = settings.shapes;
70
+ if (typeof shape === "undefined") {
71
+ shape = pool.pull("Polygon", 0, 0, [
72
+ pool.pull("Vector2d", 0, 0),
73
+ pool.pull("Vector2d", this.width, 0),
74
+ pool.pull("Vector2d", this.width, this.height)
75
+ ]);
76
+ }
77
+ this.body = new Body(this, shape);
72
78
  this.body.collisionType = collision.types.ACTION_OBJECT;
73
79
  // by default only collides with PLAYER_OBJECT
74
80
  this.body.setCollisionMask(collision.types.PLAYER_OBJECT);
@@ -1,6 +1,7 @@
1
1
  import { renderer } from "./../video/video.js";
2
2
  import * as game from "./../game.js";
3
3
  import Camera2d from "./../camera/camera2d.js";
4
+ import Color from "./../math/color.js";
4
5
 
5
6
  // a default camera instance to use across all stages
6
7
  var default_camera;
@@ -31,12 +32,46 @@ class Stage {
31
32
  * Cameras will be renderered based on this order defined in this list.
32
33
  * Only the "default" camera will be resized when the window or canvas is resized.
33
34
  * @public
34
- * @type {Map}
35
+ * @type {Map<Camera2d>}
35
36
  * @name cameras
36
37
  * @memberof Stage
37
38
  */
38
39
  this.cameras = new Map();
39
40
 
41
+ /**
42
+ * The list of active lights in this stage.
43
+ * (Note: Canvas Renderering mode will only properly support one light per stage)
44
+ * @public
45
+ * @type {Map<Light2d>}
46
+ * @name lights
47
+ * @memberof Stage
48
+ * @see Light2d
49
+ * @see Stage.ambientLight
50
+ * @example
51
+ * // create a white spot light
52
+ * var whiteLight = new me.Light2d(0, 0, 140, "#fff", 0.7);
53
+ * // and add the light to this current stage
54
+ * this.lights.set("whiteLight", whiteLight);
55
+ * // set a dark ambient light
56
+ * this.ambientLight.parseCSS("#1117");
57
+ * // make the light follow the mouse
58
+ * me.input.registerPointerEvent("pointermove", me.game.viewport, (event) => {
59
+ * whiteLight.centerOn(event.gameX, event.gameY);
60
+ * });
61
+ */
62
+ this.lights = new Map();
63
+
64
+ /**
65
+ * an ambient light that will be added to the stage rendering
66
+ * @public
67
+ * @type {Color}
68
+ * @name ambientLight
69
+ * @memberof Stage
70
+ * @default "#000000"
71
+ * @see Light2d
72
+ */
73
+ this.ambientLight = new Color(0, 0, 0, 0);
74
+
40
75
  /**
41
76
  * The given constructor options
42
77
  * @public
@@ -92,7 +127,14 @@ class Stage {
92
127
  // update the camera/viewport
93
128
  // iterate through all cameras
94
129
  this.cameras.forEach(function(camera) {
95
- if (camera.update(dt)) {
130
+ if (camera.update(dt) === true) {
131
+ isDirty = true;
132
+ };
133
+ });
134
+
135
+ // update all lights
136
+ this.lights.forEach((light) => {
137
+ if (light.update(dt) === true) {
96
138
  isDirty = true;
97
139
  };
98
140
  });
@@ -110,9 +152,32 @@ class Stage {
110
152
  */
111
153
  draw(renderer) {
112
154
  // iterate through all cameras
113
- this.cameras.forEach(function(camera) {
155
+ this.cameras.forEach((camera) => {
114
156
  // render the root container
115
157
  camera.draw(renderer, game.world);
158
+
159
+ // render the ambient light
160
+ if (this.ambientLight.alpha !== 0) {
161
+ renderer.save();
162
+ // iterate through all lights
163
+ this.lights.forEach((light) => {
164
+ // cut out all lights visible areas
165
+ renderer.setMask(light.getVisibleArea(), true);
166
+ });
167
+ // fill the screen with the ambient color
168
+ renderer.setColor(this.ambientLight);
169
+ renderer.fillRect(0, 0, camera.width, camera.height);
170
+ // clear all masks
171
+ renderer.clearMask();
172
+ renderer.restore();
173
+ }
174
+
175
+ // render all lights
176
+ this.lights.forEach((light) => {
177
+ light.preDraw(renderer, game.world);
178
+ light.draw(renderer, game.world);
179
+ light.postDraw(renderer, game.world);
180
+ });
116
181
  });
117
182
  }
118
183
 
@@ -123,6 +188,11 @@ class Stage {
123
188
  destroy() {
124
189
  // clear all cameras
125
190
  this.cameras.clear();
191
+ // clear all lights
192
+ this.lights.forEach((light) => {
193
+ light.destroy();
194
+ });
195
+ this.lights.clear();
126
196
  // notify the object
127
197
  this.onDestroyEvent.apply(this, arguments);
128
198
  }
@@ -512,7 +512,7 @@ var state = {
512
512
  // if fading effect
513
513
  if (_fade.duration && _stages[state].transition) {
514
514
  /** @ignore */
515
- _onSwitchComplete = function() {
515
+ _onSwitchComplete = () => {
516
516
  game.viewport.fadeOut(_fade.color, _fade.duration);
517
517
  };
518
518
  game.viewport.fadeIn(
@@ -112,12 +112,6 @@ function _checkCapabilities() {
112
112
  // detect device type/platform
113
113
  _detectDevice();
114
114
 
115
- // Mobile browser hacks
116
- if (device.isMobile) {
117
- // Prevent the webview from moving on a swipe
118
- device.enableSwipe(false);
119
- }
120
-
121
115
  // Touch/Gesture Event feature detection
122
116
  device.TouchEvent = !!("ontouchstart" in globalThis);
123
117
  device.PointerEvent = !!globalThis.PointerEvent;
@@ -239,6 +233,13 @@ function _checkCapabilities() {
239
233
  );
240
234
  }
241
235
  }
236
+
237
+ // Mobile browser hacks
238
+ if (device.isMobile) {
239
+ // Prevent the webview from moving on a swipe
240
+ device.enableSwipe(false);
241
+ }
242
+
242
243
  };
243
244
 
244
245
 
@@ -676,13 +677,14 @@ let device = {
676
677
  * @param {boolean} [enable=true] enable or disable swipe.
677
678
  */
678
679
  enableSwipe(enable) {
680
+ var moveEvent = device.PointerEvent ? "pointermove" : (device.TouchEvent ? "touchmove" : "mousemove");
679
681
  if (enable !== false) {
680
682
  if (swipeEnabled === false) {
681
- globalThis.document.removeEventListener("touchmove", _disableSwipeFn, false);
683
+ globalThis.document.removeEventListener(moveEvent, _disableSwipeFn);
682
684
  swipeEnabled = true;
683
685
  }
684
686
  } else if (swipeEnabled === true) {
685
- globalThis.document.addEventListener("touchmove", _disableSwipeFn, false);
687
+ globalThis.document.addEventListener(moveEvent, _disableSwipeFn, { passive: false });
686
688
  swipeEnabled = false;
687
689
  }
688
690
  },