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.
- 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 +240 -0
- package/dist/melonjs.mjs/audio/audio.js +536 -0
- package/dist/melonjs.mjs/camera/camera2d.js +732 -0
- package/dist/melonjs.mjs/entity/entity.js +248 -0
- package/dist/melonjs.mjs/game.js +29 -0
- package/dist/melonjs.mjs/geometries/ellipse.js +275 -0
- package/dist/melonjs.mjs/geometries/line.js +116 -0
- package/dist/melonjs.mjs/geometries/path2d.js +319 -0
- package/dist/melonjs.mjs/geometries/point.js +89 -0
- package/dist/melonjs.mjs/geometries/poly.js +500 -0
- package/dist/melonjs.mjs/geometries/rectangle.js +375 -0
- package/dist/melonjs.mjs/geometries/roundrect.js +168 -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 +448 -0
- package/dist/melonjs.mjs/level/tiled/TMXObject.js +355 -0
- package/dist/melonjs.mjs/level/tiled/TMXTile.js +194 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +639 -0
- package/dist/melonjs.mjs/level/tiled/TMXTileset.js +311 -0
- package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +83 -0
- package/dist/melonjs.mjs/level/tiled/TMXUtils.js +367 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +506 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +220 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +157 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +125 -0
- package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +109 -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 +618 -0
- package/dist/melonjs.mjs/math/math.js +218 -0
- package/dist/melonjs.mjs/math/matrix2.js +503 -0
- package/dist/melonjs.mjs/math/matrix3.js +681 -0
- package/dist/melonjs.mjs/math/observable_vector2.js +471 -0
- package/dist/melonjs.mjs/math/observable_vector3.js +561 -0
- package/dist/melonjs.mjs/math/vector2.js +528 -0
- package/dist/melonjs.mjs/math/vector3.js +569 -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 +267 -0
- package/dist/melonjs.mjs/particles/particle.js +188 -0
- package/dist/melonjs.mjs/particles/settings.js +319 -0
- package/dist/melonjs.mjs/physics/body.js +704 -0
- package/dist/melonjs.mjs/physics/bounds.js +460 -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 +391 -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 +221 -0
- package/dist/melonjs.mjs/plugin/plugin.js +141 -0
- package/dist/melonjs.mjs/renderable/collectable.js +62 -0
- package/dist/melonjs.mjs/renderable/colorlayer.js +80 -0
- package/dist/melonjs.mjs/renderable/container.js +1018 -0
- package/dist/melonjs.mjs/renderable/dragndrop.js +224 -0
- package/dist/melonjs.mjs/renderable/imagelayer.js +306 -0
- package/dist/melonjs.mjs/renderable/light2d.js +156 -0
- package/dist/melonjs.mjs/renderable/nineslicesprite.js +247 -0
- package/dist/melonjs.mjs/renderable/renderable.js +783 -0
- package/dist/melonjs.mjs/renderable/sprite.js +654 -0
- package/dist/melonjs.mjs/renderable/trigger.js +157 -0
- package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +213 -0
- package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +226 -0
- package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +128 -0
- package/dist/melonjs.mjs/state/stage.js +237 -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 +364 -0
- package/dist/melonjs.mjs/text/bitmaptextdata.js +199 -0
- package/dist/melonjs.mjs/text/glyph.js +66 -0
- package/dist/melonjs.mjs/text/text.js +453 -0
- package/dist/melonjs.mjs/text/textmetrics.js +176 -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 +480 -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 +807 -0
- package/dist/melonjs.mjs/video/renderer.js +411 -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 +143 -0
- package/dist/melonjs.mjs/video/webgl/glshader.js +168 -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 +495 -0
- package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1036 -0
- package/dist/melonjs.module.d.ts +1163 -1163
- package/dist/melonjs.module.js +1903 -3274
- package/package.json +22 -17
- package/src/application/application.js +3 -3
- package/src/audio/audio.js +32 -32
- package/src/camera/camera2d.js +31 -31
- package/src/entity/entity.js +17 -17
- package/src/geometries/ellipse.js +16 -16
- package/src/geometries/line.js +5 -5
- package/src/geometries/path2d.js +32 -32
- package/src/geometries/poly.js +15 -15
- package/src/geometries/rectangle.js +18 -18
- package/src/geometries/roundrect.js +8 -8
- 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 +22 -22
- package/src/level/tiled/TMXTile.js +5 -5
- package/src/level/tiled/TMXTileMap.js +6 -6
- package/src/level/tiled/TMXTileset.js +2 -2
- package/src/level/tiled/TMXUtils.js +5 -5
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +2 -2
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +2 -2
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
- package/src/level/tiled/renderer/TMXRenderer.js +19 -19
- package/src/loader/loader.js +20 -20
- package/src/math/color.js +20 -20
- package/src/math/math.js +16 -16
- package/src/math/matrix2.js +16 -16
- package/src/math/matrix3.js +25 -25
- package/src/math/observable_vector2.js +14 -14
- package/src/math/observable_vector3.js +16 -16
- package/src/math/vector2.js +9 -9
- package/src/math/vector3.js +10 -10
- package/src/particles/emitter.js +6 -6
- package/src/particles/particle.js +2 -2
- package/src/physics/body.js +28 -28
- package/src/physics/bounds.js +8 -8
- package/src/physics/collision.js +2 -2
- package/src/physics/detector.js +6 -6
- package/src/physics/quadtree.js +11 -11
- package/src/physics/sat.js +31 -31
- package/src/physics/world.js +5 -5
- package/src/plugin/plugin.js +5 -5
- package/src/renderable/collectable.js +3 -3
- package/src/renderable/colorlayer.js +5 -5
- package/src/renderable/container.js +21 -21
- package/src/renderable/dragndrop.js +14 -14
- package/src/renderable/imagelayer.js +13 -13
- package/src/renderable/light2d.js +3 -3
- package/src/renderable/nineslicesprite.js +16 -16
- package/src/renderable/renderable.js +23 -23
- package/src/renderable/sprite.js +28 -28
- package/src/renderable/trigger.js +15 -15
- package/src/renderable/ui/uibaseelement.js +7 -7
- package/src/renderable/ui/uispriteelement.js +6 -6
- package/src/renderable/ui/uitextbutton.js +13 -13
- package/src/state/stage.js +7 -7
- 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 +18 -18
- package/src/text/bitmaptextdata.js +2 -2
- package/src/text/text.js +23 -23
- package/src/text/textmetrics.js +8 -8
- package/src/tweens/tween.js +19 -19
- 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 +70 -70
- package/src/video/renderer.js +26 -26
- 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/glshader.js +10 -10
- package/src/video/webgl/webgl_compositor.js +41 -41
- package/src/video/webgl/webgl_renderer.js +75 -75
|
@@ -0,0 +1,528 @@
|
|
|
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 { clamp } from './math.js';
|
|
9
|
+
import pool from '../system/pooling.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @classdesc
|
|
13
|
+
* a generic 2D Vector Object
|
|
14
|
+
*/
|
|
15
|
+
class Vector2d {
|
|
16
|
+
/**
|
|
17
|
+
* @param {number} [x=0] - x value of the vector
|
|
18
|
+
* @param {number} [y=0] - y value of the vector
|
|
19
|
+
*/
|
|
20
|
+
constructor(x = 0, y = 0) {
|
|
21
|
+
this.onResetEvent(x, y);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @ignore
|
|
26
|
+
*/
|
|
27
|
+
onResetEvent(x = 0, y = 0) {
|
|
28
|
+
// this is to enable proper object pooling
|
|
29
|
+
this.x = x;
|
|
30
|
+
this.y = y;
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @ignore
|
|
36
|
+
*/
|
|
37
|
+
_set(x, y) {
|
|
38
|
+
this.x = x;
|
|
39
|
+
this.y = y;
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* set the Vector x and y properties to the given values<br>
|
|
45
|
+
* @name set
|
|
46
|
+
* @memberof Vector2d
|
|
47
|
+
* @param {number} x
|
|
48
|
+
* @param {number} y
|
|
49
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
50
|
+
*/
|
|
51
|
+
set(x, y) {
|
|
52
|
+
if (x !== +x || y !== +y) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
"invalid x,y parameters (not a number)"
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* x value of the vector
|
|
60
|
+
* @public
|
|
61
|
+
* @member {number}
|
|
62
|
+
* @name x
|
|
63
|
+
* @memberof Vector2d
|
|
64
|
+
*/
|
|
65
|
+
//this.x = x;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* y value of the vector
|
|
69
|
+
* @public
|
|
70
|
+
* @member {number}
|
|
71
|
+
* @name y
|
|
72
|
+
* @memberof Vector2d
|
|
73
|
+
*/
|
|
74
|
+
//this.y = y;
|
|
75
|
+
|
|
76
|
+
return this._set(x, y);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* set the Vector x and y properties to 0
|
|
81
|
+
* @name setZero
|
|
82
|
+
* @memberof Vector2d
|
|
83
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
84
|
+
*/
|
|
85
|
+
setZero() {
|
|
86
|
+
return this.set(0, 0);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* set the Vector x and y properties using the passed vector
|
|
91
|
+
* @name setV
|
|
92
|
+
* @memberof Vector2d
|
|
93
|
+
* @param {Vector2d} v
|
|
94
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
95
|
+
*/
|
|
96
|
+
setV(v) {
|
|
97
|
+
return this._set(v.x, v.y);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Add the passed vector to this vector
|
|
102
|
+
* @name add
|
|
103
|
+
* @memberof Vector2d
|
|
104
|
+
* @param {Vector2d} v
|
|
105
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
106
|
+
*/
|
|
107
|
+
add(v) {
|
|
108
|
+
return this._set(this.x + v.x, this.y + v.y);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Substract the passed vector to this vector
|
|
113
|
+
* @name sub
|
|
114
|
+
* @memberof Vector2d
|
|
115
|
+
* @param {Vector2d} v
|
|
116
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
117
|
+
*/
|
|
118
|
+
sub(v) {
|
|
119
|
+
return this._set(this.x - v.x, this.y - v.y);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Multiply this vector values by the given scalar
|
|
124
|
+
* @name scale
|
|
125
|
+
* @memberof Vector2d
|
|
126
|
+
* @param {number} x
|
|
127
|
+
* @param {number} [y=x]
|
|
128
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
129
|
+
*/
|
|
130
|
+
scale(x, y) {
|
|
131
|
+
return this._set(this.x * x, this.y * (typeof (y) !== "undefined" ? y : x));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Convert this vector into isometric coordinate space
|
|
136
|
+
* @name toIso
|
|
137
|
+
* @memberof Vector2d
|
|
138
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
139
|
+
*/
|
|
140
|
+
toIso() {
|
|
141
|
+
return this._set(this.x - this.y, (this.x + this.y) * 0.5);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Convert this vector into 2d coordinate space
|
|
146
|
+
* @name to2d
|
|
147
|
+
* @memberof Vector2d
|
|
148
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
149
|
+
*/
|
|
150
|
+
to2d() {
|
|
151
|
+
return this._set(this.y + this.x / 2, this.y - this.x / 2);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Multiply this vector values by the passed vector
|
|
156
|
+
* @name scaleV
|
|
157
|
+
* @memberof Vector2d
|
|
158
|
+
* @param {Vector2d} v
|
|
159
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
160
|
+
*/
|
|
161
|
+
scaleV(v) {
|
|
162
|
+
return this._set(this.x * v.x, this.y * v.y);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Divide this vector values by the passed value
|
|
167
|
+
* @name div
|
|
168
|
+
* @memberof Vector2d
|
|
169
|
+
* @param {number} n - the value to divide the vector by
|
|
170
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
171
|
+
*/
|
|
172
|
+
div(n) {
|
|
173
|
+
return this._set(this.x / n, this.y / n);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Update this vector values to absolute values
|
|
178
|
+
* @name abs
|
|
179
|
+
* @memberof Vector2d
|
|
180
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
181
|
+
*/
|
|
182
|
+
abs() {
|
|
183
|
+
return this._set((this.x < 0) ? -this.x : this.x, (this.y < 0) ? -this.y : this.y);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Clamp the vector value within the specified value range
|
|
188
|
+
* @name clamp
|
|
189
|
+
* @memberof Vector2d
|
|
190
|
+
* @param {number} low
|
|
191
|
+
* @param {number} high
|
|
192
|
+
* @returns {Vector2d} new me.Vector2d
|
|
193
|
+
*/
|
|
194
|
+
clamp(low, high) {
|
|
195
|
+
return new Vector2d(clamp(this.x, low, high), clamp(this.y, low, high));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Clamp this vector value within the specified value range
|
|
200
|
+
* @name clampSelf
|
|
201
|
+
* @memberof Vector2d
|
|
202
|
+
* @param {number} low
|
|
203
|
+
* @param {number} high
|
|
204
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
205
|
+
*/
|
|
206
|
+
clampSelf(low, high) {
|
|
207
|
+
return this._set(clamp(this.x, low, high), clamp(this.y, low, high));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Update this vector with the minimum value between this and the passed vector
|
|
212
|
+
* @name minV
|
|
213
|
+
* @memberof Vector2d
|
|
214
|
+
* @param {Vector2d} v
|
|
215
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
216
|
+
*/
|
|
217
|
+
minV(v) {
|
|
218
|
+
return this._set((this.x < v.x) ? this.x : v.x, (this.y < v.y) ? this.y : v.y);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Update this vector with the maximum value between this and the passed vector
|
|
223
|
+
* @name maxV
|
|
224
|
+
* @memberof Vector2d
|
|
225
|
+
* @param {Vector2d} v
|
|
226
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
227
|
+
*/
|
|
228
|
+
maxV(v) {
|
|
229
|
+
return this._set((this.x > v.x) ? this.x : v.x, (this.y > v.y) ? this.y : v.y);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Floor the vector values
|
|
234
|
+
* @name floor
|
|
235
|
+
* @memberof Vector2d
|
|
236
|
+
* @returns {Vector2d} new me.Vector2d
|
|
237
|
+
*/
|
|
238
|
+
floor() {
|
|
239
|
+
return new Vector2d(Math.floor(this.x), Math.floor(this.y));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Floor this vector values
|
|
244
|
+
* @name floorSelf
|
|
245
|
+
* @memberof Vector2d
|
|
246
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
247
|
+
*/
|
|
248
|
+
floorSelf() {
|
|
249
|
+
return this._set(Math.floor(this.x), Math.floor(this.y));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Ceil the vector values
|
|
254
|
+
* @name ceil
|
|
255
|
+
* @memberof Vector2d
|
|
256
|
+
* @returns {Vector2d} new me.Vector2d
|
|
257
|
+
*/
|
|
258
|
+
ceil() {
|
|
259
|
+
return new Vector2d(Math.ceil(this.x), Math.ceil(this.y));
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Ceil this vector values
|
|
264
|
+
* @name ceilSelf
|
|
265
|
+
* @memberof Vector2d
|
|
266
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
267
|
+
*/
|
|
268
|
+
ceilSelf() {
|
|
269
|
+
return this._set(Math.ceil(this.x), Math.ceil(this.y));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Negate the vector values
|
|
274
|
+
* @name negate
|
|
275
|
+
* @memberof Vector2d
|
|
276
|
+
* @returns {Vector2d} new me.Vector2d
|
|
277
|
+
*/
|
|
278
|
+
negate() {
|
|
279
|
+
return new Vector2d(-this.x, -this.y);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Negate this vector values
|
|
284
|
+
* @name negateSelf
|
|
285
|
+
* @memberof Vector2d
|
|
286
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
287
|
+
*/
|
|
288
|
+
negateSelf() {
|
|
289
|
+
return this._set(-this.x, -this.y);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Copy the x,y values of the passed vector to this one
|
|
294
|
+
* @name copy
|
|
295
|
+
* @memberof Vector2d
|
|
296
|
+
* @param {Vector2d} v
|
|
297
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
298
|
+
*/
|
|
299
|
+
copy(v) {
|
|
300
|
+
return this._set(v.x, v.y);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* return true if the two vectors are the same
|
|
305
|
+
* @name equals
|
|
306
|
+
* @memberof Vector2d
|
|
307
|
+
* @method
|
|
308
|
+
* @param {Vector2d} v
|
|
309
|
+
* @returns {boolean}
|
|
310
|
+
*/
|
|
311
|
+
/**
|
|
312
|
+
* return true if this vector is equal to the given values
|
|
313
|
+
* @name equals
|
|
314
|
+
* @memberof Vector2d
|
|
315
|
+
* @param {number} x
|
|
316
|
+
* @param {number} y
|
|
317
|
+
* @returns {boolean}
|
|
318
|
+
*/
|
|
319
|
+
equals() {
|
|
320
|
+
var _x, _y;
|
|
321
|
+
if (arguments.length === 2) {
|
|
322
|
+
// x, y
|
|
323
|
+
_x = arguments[0];
|
|
324
|
+
_y = arguments[1];
|
|
325
|
+
} else {
|
|
326
|
+
// vector
|
|
327
|
+
_x = arguments[0].x;
|
|
328
|
+
_y = arguments[0].y;
|
|
329
|
+
}
|
|
330
|
+
return ((this.x === _x) && (this.y === _y));
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* normalize this vector (scale the vector so that its magnitude is 1)
|
|
335
|
+
* @name normalize
|
|
336
|
+
* @memberof Vector2d
|
|
337
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
338
|
+
*/
|
|
339
|
+
normalize() {
|
|
340
|
+
return this.div(this.length() || 1);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* change this vector to be perpendicular to what it was before.<br>
|
|
345
|
+
* (Effectively rotates it 90 degrees in a clockwise direction)
|
|
346
|
+
* @name perp
|
|
347
|
+
* @memberof Vector2d
|
|
348
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
349
|
+
*/
|
|
350
|
+
perp() {
|
|
351
|
+
return this._set(this.y, -this.x);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Rotate this vector (counter-clockwise) by the specified angle (in radians).
|
|
356
|
+
* @name rotate
|
|
357
|
+
* @memberof Vector2d
|
|
358
|
+
* @param {number} angle - The angle to rotate (in radians)
|
|
359
|
+
* @param {Vector2d|ObservableVector2d} [v] - an optional point to rotate around
|
|
360
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
361
|
+
*/
|
|
362
|
+
rotate(angle, v) {
|
|
363
|
+
var cx = 0;
|
|
364
|
+
var cy = 0;
|
|
365
|
+
|
|
366
|
+
if (typeof v === "object") {
|
|
367
|
+
cx = v.x;
|
|
368
|
+
cy = v.y;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
var x = this.x - cx;
|
|
372
|
+
var y = this.y - cy;
|
|
373
|
+
|
|
374
|
+
var c = Math.cos(angle);
|
|
375
|
+
var s = Math.sin(angle);
|
|
376
|
+
|
|
377
|
+
return this._set(x * c - y * s + cx, x * s + y * c + cy);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* return the dot product of this vector and the passed one
|
|
382
|
+
* @name dot
|
|
383
|
+
* @memberof Vector2d
|
|
384
|
+
* @param {Vector2d} v
|
|
385
|
+
* @returns {number} The dot product.
|
|
386
|
+
*/
|
|
387
|
+
dot(v) {
|
|
388
|
+
return this.x * v.x + this.y * v.y;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* return the cross product of this vector and the passed one
|
|
393
|
+
* @name cross
|
|
394
|
+
* @memberof Vector2d
|
|
395
|
+
* @param {Vector2d} v
|
|
396
|
+
* @returns {number} The cross product.
|
|
397
|
+
*/
|
|
398
|
+
cross(v) {
|
|
399
|
+
return this.x * v.y - this.y * v.x;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* return the square length of this vector
|
|
404
|
+
* @name length2
|
|
405
|
+
* @memberof Vector2d
|
|
406
|
+
* @returns {number} The length^2 of this vector.
|
|
407
|
+
*/
|
|
408
|
+
length2() {
|
|
409
|
+
return this.dot(this);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* return the length (magnitude) of this vector
|
|
414
|
+
* @name length
|
|
415
|
+
* @memberof Vector2d
|
|
416
|
+
* @returns {number} the length of this vector
|
|
417
|
+
*/
|
|
418
|
+
length() {
|
|
419
|
+
return Math.sqrt(this.length2());
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Linearly interpolate between this vector and the given one.
|
|
424
|
+
* @name lerp
|
|
425
|
+
* @memberof Vector2d
|
|
426
|
+
* @param {Vector2d} v
|
|
427
|
+
* @param {number} alpha - distance along the line (alpha = 0 will be this vector, and alpha = 1 will be the given one).
|
|
428
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
429
|
+
*/
|
|
430
|
+
lerp(v, alpha) {
|
|
431
|
+
this.x += ( v.x - this.x ) * alpha;
|
|
432
|
+
this.y += ( v.y - this.y ) * alpha;
|
|
433
|
+
return this;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* interpolate the position of this vector towards the given one by the given maximum step.
|
|
438
|
+
* @name moveTowards
|
|
439
|
+
* @memberof Vector2d
|
|
440
|
+
* @param {Vector2d} target
|
|
441
|
+
* @param {number} step - the maximum step per iteration (Negative values will push the vector away from the target)
|
|
442
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
443
|
+
*/
|
|
444
|
+
moveTowards(target, step) {
|
|
445
|
+
var angle = Math.atan2(target.y - this.y, target.x - this.x);
|
|
446
|
+
|
|
447
|
+
var distance = this.distance(target);
|
|
448
|
+
|
|
449
|
+
if (distance === 0 || (step >= 0 && distance <= step * step)) {
|
|
450
|
+
return target;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
this.x += Math.cos(angle) * step;
|
|
454
|
+
this.y += Math.sin(angle) * step;
|
|
455
|
+
|
|
456
|
+
return this;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* return the distance between this vector and the passed one
|
|
461
|
+
* @name distance
|
|
462
|
+
* @memberof Vector2d
|
|
463
|
+
* @param {Vector2d} v
|
|
464
|
+
* @returns {number}
|
|
465
|
+
*/
|
|
466
|
+
distance(v) {
|
|
467
|
+
var dx = this.x - v.x, dy = this.y - v.y;
|
|
468
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* return the angle between this vector and the passed one
|
|
473
|
+
* @name angle
|
|
474
|
+
* @memberof Vector2d
|
|
475
|
+
* @param {Vector2d} v
|
|
476
|
+
* @returns {number} angle in radians
|
|
477
|
+
*/
|
|
478
|
+
angle(v) {
|
|
479
|
+
return Math.acos(clamp(this.dot(v) / (this.length() * v.length()), -1, 1));
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* project this vector on to another vector.
|
|
484
|
+
* @name project
|
|
485
|
+
* @memberof Vector2d
|
|
486
|
+
* @param {Vector2d} v - The vector to project onto.
|
|
487
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
488
|
+
*/
|
|
489
|
+
project(v) {
|
|
490
|
+
return this.scale(this.dot(v) / v.length2());
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Project this vector onto a vector of unit length.<br>
|
|
495
|
+
* This is slightly more efficient than `project` when dealing with unit vectors.
|
|
496
|
+
* @name projectN
|
|
497
|
+
* @memberof Vector2d
|
|
498
|
+
* @param {Vector2d} v - The unit vector to project onto.
|
|
499
|
+
* @returns {Vector2d} Reference to this object for method chaining
|
|
500
|
+
*/
|
|
501
|
+
projectN(v) {
|
|
502
|
+
return this.scale(this.dot(v));
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* return a clone copy of this vector
|
|
507
|
+
* @name clone
|
|
508
|
+
* @memberof Vector2d
|
|
509
|
+
* @returns {Vector2d} new me.Vector2d
|
|
510
|
+
*/
|
|
511
|
+
clone() {
|
|
512
|
+
return pool.pull("Vector2d", this.x, this.y);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* convert the object to a string representation
|
|
517
|
+
* @name toString
|
|
518
|
+
* @memberof Vector2d
|
|
519
|
+
* @returns {string}
|
|
520
|
+
*/
|
|
521
|
+
toString() {
|
|
522
|
+
return "x:" + this.x + ",y:" + this.y;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
var Vector2d$1 = Vector2d;
|
|
527
|
+
|
|
528
|
+
export { Vector2d$1 as default };
|