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,218 @@
|
|
|
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
|
+
/**
|
|
9
|
+
* a collection of math utility functions
|
|
10
|
+
* @namespace Math
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* constant to convert from degrees to radians
|
|
15
|
+
* @public
|
|
16
|
+
* @type {number}
|
|
17
|
+
* @name DEG_TO_RAD
|
|
18
|
+
* @memberof Math
|
|
19
|
+
*/
|
|
20
|
+
const DEG_TO_RAD = Math.PI / 180.0;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* constant to convert from radians to degrees
|
|
24
|
+
* @public
|
|
25
|
+
* @type {number}
|
|
26
|
+
* @name RAD_TO_DEG
|
|
27
|
+
* @memberof Math
|
|
28
|
+
*/
|
|
29
|
+
const RAD_TO_DEG = 180.0 / Math.PI;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* constant equals to 2 times pi
|
|
33
|
+
* @public
|
|
34
|
+
* @type {number}
|
|
35
|
+
* @name TAU
|
|
36
|
+
* @memberof Math
|
|
37
|
+
*/
|
|
38
|
+
const TAU = Math.PI * 2;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* constant equals to half pi
|
|
42
|
+
* @public
|
|
43
|
+
* @type {number}
|
|
44
|
+
* @name ETA
|
|
45
|
+
* @memberof Math
|
|
46
|
+
*/
|
|
47
|
+
const ETA = Math.PI * 0.5;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* the difference between 1 and the smallest floating point number greater than 1
|
|
51
|
+
* @public
|
|
52
|
+
* @type {number}
|
|
53
|
+
* @name EPSILON
|
|
54
|
+
* @memberof Math
|
|
55
|
+
*/
|
|
56
|
+
const EPSILON = 0.000001;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* returns true if the given value is a power of two
|
|
60
|
+
* @public
|
|
61
|
+
* @memberof Math
|
|
62
|
+
* @name isPowerOfTwo
|
|
63
|
+
* @param {number} val
|
|
64
|
+
* @returns {boolean}
|
|
65
|
+
*/
|
|
66
|
+
function isPowerOfTwo(val) {
|
|
67
|
+
return (val & (val - 1)) === 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* returns the next power of two for the given value
|
|
72
|
+
* @public
|
|
73
|
+
* @memberof Math
|
|
74
|
+
* @name nextPowerOfTwo
|
|
75
|
+
* @param {number} val
|
|
76
|
+
* @returns {boolean}
|
|
77
|
+
*/
|
|
78
|
+
function nextPowerOfTwo(val) {
|
|
79
|
+
val --;
|
|
80
|
+
val |= val >> 1;
|
|
81
|
+
val |= val >> 2;
|
|
82
|
+
val |= val >> 4;
|
|
83
|
+
val |= val >> 8;
|
|
84
|
+
val |= val >> 16;
|
|
85
|
+
val ++;
|
|
86
|
+
return val;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Converts an angle in degrees to an angle in radians
|
|
91
|
+
* @public
|
|
92
|
+
* @memberof Math
|
|
93
|
+
* @name degToRad
|
|
94
|
+
* @param {number} angle - angle in degrees
|
|
95
|
+
* @returns {number} corresponding angle in radians
|
|
96
|
+
* @example
|
|
97
|
+
* // convert a specific angle
|
|
98
|
+
* me.Math.degToRad(60); // return 1.0471...
|
|
99
|
+
*/
|
|
100
|
+
function degToRad(angle) {
|
|
101
|
+
return angle * DEG_TO_RAD;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Converts an angle in radians to an angle in degrees.
|
|
106
|
+
* @public
|
|
107
|
+
* @memberof Math
|
|
108
|
+
* @name radToDeg
|
|
109
|
+
* @param {number} radians - angle in radians
|
|
110
|
+
* @returns {number} corresponding angle in degrees
|
|
111
|
+
* @example
|
|
112
|
+
* // convert a specific angle
|
|
113
|
+
* me.Math.radToDeg(1.0471975511965976); // return 60
|
|
114
|
+
*/
|
|
115
|
+
function radToDeg(radians) {
|
|
116
|
+
return radians * RAD_TO_DEG;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* clamp the given value
|
|
121
|
+
* @public
|
|
122
|
+
* @memberof Math
|
|
123
|
+
* @name clamp
|
|
124
|
+
* @param {number} val - the value to clamp
|
|
125
|
+
* @param {number} low - lower limit
|
|
126
|
+
* @param {number} high - higher limit
|
|
127
|
+
* @returns {number} clamped value
|
|
128
|
+
*/
|
|
129
|
+
function clamp(val, low, high) {
|
|
130
|
+
return val < low ? low : val > high ? high : +val;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* return a random integer between min (included) and max (excluded)
|
|
135
|
+
* @public
|
|
136
|
+
* @memberof Math
|
|
137
|
+
* @name random
|
|
138
|
+
* @param {number} min - minimum value.
|
|
139
|
+
* @param {number} max - maximum value.
|
|
140
|
+
* @returns {number} random value
|
|
141
|
+
* @example
|
|
142
|
+
* // Print a random number; one of 5, 6, 7, 8, 9
|
|
143
|
+
* console.log(me.Math.random(5, 10) );
|
|
144
|
+
*/
|
|
145
|
+
function random(min, max) {
|
|
146
|
+
return (~~(Math.random() * (max - min)) + min);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* return a random float between min, max (exclusive)
|
|
151
|
+
* @public
|
|
152
|
+
* @memberof Math
|
|
153
|
+
* @name randomFloat
|
|
154
|
+
* @param {number} min - minimum value.
|
|
155
|
+
* @param {number} max - maximum value.
|
|
156
|
+
* @returns {number} random value
|
|
157
|
+
* @example
|
|
158
|
+
* // Print a random number; one of 5, 6, 7, 8, 9
|
|
159
|
+
* console.log(me.Math.randomFloat(5, 10) );
|
|
160
|
+
*/
|
|
161
|
+
function randomFloat(min, max) {
|
|
162
|
+
return (Math.random() * (max - min)) + min;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* return a weighted random between min, max (exclusive)
|
|
167
|
+
* @public
|
|
168
|
+
* @memberof Math
|
|
169
|
+
* @name weightedRandom
|
|
170
|
+
* @param {number} min - minimum value.
|
|
171
|
+
* @param {number} max - maximum value.
|
|
172
|
+
* @returns {number} random value
|
|
173
|
+
* @example
|
|
174
|
+
* // Print a random number; one of 5, 6, 7, 8, 9
|
|
175
|
+
* console.log(me.Math.weightedRandom(5, 10) );
|
|
176
|
+
*/
|
|
177
|
+
function weightedRandom(min, max) {
|
|
178
|
+
return (~~(Math.pow(Math.random(), 2) * (max - min)) + min);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* round a value to the specified number of digit
|
|
183
|
+
* @public
|
|
184
|
+
* @memberof Math
|
|
185
|
+
* @name round
|
|
186
|
+
* @param {number} num - value to be rounded.
|
|
187
|
+
* @param {number} [dec=0] - number of decimal digit to be rounded to.
|
|
188
|
+
* @returns {number} rounded value
|
|
189
|
+
* @example
|
|
190
|
+
* // round a specific value to 2 digits
|
|
191
|
+
* me.Math.round(10.33333, 2); // return 10.33
|
|
192
|
+
*/
|
|
193
|
+
function round(num, dec = 0) {
|
|
194
|
+
// if only one argument use the object value
|
|
195
|
+
var powres = Math.pow(10, dec);
|
|
196
|
+
return (~~(0.5 + num * powres) / powres);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* check if the given value is close to the expected one
|
|
201
|
+
* @public
|
|
202
|
+
* @memberof Math
|
|
203
|
+
* @name toBeCloseTo
|
|
204
|
+
* @param {number} expected - value to be compared with.
|
|
205
|
+
* @param {number} actual - actual value to compare
|
|
206
|
+
* @param {number} [precision=2] - float precision for the comparison
|
|
207
|
+
* @returns {boolean} if close to
|
|
208
|
+
* @example
|
|
209
|
+
* // test if the given value is close to 10
|
|
210
|
+
* if (me.Math.toBeCloseTo(10, value)) {
|
|
211
|
+
* // do something
|
|
212
|
+
* }
|
|
213
|
+
*/
|
|
214
|
+
function toBeCloseTo(expected, actual, precision = 2) {
|
|
215
|
+
return Math.abs(expected - actual) < (Math.pow(10, -precision) / 2);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export { DEG_TO_RAD, EPSILON, ETA, RAD_TO_DEG, TAU, clamp, degToRad, isPowerOfTwo, nextPowerOfTwo, radToDeg, random, randomFloat, round, toBeCloseTo, weightedRandom };
|
|
@@ -0,0 +1,503 @@
|
|
|
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 pool from '../system/pooling.js';
|
|
9
|
+
import Matrix3d from './matrix3.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @classdesc
|
|
13
|
+
* a Matrix2d Object.<br>
|
|
14
|
+
* the identity matrix and parameters position : <br>
|
|
15
|
+
* <img src="images/identity-matrix_2x.png"/>
|
|
16
|
+
*/
|
|
17
|
+
class Matrix2d {
|
|
18
|
+
/**
|
|
19
|
+
* @param {(Matrix2d|Matrix3d|...number)} args - an instance of me.Matrix2d or me.Matrix3d to copy from, or individual matrix components (See {@link Matrix2d.setTransform}). If not arguments are given, the matrix will be set to Identity.
|
|
20
|
+
*/
|
|
21
|
+
constructor(...args) {
|
|
22
|
+
this.onResetEvent(...args);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @ignore
|
|
27
|
+
*/
|
|
28
|
+
onResetEvent() {
|
|
29
|
+
if (typeof(this.val) === "undefined") {
|
|
30
|
+
this.val = new Float32Array(9);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (arguments.length && arguments[0] instanceof Matrix2d) {
|
|
34
|
+
this.copy(arguments[0]);
|
|
35
|
+
}
|
|
36
|
+
else if (arguments.length && arguments[0] instanceof Matrix3d) {
|
|
37
|
+
this.fromMat3d(arguments[0]);
|
|
38
|
+
}
|
|
39
|
+
else if (arguments.length >= 6) {
|
|
40
|
+
this.setTransform.apply(this, arguments);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
this.identity();
|
|
44
|
+
}
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* tx component of the matrix
|
|
50
|
+
* @public
|
|
51
|
+
* @type {number}
|
|
52
|
+
* @see Matrix2d.translate
|
|
53
|
+
* @name tx
|
|
54
|
+
* @memberof Matrix2d
|
|
55
|
+
*/
|
|
56
|
+
get tx() {
|
|
57
|
+
return this.val[6];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* ty component of the matrix
|
|
62
|
+
* @public
|
|
63
|
+
* @type {number}
|
|
64
|
+
* @see Matrix2d.translate
|
|
65
|
+
* @name ty
|
|
66
|
+
* @memberof Matrix2d
|
|
67
|
+
*/
|
|
68
|
+
get ty() {
|
|
69
|
+
return this.val[7];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* reset the transformation matrix to the identity matrix (no transformation).<br>
|
|
74
|
+
* the identity matrix and parameters position : <br>
|
|
75
|
+
* <img src="images/identity-matrix_2x.png"/>
|
|
76
|
+
* @name identity
|
|
77
|
+
* @memberof Matrix2d
|
|
78
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
79
|
+
*/
|
|
80
|
+
identity() {
|
|
81
|
+
this.setTransform(
|
|
82
|
+
1, 0, 0,
|
|
83
|
+
0, 1, 0,
|
|
84
|
+
0, 0, 1
|
|
85
|
+
);
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* set the matrix to the specified value
|
|
91
|
+
* @name setTransform
|
|
92
|
+
* @memberof Matrix2d
|
|
93
|
+
* @param {number} a
|
|
94
|
+
* @param {number} b
|
|
95
|
+
* @param {number} c
|
|
96
|
+
* @param {number} d
|
|
97
|
+
* @param {number} e
|
|
98
|
+
* @param {number} f
|
|
99
|
+
* @param {number} [g=0]
|
|
100
|
+
* @param {number} [h=0]
|
|
101
|
+
* @param {number} [i=1]
|
|
102
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
103
|
+
*/
|
|
104
|
+
setTransform() {
|
|
105
|
+
var a = this.val;
|
|
106
|
+
|
|
107
|
+
if (arguments.length === 9) {
|
|
108
|
+
a[0] = arguments[0]; // a - m00
|
|
109
|
+
a[1] = arguments[1]; // b - m10
|
|
110
|
+
a[2] = arguments[2]; // c - m20
|
|
111
|
+
a[3] = arguments[3]; // d - m01
|
|
112
|
+
a[4] = arguments[4]; // e - m11
|
|
113
|
+
a[5] = arguments[5]; // f - m21
|
|
114
|
+
a[6] = arguments[6]; // g - m02
|
|
115
|
+
a[7] = arguments[7]; // h - m12
|
|
116
|
+
a[8] = arguments[8]; // i - m22
|
|
117
|
+
} else if (arguments.length === 6) {
|
|
118
|
+
a[0] = arguments[0]; // a
|
|
119
|
+
a[1] = arguments[2]; // c
|
|
120
|
+
a[2] = arguments[4]; // e
|
|
121
|
+
a[3] = arguments[1]; // b
|
|
122
|
+
a[4] = arguments[3]; // d
|
|
123
|
+
a[5] = arguments[5]; // f
|
|
124
|
+
a[6] = 0; // g
|
|
125
|
+
a[7] = 0; // h
|
|
126
|
+
a[8] = 1; // i
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Copies over the values from another me.Matrix2d.
|
|
134
|
+
* @name copy
|
|
135
|
+
* @memberof Matrix2d
|
|
136
|
+
* @param {Matrix2d} m - the matrix object to copy from
|
|
137
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
138
|
+
*/
|
|
139
|
+
copy(m) {
|
|
140
|
+
this.val.set(m.val);
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Copies over the upper-left 3x3 values from the given me.Matrix3d
|
|
146
|
+
* @name fromMat3d
|
|
147
|
+
* @memberof Matrix2d
|
|
148
|
+
* @param {Matrix3d} m - the matrix object to copy from
|
|
149
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
150
|
+
*/
|
|
151
|
+
fromMat3d(m) {
|
|
152
|
+
var b = m.val;
|
|
153
|
+
var a = this.val;
|
|
154
|
+
|
|
155
|
+
a[0] = b[0];
|
|
156
|
+
a[1] = b[1];
|
|
157
|
+
a[2] = b[2];
|
|
158
|
+
a[3] = b[4];
|
|
159
|
+
a[4] = b[5];
|
|
160
|
+
a[5] = b[6];
|
|
161
|
+
a[6] = b[8];
|
|
162
|
+
a[7] = b[9];
|
|
163
|
+
a[8] = b[10];
|
|
164
|
+
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* multiply both matrix
|
|
170
|
+
* @name multiply
|
|
171
|
+
* @memberof Matrix2d
|
|
172
|
+
* @param {Matrix2d} m - the other matrix
|
|
173
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
174
|
+
*/
|
|
175
|
+
multiply(m) {
|
|
176
|
+
var b = m.val;
|
|
177
|
+
var a = this.val,
|
|
178
|
+
a0 = a[0],
|
|
179
|
+
a1 = a[1],
|
|
180
|
+
a3 = a[3],
|
|
181
|
+
a4 = a[4],
|
|
182
|
+
b0 = b[0],
|
|
183
|
+
b1 = b[1],
|
|
184
|
+
b3 = b[3],
|
|
185
|
+
b4 = b[4],
|
|
186
|
+
b6 = b[6],
|
|
187
|
+
b7 = b[7];
|
|
188
|
+
|
|
189
|
+
a[0] = a0 * b0 + a3 * b1;
|
|
190
|
+
a[1] = a1 * b0 + a4 * b1;
|
|
191
|
+
a[3] = a0 * b3 + a3 * b4;
|
|
192
|
+
a[4] = a1 * b3 + a4 * b4;
|
|
193
|
+
a[6] += a0 * b6 + a3 * b7;
|
|
194
|
+
a[7] += a1 * b6 + a4 * b7;
|
|
195
|
+
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Transpose the value of this matrix.
|
|
201
|
+
* @name transpose
|
|
202
|
+
* @memberof Matrix2d
|
|
203
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
204
|
+
*/
|
|
205
|
+
transpose() {
|
|
206
|
+
var a = this.val,
|
|
207
|
+
a1 = a[1],
|
|
208
|
+
a2 = a[2],
|
|
209
|
+
a5 = a[5];
|
|
210
|
+
|
|
211
|
+
a[1] = a[3];
|
|
212
|
+
a[2] = a[6];
|
|
213
|
+
a[3] = a1;
|
|
214
|
+
a[5] = a[7];
|
|
215
|
+
a[6] = a2;
|
|
216
|
+
a[7] = a5;
|
|
217
|
+
|
|
218
|
+
return this;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* invert this matrix, causing it to apply the opposite transformation.
|
|
223
|
+
* @name invert
|
|
224
|
+
* @memberof Matrix2d
|
|
225
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
226
|
+
*/
|
|
227
|
+
invert() {
|
|
228
|
+
var val = this.val;
|
|
229
|
+
|
|
230
|
+
var a = val[ 0 ], b = val[ 1 ], c = val[ 2 ],
|
|
231
|
+
d = val[ 3 ], e = val[ 4 ], f = val[ 5 ],
|
|
232
|
+
g = val[ 6 ], h = val[ 7 ], i = val[ 8 ];
|
|
233
|
+
|
|
234
|
+
var ta = i * e - f * h,
|
|
235
|
+
td = f * g - i * d,
|
|
236
|
+
tg = h * d - e * g;
|
|
237
|
+
|
|
238
|
+
var n = a * ta + b * td + c * tg;
|
|
239
|
+
|
|
240
|
+
val[ 0 ] = ta / n;
|
|
241
|
+
val[ 1 ] = ( c * h - i * b ) / n;
|
|
242
|
+
val[ 2 ] = ( f * b - c * e ) / n;
|
|
243
|
+
|
|
244
|
+
val[ 3 ] = td / n;
|
|
245
|
+
val[ 4 ] = ( i * a - c * g ) / n;
|
|
246
|
+
val[ 5 ] = ( c * d - f * a ) / n;
|
|
247
|
+
|
|
248
|
+
val[ 6 ] = tg / n;
|
|
249
|
+
val[ 7 ] = ( b * g - h * a ) / n;
|
|
250
|
+
val[ 8 ] = ( e * a - b * d ) / n;
|
|
251
|
+
|
|
252
|
+
return this;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* apply the current transform to the given 2d or 3d vector
|
|
257
|
+
* @name apply
|
|
258
|
+
* @memberof Matrix2d
|
|
259
|
+
* @param {Vector2d|Vector3d} v - the vector object to be transformed
|
|
260
|
+
* @returns {Vector2d|Vector3d} result vector object.
|
|
261
|
+
*/
|
|
262
|
+
apply(v) {
|
|
263
|
+
var a = this.val,
|
|
264
|
+
x = v.x,
|
|
265
|
+
y = v.y,
|
|
266
|
+
z = (typeof v.z !== "undefined") ? v.z : 1;
|
|
267
|
+
|
|
268
|
+
v.x = x * a[0] + y * a[3] + z * a[6];
|
|
269
|
+
v.y = x * a[1] + y * a[4] + z * a[7];
|
|
270
|
+
|
|
271
|
+
if (typeof v.z !== "undefined") {
|
|
272
|
+
v.z = x * a[2] + y * a[5] + z * a[8];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return v;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* apply the inverted current transform to the given 2d vector
|
|
280
|
+
* @name applyInverse
|
|
281
|
+
* @memberof Matrix2d
|
|
282
|
+
* @param {Vector2d} v - the vector object to be transformed
|
|
283
|
+
* @returns {Vector2d} result vector object.
|
|
284
|
+
*/
|
|
285
|
+
applyInverse(v) {
|
|
286
|
+
var a = this.val,
|
|
287
|
+
x = v.x,
|
|
288
|
+
y = v.y;
|
|
289
|
+
|
|
290
|
+
var invD = 1 / ((a[0] * a[4]) + (a[3] * -a[1]));
|
|
291
|
+
|
|
292
|
+
v.x = (a[4] * invD * x) + (-a[3] * invD * y) + (((a[7] * a[3]) - (a[6] * a[4])) * invD);
|
|
293
|
+
v.y = (a[0] * invD * y) + (-a[1] * invD * x) + (((-a[7] * a[0]) + (a[6] * a[1])) * invD);
|
|
294
|
+
|
|
295
|
+
return v;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* scale the matrix
|
|
300
|
+
* @name scale
|
|
301
|
+
* @memberof Matrix2d
|
|
302
|
+
* @param {number} x - a number representing the abscissa of the scaling vector.
|
|
303
|
+
* @param {number} [y=x] - a number representing the ordinate of the scaling vector.
|
|
304
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
305
|
+
*/
|
|
306
|
+
scale(x, y) {
|
|
307
|
+
var a = this.val,
|
|
308
|
+
_x = x,
|
|
309
|
+
_y = typeof(y) === "undefined" ? _x : y;
|
|
310
|
+
|
|
311
|
+
a[0] *= _x;
|
|
312
|
+
a[1] *= _x;
|
|
313
|
+
a[3] *= _y;
|
|
314
|
+
a[4] *= _y;
|
|
315
|
+
|
|
316
|
+
return this;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* adds a 2D scaling transformation.
|
|
321
|
+
* @name scaleV
|
|
322
|
+
* @memberof Matrix2d
|
|
323
|
+
* @param {Vector2d} v - scaling vector
|
|
324
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
325
|
+
*/
|
|
326
|
+
scaleV(v) {
|
|
327
|
+
return this.scale(v.x, v.y);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* specifies a 2D scale operation using the [sx, 1] scaling vector
|
|
332
|
+
* @name scaleX
|
|
333
|
+
* @memberof Matrix2d
|
|
334
|
+
* @param {number} x - x scaling vector
|
|
335
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
336
|
+
*/
|
|
337
|
+
scaleX(x) {
|
|
338
|
+
return this.scale(x, 1);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* specifies a 2D scale operation using the [1,sy] scaling vector
|
|
343
|
+
* @name scaleY
|
|
344
|
+
* @memberof Matrix2d
|
|
345
|
+
* @param {number} y - y scaling vector
|
|
346
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
347
|
+
*/
|
|
348
|
+
scaleY(y) {
|
|
349
|
+
return this.scale(1, y);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* rotate the matrix (counter-clockwise) by the specified angle (in radians).
|
|
354
|
+
* @name rotate
|
|
355
|
+
* @memberof Matrix2d
|
|
356
|
+
* @param {number} angle - Rotation angle in radians.
|
|
357
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
358
|
+
*/
|
|
359
|
+
rotate(angle) {
|
|
360
|
+
if (angle !== 0) {
|
|
361
|
+
var a = this.val,
|
|
362
|
+
a00 = a[0],
|
|
363
|
+
a01 = a[1],
|
|
364
|
+
a02 = a[2],
|
|
365
|
+
a10 = a[3],
|
|
366
|
+
a11 = a[4],
|
|
367
|
+
a12 = a[5],
|
|
368
|
+
s = Math.sin(angle),
|
|
369
|
+
c = Math.cos(angle);
|
|
370
|
+
|
|
371
|
+
a[0] = c * a00 + s * a10;
|
|
372
|
+
a[1] = c * a01 + s * a11;
|
|
373
|
+
a[2] = c * a02 + s * a12;
|
|
374
|
+
|
|
375
|
+
a[3] = c * a10 - s * a00;
|
|
376
|
+
a[4] = c * a11 - s * a01;
|
|
377
|
+
a[5] = c * a12 - s * a02;
|
|
378
|
+
}
|
|
379
|
+
return this;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* translate the matrix position on the horizontal and vertical axis
|
|
384
|
+
* @name translate
|
|
385
|
+
* @memberof Matrix2d
|
|
386
|
+
* @method
|
|
387
|
+
* @param {number} x - the x coordindates to translate the matrix by
|
|
388
|
+
* @param {number} y - the y coordindates to translate the matrix by
|
|
389
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
390
|
+
*/
|
|
391
|
+
/**
|
|
392
|
+
* translate the matrix by a vector on the horizontal and vertical axis
|
|
393
|
+
* @name translateV
|
|
394
|
+
* @memberof Matrix2d
|
|
395
|
+
* @param {Vector2d} v - the vector to translate the matrix by
|
|
396
|
+
* @returns {Matrix2d} Reference to this object for method chaining
|
|
397
|
+
*/
|
|
398
|
+
translate() {
|
|
399
|
+
var a = this.val;
|
|
400
|
+
var _x, _y;
|
|
401
|
+
|
|
402
|
+
if (arguments.length === 2) {
|
|
403
|
+
// x, y
|
|
404
|
+
_x = arguments[0];
|
|
405
|
+
_y = arguments[1];
|
|
406
|
+
} else {
|
|
407
|
+
// vector
|
|
408
|
+
_x = arguments[0].x;
|
|
409
|
+
_y = arguments[0].y;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
a[6] += a[0] * _x + a[3] * _y;
|
|
413
|
+
a[7] += a[1] * _x + a[4] * _y;
|
|
414
|
+
|
|
415
|
+
return this;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* returns true if the matrix is an identity matrix.
|
|
420
|
+
* @name isIdentity
|
|
421
|
+
* @memberof Matrix2d
|
|
422
|
+
* @returns {boolean}
|
|
423
|
+
*/
|
|
424
|
+
isIdentity() {
|
|
425
|
+
var a = this.val;
|
|
426
|
+
|
|
427
|
+
return (
|
|
428
|
+
a[0] === 1 &&
|
|
429
|
+
a[1] === 0 &&
|
|
430
|
+
a[2] === 0 &&
|
|
431
|
+
a[3] === 0 &&
|
|
432
|
+
a[4] === 1 &&
|
|
433
|
+
a[5] === 0 &&
|
|
434
|
+
a[6] === 0 &&
|
|
435
|
+
a[7] === 0 &&
|
|
436
|
+
a[8] === 1
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* return true if the two matrices are identical
|
|
442
|
+
* @name equals
|
|
443
|
+
* @memberof Matrix2d
|
|
444
|
+
* @param {Matrix2d} m - the other matrix
|
|
445
|
+
* @returns {boolean} true if both are equals
|
|
446
|
+
*/
|
|
447
|
+
equals(m) {
|
|
448
|
+
var b = m.val;
|
|
449
|
+
var a = this.val;
|
|
450
|
+
|
|
451
|
+
return (
|
|
452
|
+
(a[0] === b[0]) &&
|
|
453
|
+
(a[1] === b[1]) &&
|
|
454
|
+
(a[2] === b[2]) &&
|
|
455
|
+
(a[3] === b[3]) &&
|
|
456
|
+
(a[4] === b[4]) &&
|
|
457
|
+
(a[5] === b[5]) &&
|
|
458
|
+
(a[6] === b[6]) &&
|
|
459
|
+
(a[7] === b[7]) &&
|
|
460
|
+
(a[8] === b[8])
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Clone the Matrix
|
|
466
|
+
* @name clone
|
|
467
|
+
* @memberof Matrix2d
|
|
468
|
+
* @returns {Matrix2d}
|
|
469
|
+
*/
|
|
470
|
+
clone() {
|
|
471
|
+
return pool.pull("Matrix2d", this);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* return an array representation of this Matrix
|
|
476
|
+
* @name toArray
|
|
477
|
+
* @memberof Matrix2d
|
|
478
|
+
* @returns {Float32Array}
|
|
479
|
+
*/
|
|
480
|
+
toArray() {
|
|
481
|
+
return this.val;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* convert the object to a string representation
|
|
486
|
+
* @name toString
|
|
487
|
+
* @memberof Matrix2d
|
|
488
|
+
* @returns {string}
|
|
489
|
+
*/
|
|
490
|
+
toString() {
|
|
491
|
+
var a = this.val;
|
|
492
|
+
|
|
493
|
+
return "me.Matrix2d(" +
|
|
494
|
+
a[0] + ", " + a[1] + ", " + a[2] + ", " +
|
|
495
|
+
a[3] + ", " + a[4] + ", " + a[5] + ", " +
|
|
496
|
+
a[6] + ", " + a[7] + ", " + a[8] +
|
|
497
|
+
")";
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
var Matrix2d$1 = Matrix2d;
|
|
502
|
+
|
|
503
|
+
export { Matrix2d$1 as default };
|