melonjs 14.0.2 → 14.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.
- package/README.md +2 -0
- package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +10 -0
- package/dist/melonjs.mjs/_virtual/arraymultimap.js +10 -0
- package/dist/melonjs.mjs/_virtual/earcut.js +10 -0
- package/dist/melonjs.mjs/_virtual/howler.js +10 -0
- package/dist/melonjs.mjs/_virtual/index.js +10 -0
- package/dist/melonjs.mjs/_virtual/index2.js +10 -0
- package/dist/melonjs.mjs/_virtual/multimap.js +10 -0
- package/dist/melonjs.mjs/_virtual/setmultimap.js +10 -0
- package/dist/melonjs.mjs/application/application.js +238 -0
- package/dist/melonjs.mjs/audio/audio.js +536 -0
- package/dist/melonjs.mjs/camera/camera2d.js +731 -0
- package/dist/melonjs.mjs/entity/entity.js +247 -0
- package/dist/melonjs.mjs/game.js +29 -0
- package/dist/melonjs.mjs/geometries/ellipse.js +274 -0
- package/dist/melonjs.mjs/geometries/line.js +115 -0
- package/dist/melonjs.mjs/geometries/path2d.js +318 -0
- package/dist/melonjs.mjs/geometries/point.js +88 -0
- package/dist/melonjs.mjs/geometries/poly.js +498 -0
- package/dist/melonjs.mjs/geometries/rectangle.js +374 -0
- package/dist/melonjs.mjs/geometries/roundrect.js +167 -0
- package/dist/melonjs.mjs/index.js +248 -0
- package/dist/melonjs.mjs/input/gamepad.js +501 -0
- package/dist/melonjs.mjs/input/input.js +26 -0
- package/dist/melonjs.mjs/input/keyboard.js +470 -0
- package/dist/melonjs.mjs/input/pointer.js +393 -0
- package/dist/melonjs.mjs/input/pointerevent.js +818 -0
- package/dist/melonjs.mjs/lang/deprecated.js +157 -0
- package/dist/melonjs.mjs/level/level.js +297 -0
- package/dist/melonjs.mjs/level/tiled/TMXGroup.js +141 -0
- package/dist/melonjs.mjs/level/tiled/TMXLayer.js +446 -0
- package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
- package/dist/melonjs.mjs/level/tiled/TMXTile.js +193 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +636 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileset.js +309 -0
- package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +81 -0
- package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +504 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +218 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +155 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +124 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +107 -0
- package/dist/melonjs.mjs/loader/loader.js +801 -0
- package/dist/melonjs.mjs/loader/loadingscreen.js +120 -0
- package/dist/melonjs.mjs/loader/melonjs_logo.png.js +11 -0
- package/dist/melonjs.mjs/math/color.js +616 -0
- package/dist/melonjs.mjs/math/math.js +218 -0
- package/dist/melonjs.mjs/math/matrix2.js +501 -0
- package/dist/melonjs.mjs/math/matrix3.js +679 -0
- package/dist/melonjs.mjs/math/observable_vector2.js +469 -0
- package/dist/melonjs.mjs/math/observable_vector3.js +559 -0
- package/dist/melonjs.mjs/math/vector2.js +526 -0
- package/dist/melonjs.mjs/math/vector3.js +567 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/arraymultimap.js +73 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/index.js +21 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/multimap.js +324 -0
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/src/setmultimap.js +69 -0
- package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +691 -0
- package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +350 -0
- package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +3241 -0
- package/dist/melonjs.mjs/particles/emitter.js +265 -0
- package/dist/melonjs.mjs/particles/particle.js +186 -0
- package/dist/melonjs.mjs/particles/settings.js +319 -0
- package/dist/melonjs.mjs/physics/body.js +702 -0
- package/dist/melonjs.mjs/physics/bounds.js +459 -0
- package/dist/melonjs.mjs/physics/collision.js +132 -0
- package/dist/melonjs.mjs/physics/detector.js +194 -0
- package/dist/melonjs.mjs/physics/quadtree.js +394 -0
- package/dist/melonjs.mjs/physics/response.js +57 -0
- package/dist/melonjs.mjs/physics/sat.js +483 -0
- package/dist/melonjs.mjs/physics/world.js +219 -0
- package/dist/melonjs.mjs/plugin/plugin.js +141 -0
- package/dist/melonjs.mjs/renderable/collectable.js +60 -0
- package/dist/melonjs.mjs/renderable/colorlayer.js +78 -0
- package/dist/melonjs.mjs/renderable/container.js +1016 -0
- package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
- package/dist/melonjs.mjs/renderable/imagelayer.js +305 -0
- package/dist/melonjs.mjs/renderable/light2d.js +155 -0
- package/dist/melonjs.mjs/renderable/nineslicesprite.js +246 -0
- package/dist/melonjs.mjs/renderable/renderable.js +781 -0
- package/dist/melonjs.mjs/renderable/sprite.js +653 -0
- package/dist/melonjs.mjs/renderable/trigger.js +156 -0
- package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +212 -0
- package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +225 -0
- package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +127 -0
- package/dist/melonjs.mjs/state/stage.js +236 -0
- package/dist/melonjs.mjs/state/state.js +596 -0
- package/dist/melonjs.mjs/system/device.js +909 -0
- package/dist/melonjs.mjs/system/dom.js +78 -0
- package/dist/melonjs.mjs/system/event.js +537 -0
- package/dist/melonjs.mjs/system/platform.js +41 -0
- package/dist/melonjs.mjs/system/pooling.js +209 -0
- package/dist/melonjs.mjs/system/save.js +157 -0
- package/dist/melonjs.mjs/system/timer.js +286 -0
- package/dist/melonjs.mjs/text/bitmaptext.js +363 -0
- package/dist/melonjs.mjs/text/bitmaptextdata.js +198 -0
- package/dist/melonjs.mjs/text/glyph.js +65 -0
- package/dist/melonjs.mjs/text/text.js +452 -0
- package/dist/melonjs.mjs/text/textmetrics.js +175 -0
- package/dist/melonjs.mjs/text/textstyle.js +23 -0
- package/dist/melonjs.mjs/tweens/easing.js +336 -0
- package/dist/melonjs.mjs/tweens/interpolation.js +112 -0
- package/dist/melonjs.mjs/tweens/tween.js +479 -0
- package/dist/melonjs.mjs/utils/agent.js +76 -0
- package/dist/melonjs.mjs/utils/array.js +63 -0
- package/dist/melonjs.mjs/utils/file.js +42 -0
- package/dist/melonjs.mjs/utils/function.js +70 -0
- package/dist/melonjs.mjs/utils/string.js +82 -0
- package/dist/melonjs.mjs/utils/utils.js +173 -0
- package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +806 -0
- package/dist/melonjs.mjs/video/renderer.js +410 -0
- package/dist/melonjs.mjs/video/texture/atlas.js +519 -0
- package/dist/melonjs.mjs/video/texture/cache.js +143 -0
- package/dist/melonjs.mjs/video/texture/canvas_texture.js +144 -0
- package/dist/melonjs.mjs/video/video.js +462 -0
- package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +142 -0
- package/dist/melonjs.mjs/video/webgl/glshader.js +167 -0
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +10 -0
- package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +10 -0
- package/dist/melonjs.mjs/video/webgl/utils/attributes.js +25 -0
- package/dist/melonjs.mjs/video/webgl/utils/precision.js +20 -0
- package/dist/melonjs.mjs/video/webgl/utils/program.js +67 -0
- package/dist/melonjs.mjs/video/webgl/utils/string.js +25 -0
- package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +92 -0
- package/dist/melonjs.mjs/video/webgl/webgl_compositor.js +494 -0
- package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1035 -0
- package/dist/melonjs.module.d.ts +1298 -1359
- package/dist/melonjs.module.js +2072 -3520
- package/package.json +21 -16
- package/src/application/application.js +4 -5
- package/src/audio/audio.js +32 -32
- package/src/camera/camera2d.js +32 -33
- package/src/entity/entity.js +18 -19
- package/src/geometries/ellipse.js +17 -18
- package/src/geometries/line.js +6 -7
- package/src/geometries/path2d.js +33 -34
- package/src/geometries/point.js +1 -2
- package/src/geometries/poly.js +16 -18
- package/src/geometries/rectangle.js +19 -20
- package/src/geometries/roundrect.js +9 -10
- package/src/input/gamepad.js +15 -15
- package/src/input/keyboard.js +12 -12
- package/src/input/pointer.js +6 -6
- package/src/input/pointerevent.js +12 -12
- package/src/lang/deprecated.js +12 -12
- package/src/level/level.js +25 -25
- package/src/level/tiled/TMXLayer.js +23 -24
- package/src/level/tiled/TMXTile.js +6 -7
- package/src/level/tiled/TMXTileMap.js +8 -10
- package/src/level/tiled/TMXTileset.js +3 -4
- package/src/level/tiled/TMXTilesetGroup.js +1 -2
- package/src/level/tiled/TMXUtils.js +5 -5
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +3 -4
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +3 -4
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +2 -3
- package/src/level/tiled/renderer/TMXRenderer.js +20 -21
- package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -2
- package/src/loader/loader.js +20 -20
- package/src/math/color.js +21 -22
- package/src/math/math.js +16 -16
- package/src/math/matrix2.js +17 -18
- package/src/math/matrix3.js +26 -27
- package/src/math/observable_vector2.js +15 -16
- package/src/math/observable_vector3.js +17 -18
- package/src/math/vector2.js +10 -11
- package/src/math/vector3.js +11 -12
- package/src/particles/emitter.js +7 -8
- package/src/particles/particle.js +3 -4
- package/src/physics/body.js +29 -30
- package/src/physics/bounds.js +10 -10
- package/src/physics/collision.js +2 -2
- package/src/physics/detector.js +6 -6
- package/src/physics/quadtree.js +18 -23
- package/src/physics/sat.js +31 -31
- package/src/physics/world.js +6 -7
- package/src/plugin/plugin.js +5 -5
- package/src/renderable/collectable.js +4 -6
- package/src/renderable/colorlayer.js +6 -8
- package/src/renderable/container.js +25 -27
- package/src/renderable/dragndrop.js +14 -14
- package/src/renderable/imagelayer.js +14 -15
- package/src/renderable/light2d.js +4 -5
- package/src/renderable/nineslicesprite.js +17 -18
- package/src/renderable/renderable.js +26 -28
- package/src/renderable/sprite.js +29 -30
- package/src/renderable/trigger.js +16 -17
- package/src/renderable/ui/uibaseelement.js +8 -9
- package/src/renderable/ui/uispriteelement.js +8 -8
- package/src/renderable/ui/uitextbutton.js +15 -15
- package/src/state/stage.js +8 -9
- package/src/state/state.js +17 -17
- package/src/system/device.js +11 -11
- package/src/system/event.js +10 -10
- package/src/system/pooling.js +9 -9
- package/src/system/save.js +2 -2
- package/src/system/timer.js +10 -10
- package/src/text/bitmaptext.js +19 -20
- package/src/text/bitmaptextdata.js +3 -4
- package/src/text/glyph.js +1 -2
- package/src/text/text.js +24 -25
- package/src/text/textmetrics.js +9 -10
- package/src/tweens/tween.js +20 -21
- package/src/utils/agent.js +5 -5
- package/src/utils/array.js +4 -4
- package/src/utils/file.js +2 -2
- package/src/utils/function.js +6 -6
- package/src/utils/string.js +5 -5
- package/src/utils/utils.js +4 -4
- package/src/video/canvas/canvas_renderer.js +72 -73
- package/src/video/renderer.js +27 -28
- package/src/video/texture/atlas.js +22 -22
- package/src/video/texture/canvas_texture.js +9 -9
- package/src/video/video.js +17 -17
- package/src/video/webgl/buffer/vertex.js +1 -2
- package/src/video/webgl/glshader.js +12 -12
- package/src/video/webgl/webgl_compositor.js +42 -43
- package/src/video/webgl/webgl_renderer.js +76 -77
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* melonJS Game Engine - v14.1.1
|
|
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
|
+
export { Polygon as default };
|