melonjs 10.7.0 → 10.9.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 (66) hide show
  1. package/dist/melonjs.js +1488 -666
  2. package/dist/melonjs.min.js +4 -4
  3. package/dist/melonjs.module.d.ts +929 -202
  4. package/dist/melonjs.module.js +1575 -777
  5. package/package.json +9 -9
  6. package/src/camera/camera2d.js +1 -1
  7. package/src/entity/entity.js +6 -7
  8. package/src/geometries/ellipse.js +10 -11
  9. package/src/geometries/line.js +3 -3
  10. package/src/geometries/path2d.js +319 -0
  11. package/src/geometries/poly.js +11 -11
  12. package/src/geometries/rectangle.js +15 -15
  13. package/src/geometries/roundrect.js +164 -0
  14. package/src/index.js +5 -1
  15. package/src/input/gamepad.js +2 -2
  16. package/src/input/pointerevent.js +1 -1
  17. package/src/lang/deprecated.js +1 -1
  18. package/src/level/tiled/TMXLayer.js +1 -1
  19. package/src/level/tiled/TMXObject.js +9 -12
  20. package/src/level/tiled/TMXTileMap.js +23 -4
  21. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
  22. package/src/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
  23. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  24. package/src/level/tiled/renderer/TMXRenderer.js +1 -1
  25. package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
  26. package/src/loader/loader.js +4 -4
  27. package/src/loader/loadingscreen.js +1 -1
  28. package/src/math/color.js +1 -1
  29. package/src/math/matrix2.js +1 -1
  30. package/src/math/matrix3.js +1 -1
  31. package/src/math/observable_vector2.js +1 -1
  32. package/src/math/observable_vector3.js +1 -1
  33. package/src/math/vector2.js +1 -1
  34. package/src/math/vector3.js +1 -1
  35. package/src/particles/emitter.js +23 -14
  36. package/src/particles/particle.js +3 -2
  37. package/src/physics/body.js +67 -51
  38. package/src/physics/bounds.js +8 -9
  39. package/src/physics/world.js +1 -1
  40. package/src/polyfill/index.js +1 -0
  41. package/src/polyfill/roundrect.js +235 -0
  42. package/src/renderable/collectable.js +9 -2
  43. package/src/renderable/colorlayer.js +1 -1
  44. package/src/renderable/container.js +1 -1
  45. package/src/renderable/imagelayer.js +1 -1
  46. package/src/renderable/renderable.js +2 -2
  47. package/src/renderable/sprite.js +2 -3
  48. package/src/renderable/trigger.js +10 -4
  49. package/src/state/stage.js +1 -1
  50. package/src/state/state.js +1 -1
  51. package/src/system/device.js +10 -8
  52. package/src/system/pooling.js +156 -149
  53. package/src/text/bitmaptext.js +1 -1
  54. package/src/text/text.js +1 -1
  55. package/src/utils/utils.js +2 -2
  56. package/src/video/canvas/canvas_renderer.js +83 -39
  57. package/src/video/renderer.js +36 -16
  58. package/src/video/texture.js +1 -1
  59. package/src/video/webgl/glshader.js +29 -193
  60. package/src/video/webgl/utils/attributes.js +16 -0
  61. package/src/video/webgl/utils/precision.js +11 -0
  62. package/src/video/webgl/utils/program.js +58 -0
  63. package/src/video/webgl/utils/string.js +16 -0
  64. package/src/video/webgl/utils/uniforms.js +87 -0
  65. package/src/video/webgl/webgl_compositor.js +1 -14
  66. package/src/video/webgl/webgl_renderer.js +124 -182
@@ -1,5 +1,4 @@
1
1
  import Color from "./../../math/color.js";
2
- import Vector2d from "./../../math/vector2.js";
3
2
  import Matrix2d from "./../../math/matrix2.js";
4
3
  import WebGLCompositor from "./webgl_compositor.js";
5
4
  import Renderer from "./../renderer.js";
@@ -7,8 +6,8 @@ import TextureCache from "./../texture_cache.js";
7
6
  import { TextureAtlas, createAtlas } from "./../texture.js";
8
7
  import { createCanvas, renderer } from "./../video.js";
9
8
  import * as event from "./../../system/event.js";
10
- import * as pool from "./../../system/pooling.js";
11
- import { isPowerOfTwo, nextPowerOfTwo, TAU } from "./../../math/math.js";
9
+ import pool from "./../../system/pooling.js";
10
+ import { isPowerOfTwo, nextPowerOfTwo } from "./../../math/math.js";
12
11
 
