melonjs 14.0.1 → 14.1.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 (214) hide show
  1. package/README.md +2 -0
  2. package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +10 -0
  3. package/dist/melonjs.mjs/_virtual/arraymultimap.js +10 -0
  4. package/dist/melonjs.mjs/_virtual/earcut.js +10 -0
  5. package/dist/melonjs.mjs/_virtual/howler.js +10 -0
  6. package/dist/melonjs.mjs/_virtual/index.js +10 -0
  7. package/dist/melonjs.mjs/_virtual/index2.js +10 -0
  8. package/dist/melonjs.mjs/_virtual/multimap.js +10 -0
  9. package/dist/melonjs.mjs/_virtual/setmultimap.js +10 -0
  10. package/dist/melonjs.mjs/application/application.js +240 -0
  11. package/dist/melonjs.mjs/audio/audio.js +536 -0
  12. package/dist/melonjs.mjs/camera/camera2d.js +732 -0
  13. package/dist/melonjs.mjs/entity/entity.js +248 -0
  14. package/dist/melonjs.mjs/game.js +29 -0
  15. package/dist/melonjs.mjs/geometries/ellipse.js +275 -0
  16. package/dist/melonjs.mjs/geometries/line.js +116 -0
  17. package/dist/melonjs.mjs/geometries/path2d.js +319 -0
  18. package/dist/melonjs.mjs/geometries/point.js +89 -0
  19. package/dist/melonjs.mjs/geometries/poly.js +500 -0
  20. package/dist/melonjs.mjs/geometries/rectangle.js +375 -0
  21. package/dist/melonjs.mjs/geometries/roundrect.js +168 -0
  22. package/dist/melonjs.mjs/index.js +248 -0
  23. package/dist/melonjs.mjs/input/gamepad.js +501 -0
  24. package/dist/melonjs.mjs/input/input.js +26 -0
  25. package/dist/melonjs.mjs/input/keyboard.js +470 -0
  26. package/dist/melonjs.mjs/input/pointer.js +393 -0
  27. package/dist/melonjs.mjs/input/pointerevent.js +818 -0
  28. package/dist/melonjs.mjs/lang/deprecated.js +157 -0
  29. package/dist/melonjs.mjs/level/level.js +297 -0
  30. package/dist/melonjs.mjs/level/tiled/TMXGroup.js +141 -0
  31. package/dist/melonjs.mjs/level/tiled/TMXLayer.js +448 -0
  32. package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
  33. package/dist/melonjs.mjs/level/tiled/TMXTile.js +194 -0
  34. package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +639 -0
  35. package/dist/melonjs.mjs/level/tiled/TMXTileset.js +311 -0
  36. package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +83 -0
  37. package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
  38. package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +506 -0
  39. package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +220 -0
  40. package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +157 -0
  41. package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +125 -0
  42. package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +109 -0
  43. package/dist/melonjs.mjs/loader/loader.js +801 -0
  44. package/dist/melonjs.mjs/loader/loadingscreen.js +120 -0
  45. package/dist/melonjs.mjs/loader/melonjs_logo.png.js +11 -0
  46. package/dist/melonjs.mjs/math/color.js +618 -0
  47. package/dist/melonjs.mjs/math/math.js +218 -0
  48. package/dist/melonjs.mjs/math/matrix2.js +503 -0
  49. package/dist/melonjs.mjs/math/matrix3.js +681 -0
  50. package/dist/melonjs.mjs/math/observable_vector2.js +471 -0
  51. package/dist/melonjs.mjs/math/observable_vector3.js +561 -0
  52. package/dist/melonjs.mjs/math/vector2.js +528 -0
  53. package/dist/melonjs.mjs/math/vector3.js +569 -0
  54. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +73 -0
  55. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +21 -0
  56. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +324 -0
  57. package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +69 -0
  58. package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +691 -0
  59. package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +350 -0
  60. package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +3241 -0
  61. package/dist/melonjs.mjs/particles/emitter.js +267 -0
  62. package/dist/melonjs.mjs/particles/particle.js +188 -0
  63. package/dist/melonjs.mjs/particles/settings.js +319 -0
  64. package/dist/melonjs.mjs/physics/body.js +704 -0
  65. package/dist/melonjs.mjs/physics/bounds.js +460 -0
  66. package/dist/melonjs.mjs/physics/collision.js +132 -0
  67. package/dist/melonjs.mjs/physics/detector.js +194 -0
  68. package/dist/melonjs.mjs/physics/quadtree.js +391 -0
  69. package/dist/melonjs.mjs/physics/response.js +57 -0
  70. package/dist/melonjs.mjs/physics/sat.js +483 -0
  71. package/dist/melonjs.mjs/physics/world.js +221 -0
  72. package/dist/melonjs.mjs/plugin/plugin.js +141 -0
  73. package/dist/melonjs.mjs/renderable/collectable.js +62 -0
  74. package/dist/melonjs.mjs/renderable/colorlayer.js +80 -0
  75. package/dist/melonjs.mjs/renderable/container.js +1018 -0
  76. package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
  77. package/dist/melonjs.mjs/renderable/imagelayer.js +306 -0
  78. package/dist/melonjs.mjs/renderable/light2d.js +156 -0
  79. package/dist/melonjs.mjs/renderable/nineslicesprite.js +247 -0
  80. package/dist/melonjs.mjs/renderable/renderable.js +783 -0
  81. package/dist/melonjs.mjs/renderable/sprite.js +654 -0
  82. package/dist/melonjs.mjs/renderable/trigger.js +157 -0
  83. package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +213 -0
  84. package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +226 -0
  85. package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +128 -0
  86. package/dist/melonjs.mjs/state/stage.js +237 -0
  87. package/dist/melonjs.mjs/state/state.js +596 -0
  88. package/dist/melonjs.mjs/system/device.js +909 -0
  89. package/dist/melonjs.mjs/system/dom.js +78 -0
  90. package/dist/melonjs.mjs/system/event.js +537 -0
  91. package/dist/melonjs.mjs/system/platform.js +41 -0
  92. package/dist/melonjs.mjs/system/pooling.js +209 -0
  93. package/dist/melonjs.mjs/system/save.js +157 -0
  94. package/dist/melonjs.mjs/system/timer.js +286 -0
  95. package/dist/melonjs.mjs/text/bitmaptext.js +364 -0
  96. package/dist/melonjs.mjs/text/bitmaptextdata.js +199 -0
  97. package/dist/melonjs.mjs/text/glyph.js +66 -0
  98. package/dist/melonjs.mjs/text/text.js +453 -0
  99. package/dist/melonjs.mjs/text/textmetrics.js +176 -0
  100. package/dist/melonjs.mjs/text/textstyle.js +23 -0
  101. package/dist/melonjs.mjs/tweens/easing.js +336 -0
  102. package/dist/melonjs.mjs/tweens/interpolation.js +112 -0
  103. package/dist/melonjs.mjs/tweens/tween.js +480 -0
  104. package/dist/melonjs.mjs/utils/agent.js +76 -0
  105. package/dist/melonjs.mjs/utils/array.js +63 -0
  106. package/dist/melonjs.mjs/utils/file.js +42 -0
  107. package/dist/melonjs.mjs/utils/function.js +70 -0
  108. package/dist/melonjs.mjs/utils/string.js +82 -0
  109. package/dist/melonjs.mjs/utils/utils.js +173 -0
  110. package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +807 -0
  111. package/dist/melonjs.mjs/video/renderer.js +411 -0
  112. package/dist/melonjs.mjs/video/texture/atlas.js +519 -0
  113. package/dist/melonjs.mjs/video/texture/cache.js +143 -0
  114. package/dist/melonjs.mjs/video/texture/canvas_texture.js +144 -0
  115. package/dist/melonjs.mjs/video/video.js +462 -0
  116. package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +143 -0
  117. package/dist/melonjs.mjs/video/webgl/glshader.js +168 -0
  118. package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +10 -0
  119. package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +10 -0
  120. package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +10 -0
  121. package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +10 -0
  122. package/dist/melonjs.mjs/video/webgl/utils/attributes.js +25 -0
  123. package/dist/melonjs.mjs/video/webgl/utils/precision.js +20 -0
  124. package/dist/melonjs.mjs/video/webgl/utils/program.js +67 -0
  125. package/dist/melonjs.mjs/video/webgl/utils/string.js +25 -0
  126. package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +92 -0
  127. package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +495 -0
  128. package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1036 -0
  129. package/dist/melonjs.module.d.ts +1163 -1163
  130. package/dist/melonjs.module.js +1903 -3274
  131. package/package.json +22 -17
  132. package/src/application/application.js +3 -3
  133. package/src/audio/audio.js +32 -32
  134. package/src/camera/camera2d.js +31 -31
  135. package/src/entity/entity.js +17 -17
  136. package/src/geometries/ellipse.js +16 -16
  137. package/src/geometries/line.js +5 -5
  138. package/src/geometries/path2d.js +32 -32
  139. package/src/geometries/poly.js +15 -15
  140. package/src/geometries/rectangle.js +18 -18
  141. package/src/geometries/roundrect.js +8 -8
  142. package/src/input/gamepad.js +15 -15
  143. package/src/input/keyboard.js +12 -12
  144. package/src/input/pointer.js +6 -6
  145. package/src/input/pointerevent.js +12 -12
  146. package/src/lang/deprecated.js +12 -12
  147. package/src/level/level.js +25 -25
  148. package/src/level/tiled/TMXLayer.js +22 -22
  149. package/src/level/tiled/TMXTile.js +5 -5
  150. package/src/level/tiled/TMXTileMap.js +6 -6
  151. package/src/level/tiled/TMXTileset.js +2 -2
  152. package/src/level/tiled/TMXUtils.js +5 -5
  153. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +2 -2
  154. package/src/level/tiled/renderer/TMXIsometricRenderer.js +2 -2
  155. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  156. package/src/level/tiled/renderer/TMXRenderer.js +19 -19
  157. package/src/loader/loader.js +20 -20
  158. package/src/math/color.js +20 -20
  159. package/src/math/math.js +16 -16
  160. package/src/math/matrix2.js +16 -16
  161. package/src/math/matrix3.js +25 -25
  162. package/src/math/observable_vector2.js +14 -14
  163. package/src/math/observable_vector3.js +16 -16
  164. package/src/math/vector2.js +9 -9
  165. package/src/math/vector3.js +10 -10
  166. package/src/particles/emitter.js +6 -6
  167. package/src/particles/particle.js +2 -2
  168. package/src/physics/body.js +28 -28
  169. package/src/physics/bounds.js +8 -8
  170. package/src/physics/collision.js +2 -2
  171. package/src/physics/detector.js +6 -6
  172. package/src/physics/quadtree.js +11 -11
  173. package/src/physics/sat.js +31 -31
  174. package/src/physics/world.js +5 -5
  175. package/src/plugin/plugin.js +5 -5
  176. package/src/renderable/collectable.js +3 -3
  177. package/src/renderable/colorlayer.js +5 -5
  178. package/src/renderable/container.js +21 -21
  179. package/src/renderable/dragndrop.js +14 -14
  180. package/src/renderable/imagelayer.js +13 -13
  181. package/src/renderable/light2d.js +3 -3
  182. package/src/renderable/nineslicesprite.js +16 -16
  183. package/src/renderable/renderable.js +23 -23
  184. package/src/renderable/sprite.js +28 -28
  185. package/src/renderable/trigger.js +15 -15
  186. package/src/renderable/ui/uibaseelement.js +7 -7
  187. package/src/renderable/ui/uispriteelement.js +6 -6
  188. package/src/renderable/ui/uitextbutton.js +13 -13
  189. package/src/state/stage.js +7 -7
  190. package/src/state/state.js +17 -17
  191. package/src/system/device.js +11 -11
  192. package/src/system/event.js +10 -10
  193. package/src/system/pooling.js +9 -9
  194. package/src/system/save.js +2 -2
  195. package/src/system/timer.js +10 -10
  196. package/src/text/bitmaptext.js +18 -18
  197. package/src/text/bitmaptextdata.js +2 -2
  198. package/src/text/text.js +23 -23
  199. package/src/text/textmetrics.js +8 -8
  200. package/src/tweens/tween.js +19 -19
  201. package/src/utils/agent.js +5 -5
  202. package/src/utils/array.js +4 -4
  203. package/src/utils/file.js +2 -2
  204. package/src/utils/function.js +6 -6
  205. package/src/utils/string.js +5 -5
  206. package/src/utils/utils.js +4 -4
  207. package/src/video/canvas/canvas_renderer.js +70 -70
  208. package/src/video/renderer.js +26 -26
  209. package/src/video/texture/atlas.js +22 -22
  210. package/src/video/texture/canvas_texture.js +9 -9
  211. package/src/video/video.js +17 -17
  212. package/src/video/webgl/glshader.js +10 -10
  213. package/src/video/webgl/webgl_compositor.js +41 -41
  214. package/src/video/webgl/webgl_renderer.js +75 -75
