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,559 @@
|
|
|
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 Vector3d from './vector3.js';
|
|
9
|
+
import { clamp } from './math.js';
|
|
10
|
+
import pool from '../system/pooling.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @classdesc
|
|
14
|
+
* A Vector3d object that provide notification by executing the given callback when the vector is changed.
|
|
15
|
+
* @augments Vector3d
|
|
16
|
+
*/
|
|
17
|
+
class ObservableVector3d extends Vector3d {
|
|
18
|
+
/**
|
|
19
|
+
* @param {number} x - x value of the vector
|
|
20
|
+
* @param {number} y - y value of the vector
|
|
21
|
+
* @param {number} z - z value of the vector
|
|
22
|
+
* @param {object} settings - additional required parameters
|
|
23
|
+
* @param {Function} settings.onUpdate - the callback to be executed when the vector is changed
|
|
24
|
+
* @param {object} [settings.scope] - the value to use as this when calling onUpdate
|
|
25
|
+
*/
|
|
26
|
+
constructor(x = 0, y = 0, z = 0, settings) {
|
|
27
|
+
super(x, y, z);
|
|
28
|
+
if (typeof(settings) === "undefined") {
|
|
29
|
+
throw new Error(
|
|
30
|
+
"undefined `onUpdate` callback"
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
this.setCallback(settings.onUpdate, settings.scope);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @ignore
|
|
38
|
+
*/
|
|
39
|
+
onResetEvent(x = 0, y = 0, z = 0, settings) {
|
|
40
|
+
// init is call by the constructor and does not trigger the cb
|
|
41
|
+
this.setMuted(x, y, z);
|
|
42
|
+
if (typeof settings !== "undefined") {
|
|
43
|
+
this.setCallback(settings.onUpdate, settings.scope);
|
|
44
|
+
}
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* x value of the vector
|
|
50
|
+
* @public
|
|
51
|
+
* @type {number}
|
|
52
|
+
* @name x
|
|
53
|
+
* @memberof ObservableVector3d
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
get x() {
|
|
57
|
+
return this._x;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
set x(value) {
|
|
61
|
+
var ret = this.onUpdate.call(this.scope, value, this._y, this._z, this._x, this._y, this._z);
|
|
62
|
+
if (ret && "x" in ret) {
|
|
63
|
+
this._x = ret.x;
|
|
64
|
+
} else {
|
|
65
|
+
this._x = value;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* y value of the vector
|
|
71
|
+
* @public
|
|
72
|
+
* @type {number}
|
|
73
|
+
* @name y
|
|
74
|
+
* @memberof ObservableVector3d
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
get y() {
|
|
78
|
+
return this._y;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
set y(value) {
|
|
82
|
+
var ret = this.onUpdate.call(this.scope, this._x, value, this._z, this._x, this._y, this._z);
|
|
83
|
+
if (ret && "y" in ret) {
|
|
84
|
+
this._y = ret.y;
|
|
85
|
+
} else {
|
|
86
|
+
this._y = value;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* z value of the vector
|
|
93
|
+
* @public
|
|
94
|
+
* @type {number}
|
|
95
|
+
* @name z
|
|
96
|
+
* @memberof ObservableVector3d
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
get z() {
|
|
101
|
+
return this._z;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
set z(value) {
|
|
105
|
+
var ret = this.onUpdate.call(this.scope, this._x, this._y, value, this._x, this._y, this._z);
|
|
106
|
+
if (ret && "z" in ret) {
|
|
107
|
+
this._z = ret.z;
|
|
108
|
+
} else {
|
|
109
|
+
this._z = value;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @ignore
|
|
115
|
+
*/
|
|
116
|
+
_set(x, y, z) {
|
|
117
|
+
var ret = this.onUpdate.call(this.scope, x, y, z, this._x, this._y, this._z);
|
|
118
|
+
if (ret && "x" in ret && "y" in ret && "z" in ret) {
|
|
119
|
+
this._x = ret.x;
|
|
120
|
+
this._y = ret.y;
|
|
121
|
+
this._z = ret.z;
|
|
122
|
+
} else {
|
|
123
|
+
this._x = x;
|
|
124
|
+
this._y = y;
|
|
125
|
+
this._z = z || 0;
|
|
126
|
+
}
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* set the vector value without triggering the callback
|
|
132
|
+
* @name setMuted
|
|
133
|
+
* @memberof ObservableVector3d
|
|
134
|
+
* @param {number} x - x value of the vector
|
|
135
|
+
* @param {number} y - y value of the vector
|
|
136
|
+
* @param {number} [z=0] - z value of the vector
|
|
137
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
138
|
+
*/
|
|
139
|
+
setMuted(x, y, z) {
|
|
140
|
+
this._x = x;
|
|
141
|
+
this._y = y;
|
|
142
|
+
this._z = z || 0;
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* set the callback to be executed when the vector is changed
|
|
148
|
+
* @name setCallback
|
|
149
|
+
* @memberof ObservableVector3d
|
|
150
|
+
* @param {Function} fn - callback
|
|
151
|
+
* @param {Function} [scope=null] - scope
|
|
152
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
153
|
+
*/
|
|
154
|
+
setCallback(fn, scope = null) {
|
|
155
|
+
if (typeof(fn) !== "function") {
|
|
156
|
+
throw new Error(
|
|
157
|
+
"invalid `onUpdate` callback"
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
this.onUpdate = fn;
|
|
161
|
+
this.scope = scope;
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Add the passed vector to this vector
|
|
167
|
+
* @name add
|
|
168
|
+
* @memberof ObservableVector3d
|
|
169
|
+
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
|
|
170
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
171
|
+
*/
|
|
172
|
+
add(v) {
|
|
173
|
+
return this._set(this._x + v.x, this._y + v.y, this._z + (v.z || 0));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Substract the passed vector to this vector
|
|
178
|
+
* @name sub
|
|
179
|
+
* @memberof ObservableVector3d
|
|
180
|
+
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
|
|
181
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
182
|
+
*/
|
|
183
|
+
sub(v) {
|
|
184
|
+
return this._set(this._x - v.x, this._y - v.y, this._z - (v.z || 0));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Multiply this vector values by the given scalar
|
|
189
|
+
* @name scale
|
|
190
|
+
* @memberof ObservableVector3d
|
|
191
|
+
* @param {number} x
|
|
192
|
+
* @param {number} [y=x]
|
|
193
|
+
* @param {number} [z=1]
|
|
194
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
195
|
+
*/
|
|
196
|
+
scale(x, y, z) {
|
|
197
|
+
y = (typeof (y) !== "undefined" ? y : x);
|
|
198
|
+
return this._set(this._x * x, this._y * y, this._z * (z || 1));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Multiply this vector values by the passed vector
|
|
203
|
+
* @name scaleV
|
|
204
|
+
* @memberof ObservableVector3d
|
|
205
|
+
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
|
|
206
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
207
|
+
*/
|
|
208
|
+
scaleV(v) {
|
|
209
|
+
return this._set(this._x * v.x, this._y * v.y, this._z * (v.z || 1));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Divide this vector values by the passed value
|
|
214
|
+
* @name div
|
|
215
|
+
* @memberof ObservableVector3d
|
|
216
|
+
* @param {number} n - the value to divide the vector by
|
|
217
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
218
|
+
*/
|
|
219
|
+
div(n) {
|
|
220
|
+
return this._set(this._x / n, this._y / n, this._z / n);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Update this vector values to absolute values
|
|
225
|
+
* @name abs
|
|
226
|
+
* @memberof ObservableVector3d
|
|
227
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
228
|
+
*/
|
|
229
|
+
abs() {
|
|
230
|
+
return this._set(
|
|
231
|
+
(this._x < 0) ? -this._x : this._x,
|
|
232
|
+
(this._y < 0) ? -this._y : this._y,
|
|
233
|
+
(this._Z < 0) ? -this._z : this._z
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Clamp the vector value within the specified value range
|
|
239
|
+
* @name clamp
|
|
240
|
+
* @memberof ObservableVector3d
|
|
241
|
+
* @param {number} low
|
|
242
|
+
* @param {number} high
|
|
243
|
+
* @returns {ObservableVector3d} new me.ObservableVector3d
|
|
244
|
+
*/
|
|
245
|
+
clamp(low, high) {
|
|
246
|
+
return new ObservableVector3d(
|
|
247
|
+
clamp(this._x, low, high),
|
|
248
|
+
clamp(this._y, low, high),
|
|
249
|
+
clamp(this._z, low, high),
|
|
250
|
+
{onUpdate: this.onUpdate, scope: this.scope}
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Clamp this vector value within the specified value range
|
|
256
|
+
* @name clampSelf
|
|
257
|
+
* @memberof ObservableVector3d
|
|
258
|
+
* @param {number} low
|
|
259
|
+
* @param {number} high
|
|
260
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
261
|
+
*/
|
|
262
|
+
clampSelf(low, high) {
|
|
263
|
+
return this._set(
|
|
264
|
+
clamp(this._x, low, high),
|
|
265
|
+
clamp(this._y, low, high),
|
|
266
|
+
clamp(this._z, low, high)
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Update this vector with the minimum value between this and the passed vector
|
|
272
|
+
* @name minV
|
|
273
|
+
* @memberof ObservableVector3d
|
|
274
|
+
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
|
|
275
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
276
|
+
*/
|
|
277
|
+
minV(v) {
|
|
278
|
+
var _vz = v.z || 0;
|
|
279
|
+
return this._set(
|
|
280
|
+
(this._x < v.x) ? this._x : v.x,
|
|
281
|
+
(this._y < v.y) ? this._y : v.y,
|
|
282
|
+
(this._z < _vz) ? this._z : _vz
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Update this vector with the maximum value between this and the passed vector
|
|
288
|
+
* @name maxV
|
|
289
|
+
* @memberof ObservableVector3d
|
|
290
|
+
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
|
|
291
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
292
|
+
*/
|
|
293
|
+
maxV(v) {
|
|
294
|
+
var _vz = v.z || 0;
|
|
295
|
+
return this._set(
|
|
296
|
+
(this._x > v.x) ? this._x : v.x,
|
|
297
|
+
(this._y > v.y) ? this._y : v.y,
|
|
298
|
+
(this._z > _vz) ? this._z : _vz
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Floor the vector values
|
|
304
|
+
* @name floor
|
|
305
|
+
* @memberof ObservableVector3d
|
|
306
|
+
* @returns {ObservableVector3d} new me.ObservableVector3d
|
|
307
|
+
*/
|
|
308
|
+
floor() {
|
|
309
|
+
return new ObservableVector3d(
|
|
310
|
+
Math.floor(this._x),
|
|
311
|
+
Math.floor(this._y),
|
|
312
|
+
Math.floor(this._z),
|
|
313
|
+
{onUpdate: this.onUpdate, scope: this.scope}
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Floor this vector values
|
|
319
|
+
* @name floorSelf
|
|
320
|
+
* @memberof ObservableVector3d
|
|
321
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
322
|
+
*/
|
|
323
|
+
floorSelf() {
|
|
324
|
+
return this._set(Math.floor(this._x), Math.floor(this._y), Math.floor(this._z));
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Ceil the vector values
|
|
329
|
+
* @name ceil
|
|
330
|
+
* @memberof ObservableVector3d
|
|
331
|
+
* @returns {ObservableVector3d} new me.ObservableVector3d
|
|
332
|
+
*/
|
|
333
|
+
ceil() {
|
|
334
|
+
return new ObservableVector3d(
|
|
335
|
+
Math.ceil(this._x),
|
|
336
|
+
Math.ceil(this._y),
|
|
337
|
+
Math.ceil(this._z),
|
|
338
|
+
{onUpdate: this.onUpdate, scope: this.scope}
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Ceil this vector values
|
|
344
|
+
* @name ceilSelf
|
|
345
|
+
* @memberof ObservableVector3d
|
|
346
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
347
|
+
*/
|
|
348
|
+
ceilSelf() {
|
|
349
|
+
return this._set(Math.ceil(this._x), Math.ceil(this._y), Math.ceil(this._z));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Negate the vector values
|
|
354
|
+
* @name negate
|
|
355
|
+
* @memberof ObservableVector3d
|
|
356
|
+
* @returns {ObservableVector3d} new me.ObservableVector3d
|
|
357
|
+
*/
|
|
358
|
+
negate() {
|
|
359
|
+
return new ObservableVector3d(
|
|
360
|
+
-this._x,
|
|
361
|
+
-this._y,
|
|
362
|
+
-this._z,
|
|
363
|
+
{onUpdate: this.onUpdate, scope: this.scope}
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Negate this vector values
|
|
369
|
+
* @name negateSelf
|
|
370
|
+
* @memberof ObservableVector3d
|
|
371
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
372
|
+
*/
|
|
373
|
+
negateSelf() {
|
|
374
|
+
return this._set(-this._x, -this._y, -this._z);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Copy the components of the given vector into this one
|
|
379
|
+
* @name copy
|
|
380
|
+
* @memberof ObservableVector3d
|
|
381
|
+
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
|
|
382
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
383
|
+
*/
|
|
384
|
+
copy(v) {
|
|
385
|
+
return this._set(v.x, v.y, v.z || 0);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* return true if the two vectors are the same
|
|
390
|
+
* @name equals
|
|
391
|
+
* @memberof ObservableVector3d
|
|
392
|
+
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
|
|
393
|
+
* @returns {boolean}
|
|
394
|
+
*/
|
|
395
|
+
equals(v) {
|
|
396
|
+
return ((this._x === v.x) && (this._y === v.y) && (this._z === (v.z || this._z)));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* change this vector to be perpendicular to what it was before.<br>
|
|
401
|
+
* (Effectively rotates it 90 degrees in a clockwise direction)
|
|
402
|
+
* @name perp
|
|
403
|
+
* @memberof ObservableVector3d
|
|
404
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
405
|
+
*/
|
|
406
|
+
perp() {
|
|
407
|
+
return this._set(this._y, -this._x, this._z);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Rotate this vector (counter-clockwise) by the specified angle (in radians).
|
|
412
|
+
* @name rotate
|
|
413
|
+
* @memberof ObservableVector3d
|
|
414
|
+
* @param {number} angle - The angle to rotate (in radians)
|
|
415
|
+
* @param {Vector2d|ObservableVector2d} [v] - an optional point to rotate around (on the same z axis)
|
|
416
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
417
|
+
*/
|
|
418
|
+
rotate(angle, v) {
|
|
419
|
+
var cx = 0;
|
|
420
|
+
var cy = 0;
|
|
421
|
+
|
|
422
|
+
if (typeof v === "object") {
|
|
423
|
+
cx = v.x;
|
|
424
|
+
cy = v.y;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// TODO also rotate on the z axis if the given vector is a 3d one
|
|
428
|
+
var x = this.x - cx;
|
|
429
|
+
var y = this.y - cy;
|
|
430
|
+
|
|
431
|
+
var c = Math.cos(angle);
|
|
432
|
+
var s = Math.sin(angle);
|
|
433
|
+
|
|
434
|
+
return this._set(x * c - y * s + cx, x * s + y * c + cy, this.z);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* return the dot product of this vector and the passed one
|
|
439
|
+
* @name dot
|
|
440
|
+
* @memberof ObservableVector3d
|
|
441
|
+
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
|
|
442
|
+
* @returns {number} The dot product.
|
|
443
|
+
*/
|
|
444
|
+
dot(v) {
|
|
445
|
+
return this._x * v.x + this._y * v.y + this._z * (v.z || 1);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* calculate the cross product of this vector and the passed one
|
|
450
|
+
* @name cross
|
|
451
|
+
* @memberof ObservableVector3d
|
|
452
|
+
* @param {Vector3d|ObservableVector3d} v
|
|
453
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
454
|
+
*/
|
|
455
|
+
cross(v) {
|
|
456
|
+
var ax = this._x, ay = this._y, az = this._z;
|
|
457
|
+
var bx = v.x, by = v.y, bz = v.z;
|
|
458
|
+
|
|
459
|
+
return this._set(
|
|
460
|
+
ay * bz - az * by,
|
|
461
|
+
az * bx - ax * bz,
|
|
462
|
+
ax * by - ay * bx
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Linearly interpolate between this vector and the given one.
|
|
468
|
+
* @name lerp
|
|
469
|
+
* @memberof ObservableVector3d
|
|
470
|
+
* @param {Vector3d|ObservableVector3d} v
|
|
471
|
+
* @param {number} alpha - distance along the line (alpha = 0 will be this vector, and alpha = 1 will be the given one).
|
|
472
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
473
|
+
*/
|
|
474
|
+
lerp(v, alpha) {
|
|
475
|
+
return this._set(
|
|
476
|
+
this._x + ( v.x - this._x ) * alpha,
|
|
477
|
+
this._y + ( v.y - this._y ) * alpha,
|
|
478
|
+
this._z + ( v.z - this._z ) * alpha
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* interpolate the position of this vector on the x and y axis towards the given one while ensure that the distance never exceeds the given step.
|
|
484
|
+
* @name moveTowards
|
|
485
|
+
* @memberof ObservableVector3d
|
|
486
|
+
* @param {Vector2d|ObservableVector2d|Vector3d|ObservableVector3d} target
|
|
487
|
+
* @param {number} step - the maximum step per iteration (Negative values will push the vector away from the target)
|
|
488
|
+
* @returns {ObservableVector3d} Reference to this object for method chaining
|
|
489
|
+
*/
|
|
490
|
+
moveTowards(target, step) {
|
|
491
|
+
var angle = Math.atan2(target.y - this._y, target.x - this._x);
|
|
492
|
+
|
|
493
|
+
var dx = this._x - target.x;
|
|
494
|
+
var dy = this._y - target.y;
|
|
495
|
+
|
|
496
|
+
var distance = Math.sqrt(dx * dx + dy * dy);
|
|
497
|
+
|
|
498
|
+
if (distance === 0 || (step >= 0 && distance <= step * step)) {
|
|
499
|
+
return target;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
return this._set(
|
|
503
|
+
this._x + Math.cos(angle) * step,
|
|
504
|
+
this._y + Math.sin(angle) * step,
|
|
505
|
+
this._z
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* return the distance between this vector and the passed one
|
|
511
|
+
* @name distance
|
|
512
|
+
* @memberof ObservableVector3d
|
|
513
|
+
* @param {Vector2d|Vector3d|ObservableVector2d|ObservableVector3d} v
|
|
514
|
+
* @returns {number}
|
|
515
|
+
*/
|
|
516
|
+
distance(v) {
|
|
517
|
+
var dx = this._x - v.x;
|
|
518
|
+
var dy = this._y - v.y;
|
|
519
|
+
var dz = this._z - (v.z || 0);
|
|
520
|
+
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* return a clone copy of this vector
|
|
525
|
+
* @name clone
|
|
526
|
+
* @memberof ObservableVector3d
|
|
527
|
+
* @returns {ObservableVector3d} new me.ObservableVector3d
|
|
528
|
+
*/
|
|
529
|
+
clone() {
|
|
530
|
+
return pool.pull("ObservableVector3d",
|
|
531
|
+
this._x,
|
|
532
|
+
this._y,
|
|
533
|
+
this._z,
|
|
534
|
+
{onUpdate: this.onUpdate}
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* return a `me.Vector3d` copy of this `me.ObservableVector3d` object
|
|
540
|
+
* @name toVector3d
|
|
541
|
+
* @memberof ObservableVector3d
|
|
542
|
+
* @returns {Vector3d} new me.Vector3d
|
|
543
|
+
*/
|
|
544
|
+
toVector3d() {
|
|
545
|
+
return pool.pull("Vector3d", this._x, this._y, this._z);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* convert the object to a string representation
|
|
550
|
+
* @name toString
|
|
551
|
+
* @memberof ObservableVector3d
|
|
552
|
+
* @returns {string}
|
|
553
|
+
*/
|
|
554
|
+
toString() {
|
|
555
|
+
return "x:" + this._x + ",y:" + this._y + ",z:" + this._z;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export { ObservableVector3d as default };
|