13
12
 
14
13
  /**
@@ -41,7 +40,7 @@ class WebGLRenderer extends Renderer {
41
40
  /**
42
41
  * The WebGL version used by this renderer (1 or 2)
43
42
  * @name WebGLVersion
44
- * @memberof WebGLRenderer
43
+ * @memberof WebGLRenderer#
45
44
  * @type {number}
46
45
  * @default 1
47
46
  * @readonly
@@ -51,7 +50,7 @@ class WebGLRenderer extends Renderer {
51
50
  /**
52
51
  * The vendor string of the underlying graphics driver.
53
52
  * @name GPUVendor
54
- * @memberof WebGLRenderer
53
+ * @memberof WebGLRenderer#
55
54
  * @type {string}
56
55
  * @default null
57
56
  * @readonly
@@ -61,7 +60,7 @@ class WebGLRenderer extends Renderer {
61
60
  /**
62
61
  * The renderer string of the underlying graphics driver.
63
62
  * @name GPURenderer
64
- * @memberof WebGLRenderer
63
+ * @memberof WebGLRenderer#
65
64
  * @type {string}
66
65
  * @default null
67
66
  * @readonly
@@ -71,7 +70,7 @@ class WebGLRenderer extends Renderer {
71
70
  /**
72
71
  * The WebGL context
73
72
  * @name gl
74
- * @memberof WebGLRenderer
73
+ * @memberof WebGLRenderer#
75
74
  * type {WebGLRenderingContext}
76
75
  */
77
76
  this.context = this.gl = this.getContextGL(this.getScreenCanvas(), options.transparent);