@@ -0,0 +1,500 @@
1
+ /*!
2
+ * melonJS Game Engine - v14.1.0
3
+ * http://www.melonjs.org
4
+ * melonjs is licensed under the MIT License.
5
+ * http://www.opensource.org/licenses/mit-license
6
+ * @copyright (C) 2011 - 2022 Olivier Biot (AltByte Pte Ltd)
7
+ */
8
+ import '../node_modules/earcut/src/earcut.js';
9
+ import Vector2d from '../math/vector2.js';
10
+ import pool from '../system/pooling.js';
11
+ import { e as earcut } from '../_virtual/earcut.js';
12
+
13
+ /**
14
+ * @classdesc
15
+ * a polygon Object.<br>
16
+ * Please do note that melonJS implements a simple Axis-Aligned Boxes collision algorithm, which requires all polygons used for collision to be convex with all vertices defined with clockwise winding.
17
+ * A polygon is convex when all line segments connecting two points in the interior do not cross any edge of the polygon
18
+ * (which means that all angles are less than 180 degrees), as described here below : <br>
19
+ * <center><img src="images/convex_polygon.png"/></center><br>
20
+ *
21
+ * A polygon's `winding` is clockwise if its vertices (points) are declared turning to the right. The image above shows COUNTERCLOCKWISE winding.
22
+ */
23
+ class Polygon {
24
+ /**
25
+ * @param {number} x - origin point of the Polygon
26
+ * @param {number} y - origin point of the Polygon
27
+ * @param {Vector2d[]} points - array of vector defining the Polygon
28
+ */
29
+ constructor(x, y, points) {
30
+ /**
31
+ * origin point of the Polygon
32
+ * @public
33
+ * @type {Vector2d}
34
+ * @name pos
35
+ * @memberof Polygon
36
+ */
37
+ this.pos = pool.pull("Vector2d");
38
+
39
+ /**
40
+ * The bounding rectangle for this shape
41
+ * @ignore
42
+ * @member {Bounds}
43
+ * @name _bounds
44
+ * @memberof Polygon
45
+ */
46
+ this._bounds;
47
+
48
+ /**
49
+ * Array of points defining the Polygon <br>
50
+ * Note: If you manually change `points`, you **must** call `recalc`afterwards so that the changes get applied correctly.
51
+ * @public
52
+ * @type {Vector2d[]}
53
+ * @name points
54
+ * @memberof Polygon
55
+ */
56
+ this.points = [];
57
+
58
+ /**
59
+ * The edges here are the direction of the `n`th edge of the polygon, relative to
60
+ * the `n`th point. If you want to draw a given edge from the edge value, you must
61
+ * first translate to the position of the starting point.
62
+ * @ignore
63
+ */
64
+ this.edges = [];
65
+
66
+ /**
67
+ * a list of indices for all vertices composing this polygon (@see earcut)
68
+ * @ignore
69
+ */
70
+ this.indices = [];
71
+
72
+ /**
73
+ * The normals here are the direction of the normal for the `n`th edge of the polygon, relative
74
+ * to the position of the `n`th point. If you want to draw an edge normal, you must first
75
+ * translate to the position of the starting point.
76
+ * @ignore
77
+ */
78
+ this.normals = [];
79
+
80
+ // the shape type
81
+ this.shapeType = "Polygon";
82
+ this.setShape(x, y, points);
83
+ }
84
+
85
+ /** @ignore */
86
+ onResetEvent(x, y, points) {
87
+ this.setShape(x, y, points);
88
+ }
89
+
90
+ /**
91
+ * set new value to the Polygon
92
+ * @name setShape
93
+ * @memberof Polygon
94
+ * @param {number} x - position of the Polygon
95
+ * @param {number} y - position of the Polygon
96
+ * @param {Vector2d[]|number[]} points - array of vector or vertice defining the Polygon
97
+ * @returns {Polygon} this instance for objecf chaining
98
+ */
99
+ setShape(x, y, points) {
100
+ this.pos.set(x, y);
101
+ this.setVertices(points);
102
+ return this;
103
+ }
104
+
105
+ /**
106
+ * set the vertices defining this Polygon
107
+ * @name setVertices
108
+ * @memberof Polygon
109
+ * @param {Vector2d[]} vertices - array of vector or vertice defining the Polygon
110
+ * @returns {Polygon} this instance for objecf chaining
111
+ */
112
+ setVertices(vertices) {
113
+
114
+ if (!Array.isArray(vertices)) {
115
+ return this;
116
+ }
117
+
118
+ // convert given points to me.Vector2d if required
119
+ if (!(vertices[0] instanceof Vector2d)) {
120
+ this.points.length = 0;
121
+
122
+ if (typeof vertices[0] === "object") {
123
+ // array of {x,y} object
124
+ vertices.forEach((vertice) => {
125
+ this.points.push(pool.pull("Vector2d", vertice.x, vertice.y));
126
+ });
127
+
128
+ } else {
129
+ // it's a flat array
130
+ for (var p = 0; p < vertices.length; p += 2) {
131
+ this.points.push(pool.pull("Vector2d", vertices[p], vertices[p + 1]));
132
+ }
133
+ }
134
+ } else {
135
+ // array of me.Vector2d
136
+ this.points = vertices;
137
+ }
138
+
139
+ this.recalc();
140
+ this.updateBounds();
141
+ return this;
142
+ }
143
+
144
+ /**
145
+ * apply the given transformation matrix to this Polygon
146
+ * @name transform
147
+ * @memberof Polygon
148
+ * @param {Matrix2d} m - the transformation matrix
149
+ * @returns {Polygon} Reference to this object for method chaining
150
+ */
151
+ transform(m) {
152
+ var points = this.points;
153
+ var len = points.length;
154
+ for (var i = 0; i < len; i++) {
155
+ m.apply(points[i]);
156
+ }
157
+ this.recalc();
158
+ this.updateBounds();
159
+ return this;
160
+ }
161
+
162
+ /**
163
+ * apply an isometric projection to this shape
164
+ * @name toIso
165
+ * @memberof Polygon
166
+ * @returns {Polygon} Reference to this object for method chaining
167
+ */
168
+ toIso() {
169
+ return this.rotate(Math.PI / 4).scale(Math.SQRT2, Math.SQRT1_2);
170
+ }
171
+
172
+ /**
173
+ * apply a 2d projection to this shape
174
+ * @name to2d
175
+ * @memberof Polygon
176
+ * @returns {Polygon} Reference to this object for method chaining
177
+ */
178
+ to2d() {
179
+ return this.scale(Math.SQRT1_2, Math.SQRT2).rotate(-Math.PI / 4);
180
+ }
181
+
182
+ /**
183
+ * Rotate this Polygon (counter-clockwise) by the specified angle (in radians).
184
+ * @name rotate
185
+ * @memberof Polygon
186
+ * @param {number} angle - The angle to rotate (in radians)
187
+ * @param {Vector2d|ObservableVector2d} [v] - an optional point to rotate around
188
+ * @returns {Polygon} Reference to this object for method chaining
189
+ */
190
+ rotate(angle, v) {
191
+ if (angle !== 0) {
192
+ var points = this.points;
193
+ var len = points.length;
194
+ for (var i = 0; i < len; i++) {
195
+ points[i].rotate(angle, v);
196
+ }
197
+ this.recalc();
198
+ this.updateBounds();
199
+ }
200
+ return this;
201
+ }
202
+
203
+ /**
204
+ * Scale this Polygon by the given scalar.
205
+ * @name scale
206
+ * @memberof Polygon
207
+ * @param {number} x
208
+ * @param {number} [y=x]
209
+ * @returns {Polygon} Reference to this object for method chaining
210
+ */
211
+ scale(x, y) {
212
+ y = typeof (y) !== "undefined" ? y : x;
213
+
214
+ var points = this.points;
215
+ var len = points.length;
216
+ for (var i = 0; i < len; i++) {
217
+ points[i].scale(x, y);
218
+ }
219
+ this.recalc();
220
+ this.updateBounds();
221
+ return this;
222
+ }
223
+
224
+ /**
225
+ * Scale this Polygon by the given vector
226
+ * @name scaleV
227
+ * @memberof Polygon
228
+ * @param {Vector2d} v
229
+ * @returns {Polygon} Reference to this object for method chaining
230
+ */
231
+ scaleV(v) {
232
+ return this.scale(v.x, v.y);
233
+ }
234
+
235
+ /**
236
+ * Computes the calculated collision polygon.
237
+ * This **must** be called if the `points` array, `angle`, or `offset` is modified manually.
238
+ * @name recalc
239
+ * @memberof Polygon
240
+ * @returns {Polygon} Reference to this object for method chaining
241
+ */
242
+ recalc() {
243
+ var i;
244
+ var edges = this.edges;
245
+ var normals = this.normals;
246
+ var indices = this.indices;
247
+
248
+ // Copy the original points array and apply the offset/angle
249
+ var points = this.points;
250
+ var len = points.length;
251
+
252
+ if (len < 3) {
253
+ throw new Error("Requires at least 3 points");
254
+ }
255
+
256
+ // Calculate the edges/normals
257
+ for (i = 0; i < len; i++) {
258
+ if (edges[i] === undefined) {
259
+ edges[i] = pool.pull("Vector2d");
260
+ }
261
+ edges[i].copy(points[(i + 1) % len]).sub(points[i]);
262
+
263
+ if (normals[i] === undefined) {
264
+ normals[i] = pool.pull("Vector2d");
265
+ }
266
+ normals[i].copy(edges[i]).perp().normalize();
267
+ }
268
+ // trunc array
269
+ edges.length = len;
270
+ normals.length = len;
271
+ // do not do anything here, indices will be computed by
272
+ // getIndices if array is empty upon function call
273
+ indices.length = 0;
274
+
275
+ return this;
276
+ }
277
+
278
+
279
+ /**
280
+ * returns a list of indices for all triangles defined in this polygon
281
+ * @name getIndices
282
+ * @memberof Polygon
283
+ * @returns {Array} an array of vertex indices for all triangles forming this polygon.
284
+ */
285
+ getIndices() {
286
+ if (this.indices.length === 0) {
287
+ this.indices = earcut.exports(this.points.flatMap(p => [p.x, p.y]));
288
+ }
289
+ return this.indices;
290
+ }
291
+
292
+ /**
293
+ * Returns true if the vertices composing this polygon form a convex shape (vertices must be in clockwise order).
294
+ * @name isConvex
295
+ * @memberof Polygon
296
+ * @returns {boolean} true if the vertices are convex, false if not, null if not computable
297
+ */
298
+ isConvex() {
299
+ // http://paulbourke.net/geometry/polygonmesh/
300
+ // Copyright (c) Paul Bourke (use permitted)
301
+
302
+ var flag = 0,
303
+ vertices = this.points,
304
+ n = vertices.length,
305
+ i,
306
+ j,
307
+ k,
308
+ z;
309
+
310
+ if (n < 3) {
311
+ return null;
312
+ }
313
+
314
+ for (i = 0; i < n; i++) {
315
+ j = (i + 1) % n;
316
+ k = (i + 2) % n;
317
+ z = (vertices[j].x - vertices[i].x) * (vertices[k].y - vertices[j].y);
318
+ z -= (vertices[j].y - vertices[i].y) * (vertices[k].x - vertices[j].x);
319
+
320
+ if (z < 0) {
321
+ flag |= 1;
322
+ } else if (z > 0) {
323
+ flag |= 2;
324
+ }
325
+
326
+ if (flag === 3) {
327
+ return false;
328
+ }
329
+ }
330
+
331
+ if (flag !== 0) {
332
+ return true;
333
+ } else {
334
+ return null;
335
+ }
336
+ }
337
+
338
+ /**
339
+ * translate the Polygon by the specified offset
340
+ * @name translate
341
+ * @memberof Polygon
342
+ * @method
343
+ * @param {number} x - x offset
344
+ * @param {number} y - y offset
345
+ * @returns {Polygon} this Polygon
346
+ */
347
+ /**
348
+ * translate the Polygon by the specified vector
349
+ * @name translate
350
+ * @memberof Polygon
351
+ * @param {Vector2d} v - vector offset
352
+ * @returns {Polygon} Reference to this object for method chaining
353
+ */
354
+ translate() {
355
+ var _x, _y;
356
+
357
+ if (arguments.length === 2) {
358
+ // x, y
359
+ _x = arguments[0];
360
+ _y = arguments[1];
361
+ } else {
362
+ // vector
363
+ _x = arguments[0].x;
364
+ _y = arguments[0].y;
365
+ }
366
+
367
+ this.pos.x += _x;
368
+ this.pos.y += _y;
369
+ this.getBounds().translate(_x, _y);
370
+
371
+ return this;
372
+ }
373
+
374
+ /**
375
+ * Shifts the Polygon to the given position vector.
376
+ * @name shift
377
+ * @memberof Polygon
378
+ * @method
379
+ * @param {Vector2d} position
380
+ */
381
+ /**
382
+ * Shifts the Polygon to the given x, y position.
383
+ * @name shift
384
+ * @memberof Polygon
385
+ * @param {number} x
386
+ * @param {number} y
387
+ */
388
+ shift() {
389
+ var _x, _y;
390
+ if (arguments.length === 2) {
391
+ // x, y
392
+ _x = arguments[0];
393
+ _y = arguments[1];
394
+ } else {
395
+ // vector
396
+ _x = arguments[0].x;
397
+ _y = arguments[0].y;
398
+ }
399
+ this.pos.x = _x;
400
+ this.pos.y = _y;
401
+ this.updateBounds();
402
+ }
403
+
404
+ /**
405
+ * Returns true if the polygon contains the given point.
406
+ * (Note: it is highly recommended to first do a hit test on the corresponding <br>
407
+ * bounding rect, as the function can be highly consuming with complex shapes)
408
+ * @name contains
409
+ * @memberof Polygon
410
+ * @method
411
+ * @param {Vector2d} point
412
+ * @returns {boolean} true if contains
413
+ */
414
+
415
+ /**
416
+ * Returns true if the polygon contains the given point. <br>
417
+ * (Note: it is highly recommended to first do a hit test on the corresponding <br>
418
+ * bounding rect, as the function can be highly consuming with complex shapes)
419
+ * @name contains
420
+ * @memberof Polygon
421
+ * @param {number} x - x coordinate
422
+ * @param {number} y - y coordinate
423
+ * @returns {boolean} true if contains
424
+ */
425
+ contains() {
426
+ var _x, _y;
427
+
428
+ if (arguments.length === 2) {
429
+ // x, y
430
+ _x = arguments[0];
431
+ _y = arguments[1];
432
+ } else {
433
+ // vector
434
+ _x = arguments[0].x;
435
+ _y = arguments[0].y;
436
+ }
437
+
438
+ var intersects = false;
439
+ var posx = this.pos.x, posy = this.pos.y;
440
+ var points = this.points;
441
+ var len = points.length;
442
+
443
+ //http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
444
+ for (var i = 0, j = len - 1; i < len; j = i++) {
445
+ var iy = points[i].y + posy, ix = points[i].x + posx,
446
+ jy = points[j].y + posy, jx = points[j].x + posx;
447
+ if (((iy > _y) !== (jy > _y)) && (_x < (jx - ix) * (_y - iy) / (jy - iy) + ix)) {
448
+ intersects = !intersects;
449
+ }
450
+ }
451
+ return intersects;
452
+ }
453
+
454
+ /**
455
+ * returns the bounding box for this shape, the smallest Rectangle object completely containing this shape.
456
+ * @name getBounds
457
+ * @memberof Polygon
458
+ * @returns {Bounds} this shape bounding box Rectangle object
459
+ */
460
+ getBounds() {
461
+ if (typeof this._bounds === "undefined") {
462
+ this._bounds = pool.pull("Bounds");
463
+ }
464
+ return this._bounds;
465
+ }
466
+
467
+ /**
468
+ * update the bounding box for this shape.
469
+ * @ignore
470
+ * @name updateBounds
471
+ * @memberof Polygon
472
+ * @returns {Bounds} this shape bounding box Rectangle object
473
+ */
474
+ updateBounds() {
475
+ var bounds = this.getBounds();
476
+
477
+ bounds.update(this.points);
478
+ bounds.translate(this.pos);
479
+
480
+ return bounds;
481
+ }
482
+
483
+ /**
484
+ * clone this Polygon
485
+ * @name clone
486
+ * @memberof Polygon
487
+ * @returns {Polygon} new Polygon
488
+ */
489
+ clone() {
490
+ var copy = [];
491
+ this.points.forEach((point) => {
492
+ copy.push(point.clone());
493
+ });
494
+ return new Polygon(this.pos.x, this.pos.y, copy);
495
+ }
496
+ }
497
+
498
+ var Polygon$1 = Polygon;
499
+
500
+ export { Polygon$1 as default };