@@ -79,7 +78,7 @@ class WebGLRenderer extends Renderer {
79
78
  /**
80
79
  * Maximum number of texture unit supported under the current context
81
80
  * @name maxTextures
82
- * @memberof WebGLRenderer
81
+ * @memberof WebGLRenderer#
83
82
  * @type {number}
84
83
  * @readonly
85
84
  */
@@ -105,16 +104,6 @@ class WebGLRenderer extends Renderer {
105
104
  */
106
105
  this._blendStack = [];
107
106
 
108
- /**
109
- * @ignore
110
- */
111
- this._glPoints = [
112
- new Vector2d(),
113
- new Vector2d(),
114
- new Vector2d(),
115
- new Vector2d()
116
- ];
117
-
118
107
  /**
119
108
  * The current transformation matrix used for transformations on the overall scene
120
109
  * @name currentTransform
@@ -134,7 +123,7 @@ class WebGLRenderer extends Renderer {
134
123
  /**
135
124
  * The list of active compositors
136
125
  * @name compositors
137
- * @type {Map}
126
+ * @type {Map<WebGLCompositor>}
138
127
  * @memberof WebGLRenderer#
139
128
  */
140
129
  this.compositors = new Map();
@@ -721,6 +710,17 @@ class WebGLRenderer extends Renderer {
721
710
  this.currentColor.alpha = alpha;
722
711
  }
723
712
 
713
+ /**
714
+ * Return the global alpha
715
+ * @name getGlobalAlpha
716
+ * @memberof WebGLRenderer.prototype
717
+ * @function
718
+ * @returns {number} global alpha value
719
+ */
720
+ getGlobalAlpha() {
721
+ return this.currentColor.alpha;
722
+ }
723
+
724
724
  /**
725
725
  * Set the current fill & stroke style color.
726
726
  * By default, or upon reset, the value is set to #000000.
@@ -759,34 +759,18 @@ class WebGLRenderer extends Renderer {
759
759
  * @param {boolean} [antiClockwise=false] draw arc anti-clockwise
760
760
  * @param {boolean} [fill=false]
761
761
  */
762
- strokeArc(x, y, radius, start, end, antiClockwise = false, fill) {
763
- if (fill === true ) {
764
- this.fillArc(x, y, radius, start, end, antiClockwise);
762
+ strokeArc(x, y, radius, start, end, antiClockwise = false, fill = false) {
763
+ if (this.getGlobalAlpha() < 1 / 255) {
764
+ // Fast path: don't draw fully transparent
765
+ return;
766
+ }
767
+ this.path2D.beginPath();
768
+ this.path2D.arc(x, y, radius, start, end, antiClockwise);
769
+ if (fill === false) {
770
+ this.currentCompositor.drawVertices(this.gl.LINE_STRIP, this.path2D.points);
765
771
  } else {
766
- // XXX to be optimzed using a specific shader
767
- var points = this._glPoints;
768
- var i, len = Math.floor(24 * Math.sqrt(radius * 2));
769
- var theta = (end - start) / (len * 2);
770
- var theta2 = theta * 2;
771
- var cos_theta = Math.cos(theta);
772
- var sin_theta = Math.sin(theta);
773
-
774
- // Grow internal points buffer if necessary
775
- for (i = points.length; i < len + 1; i++) {
776
- points.push(new Vector2d());
777
- }
778
-
779
- // calculate and draw all segments
780
- for (i = 0; i < len; i++) {
781
- var angle = ((theta) + start + (theta2 * i));
782
- var cos = Math.cos(angle);
783
- var sin = -Math.sin(angle);
784
-
785
- points[i].x = x + (((cos_theta * cos) + (sin_theta * sin)) * radius);
786
- points[i].y = y + (((cos_theta * -sin) + (sin_theta * cos)) * radius);
787
- }
788
- // batch draw all lines
789
- this.currentCompositor.drawVertices(this.gl.LINE_STRIP, points, len);
772
+ this.path2D.closePath();
773
+ this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
790
774
  }
791
775
  }
792
776
 
@@ -802,35 +786,8 @@ class WebGLRenderer extends Renderer {
802
786
  * @param {number} end end angle in radians
803
787
  * @param {boolean} [antiClockwise=false] draw arc anti-clockwise
804
788
  */
805
- fillArc(x, y, radius, start, end /*, antiClockwise = false*/) {
806
- // XXX to be optimzed using a specific shader
807
- var points = this._glPoints;
808
- var i, index = 0;
809
- var len = Math.floor(24 * Math.sqrt(radius * 2));
810
- var theta = (end - start) / (len * 2);
811
- var theta2 = theta * 2;
812
- var cos_theta = Math.cos(theta);
813
- var sin_theta = Math.sin(theta);
814
-
815
- // Grow internal points buffer if necessary
816
- for (i = points.length; i < len * 2; i++) {
817
- points.push(new Vector2d());
818
- }
819
-
820
- // calculate and draw all segments
821
- for (i = 0; i < len - 1; i++) {
822
- var angle = ((theta) + start + (theta2 * i));
823
- var cos = Math.cos(angle);
824
- var sin = -Math.sin(angle);
825
-
826
- points[index++].set(x, y);
827
- points[index++].set(
828
- x - (((cos_theta * cos) + (sin_theta * sin)) * radius),
829
- y - (((cos_theta * -sin) + (sin_theta * cos)) * radius)
830
- );
831
- }
832
- // batch draw all triangles
833
- this.currentCompositor.drawVertices(this.gl.TRIANGLE_STRIP, points, index);
789
+ fillArc(x, y, radius, start, end, antiClockwise = false) {
790
+ this.strokeArc(x, y, radius, start, end, antiClockwise, true);
834
791
  }
835
792
 
836
793
  /**
@@ -845,28 +802,17 @@ class WebGLRenderer extends Renderer {
845
802
  * @param {boolean} [fill=false] also fill the shape with the current color if true
846
803
  */
847
804
  strokeEllipse(x, y, w, h, fill = false) {
848
- if (fill === true ) {
849
- this.fillEllipse(x, y, w, h);
805
+ if (this.getGlobalAlpha() < 1 / 255) {
806
+ // Fast path: don't draw fully transparent
807
+ return;
808
+ }
809
+ this.path2D.beginPath();
810
+ this.path2D.ellipse(x, y, w, h, 0, 0, 360);
811
+ this.path2D.closePath();
812
+ if (fill === false) {
813
+ this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
850
814
  } else {
851
- // XXX to be optimzed using a specific shader
852
- var len = Math.floor(24 * Math.sqrt(w)) ||
853
- Math.floor(12 * Math.sqrt(w + h));
854
- var segment = (TAU) / len;
855
- var points = this._glPoints,
856
- i;
857
-
858
- // Grow internal points buffer if necessary
859
- for (i = points.length; i < len; i++) {
860
- points.push(new Vector2d());
861
- }
862
-
863
- // calculate and draw all segments
864
- for (i = 0; i < len; i++) {
865
- points[i].x = x + (Math.sin(segment * -i) * w);
866
- points[i].y = y + (Math.cos(segment * -i) * h);
867
- }
868
- // batch draw all lines
869
- this.currentCompositor.drawVertices(this.gl.LINE_LOOP, points, len);
815
+ this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
870
816
  }
871
817
  }
872
818
 
@@ -881,28 +827,7 @@ class WebGLRenderer extends Renderer {
881
827
  * @param {number} h vertical radius of the ellipse
882
828
  */
883
829
  fillEllipse(x, y, w, h) {
884
- // XXX to be optimzed using a specific shader
885
- var len = Math.floor(24 * Math.sqrt(w)) ||
886
- Math.floor(12 * Math.sqrt(w + h));
887
- var segment = (TAU) / len;
888
- var points = this._glPoints;
889
- var index = 0, i;
890
-
891
- // Grow internal points buffer if necessary
892
- for (i = points.length; i < (len + 1) * 2; i++) {
893
- points.push(new Vector2d());
894
- }
895
-
896
- // draw all vertices vertex coordinates
897
- for (i = 0; i < len + 1; i++) {
898
- points[index++].set(x, y);
899
- points[index++].set(
900
- x + (Math.sin(segment * i) * w),
901
- y + (Math.cos(segment * i) * h)
902
- );
903
- }
904
- // batch draw all triangles
905
- this.currentCompositor.drawVertices(this.gl.TRIANGLE_STRIP, points, index);
830
+ this.strokeEllipse(x, y, w, h, false);
906
831
  }
907
832
 
908
833
  /**
@@ -916,12 +841,14 @@ class WebGLRenderer extends Renderer {
916
841
  * @param {number} endY the end y coordinate
917
842
  */
918
843
  strokeLine(startX, startY, endX, endY) {
919
- var points = this._glPoints;
920
- points[0].x = startX;
921
- points[0].y = startY;
922
- points[1].x = endX;
923
- points[1].y = endY;
924
- this.currentCompositor.drawVertices(this.gl.LINE_STRIP, points, 2);
844
+ if (this.getGlobalAlpha() < 1 / 255) {
845
+ // Fast path: don't draw fully transparent
846
+ return;
847
+ }
848
+ this.path2D.beginPath();
849
+ this.path2D.moveTo(startX, startY);
850
+ this.path2D.lineTo(endX, endY);
851
+ this.currentCompositor.drawVertices(this.gl.LINE_STRIP, this.path2D.points);
925
852
  }
926
853
 
927
854
 
@@ -948,25 +875,27 @@ class WebGLRenderer extends Renderer {
948
875
  * @param {boolean} [fill=false] also fill the shape with the current color if true
949
876
  */
950
877
  strokePolygon(poly, fill = false) {
951
- if (fill === true ) {
952
- this.fillPolygon(poly);
878
+ if (this.getGlobalAlpha() < 1 / 255) {
879
+ // Fast path: don't draw fully transparent
880
+ return;
881
+ }
882
+ this.translate(poly.pos.x, poly.pos.y);
883
+ this.path2D.beginPath();
884
+ this.path2D.moveTo(poly.points[0].x, poly.points[0].y);
885
+ var point;
886
+ for (var i = 1; i < poly.points.length; i++) {
887
+ point = poly.points[i];
888
+ this.path2D.lineTo(point.x, point.y);
889
+ }
890
+ this.path2D.lineTo(poly.points[0].x, poly.points[0].y);
891
+ this.path2D.closePath();
892
+ if (fill === false) {
893
+ this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
953
894
  } else {
954
- var len = poly.points.length,
955
- points = this._glPoints,
956
- i;
957
-
958
- // Grow internal points buffer if necessary
959
- for (i = points.length; i < len; i++) {
960
- points.push(new Vector2d());
961
- }
962
-
963
- // calculate and draw all segments
964
- for (i = 0; i < len; i++) {
965
- points[i].x = poly.pos.x + poly.points[i].x;
966
- points[i].y = poly.pos.y + poly.points[i].y;
967
- }
968
- this.currentCompositor.drawVertices(this.gl.LINE_LOOP, points, len);
895
+ // draw all triangles
896
+ this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
969
897
  }
898
+ this.translate(-poly.pos.x, -poly.pos.y);
970
899
  }
971
900
 
972
901
  /**
@@ -977,24 +906,7 @@ class WebGLRenderer extends Renderer {
977
906
  * @param {Polygon} poly the shape to draw
978
907
  */
979
908
  fillPolygon(poly) {
980
- var points = poly.points;
981
- var glPoints = this._glPoints;
982
- var indices = poly.getIndices();
983
- var x = poly.pos.x, y = poly.pos.y;
984
- var i;
985
-
986
- // Grow internal points buffer if necessary
987
- for (i = glPoints.length; i < indices.length; i++) {
988
- glPoints.push(new Vector2d());
989
- }
990
-
991
- // calculate all vertices
992
- for (i = 0; i < indices.length; i++ ) {
993
- glPoints[i].set(x + points[indices[i]].x, y + points[indices[i]].y);
994
- }
995
-
996
- // draw all triangle
997
- this.currentCompositor.drawVertices(this.gl.TRIANGLES, glPoints, indices.length);
909
+ this.strokePolygon(poly, true);
998
910
  }
999
911
 
1000
912
  /**
@@ -1009,19 +921,16 @@ class WebGLRenderer extends Renderer {
1009
921
  * @param {boolean} [fill=false] also fill the shape with the current color if true
1010
922
  */
1011
923
  strokeRect(x, y, width, height, fill = false) {
1012
- if (fill === true ) {
1013
- this.fillRect(x, y, width, height);
924
+ if (this.getGlobalAlpha() < 1 / 255) {
925
+ // Fast path: don't draw fully transparent
926
+ return;
927
+ }
928
+ this.path2D.beginPath();
929
+ this.path2D.rect(x, y, width, height);
930
+ if (fill === false) {
931
+ this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
1014
932
  } else {
1015
- var points = this._glPoints;
1016
- points[0].x = x;
1017
- points[0].y = y;
1018
- points[1].x = x + width;
1019
- points[1].y = y;
1020
- points[2].x = x + width;
1021
- points[2].y = y + height;
1022
- points[3].x = x;
1023
- points[3].y = y + height;
1024
- this.currentCompositor.drawVertices(this.gl.LINE_LOOP, points, 4);
933
+ this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
1025
934
  }
1026
935
  }
1027
936
 
@@ -1036,16 +945,49 @@ class WebGLRenderer extends Renderer {
1036
945
  * @param {number} height
1037
946
  */
1038
947
  fillRect(x, y, width, height) {
1039
- var glPoints = this._glPoints;
1040
- glPoints[0].x = x + width;
1041
- glPoints[0].y = y;
1042
- glPoints[1].x = x;
1043
- glPoints[1].y = y;
1044
- glPoints[2].x = x + width;
1045
- glPoints[2].y = y + height;
1046
- glPoints[3].x = x;
1047
- glPoints[3].y = y + height;
1048
- this.currentCompositor.drawVertices(this.gl.TRIANGLE_STRIP, glPoints, 4);
948
+ this.strokeRect(x, y, width, height, true);
949
+ }
950
+
951
+ /**
952
+ * Stroke a rounded rectangle at the specified coordinates
953
+ * @name strokeRoundRect
954
+ * @memberof WebGLRenderer.prototype
955
+ * @function
956
+ * @param {number} x
957
+ * @param {number} y
958
+ * @param {number} width
959
+ * @param {number} height
960
+ * @param {number} radius
961
+ * @param {boolean} [fill=false] also fill the shape with the current color if true
962
+ */
963
+ strokeRoundRect(x, y, width, height, radius, fill = false) {
964
+ if (this.getGlobalAlpha() < 1 / 255) {
965
+ // Fast path: don't draw fully transparent
966
+ return;
967
+ }
968
+ this.path2D.beginPath();
969
+ this.path2D.roundRect(x, y, width, height, radius);
970
+ if (fill === false) {
971
+ this.currentCompositor.drawVertices(this.gl.LINE_LOOP, this.path2D.points);
972
+ } else {
973
+ this.path2D.closePath();
974
+ this.currentCompositor.drawVertices(this.gl.TRIANGLES, this.path2D.triangulatePath());
975
+ }
976
+ }
977
+
978
+ /**
979
+ * Draw a rounded filled rectangle at the specified coordinates
980
+ * @name fillRoundRect
981
+ * @memberof WebGLRenderer.prototype
982
+ * @function
983
+ * @param {number} x
984
+ * @param {number} y
985
+ * @param {number} width
986
+ * @param {number} height
987
+ * @param {number} radius
988
+ */
989
+ fillRoundRect(x, y, width, height, radius) {
990
+ this.strokeRoundRect(x, y, width, height, radius, true);
1049
991
  }
1050
992
 
1051
993
  /**
@@ -1155,7 +1097,7 @@ class WebGLRenderer extends Renderer {
1155
1097
  * @name setMask
1156
1098
  * @memberof WebGLRenderer.prototype
1157
1099
  * @function
1158
- * @param {Rect|Polygon|Line|Ellipse} [mask] the shape defining the mask to be applied
1100
+ * @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] the shape defining the mask to be applied
1159
1101
  */
1160
1102
  setMask(mask) {
1161
1103
  var gl = this.gl;