melonjs 15.1.5 → 15.2.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 -2
- package/dist/melonjs.mjs/_virtual/_commonjsHelpers.js +1 -1
- package/dist/melonjs.mjs/_virtual/earcut.js +1 -1
- package/dist/melonjs.mjs/_virtual/howler.js +1 -1
- package/dist/melonjs.mjs/_virtual/index.js +1 -1
- package/dist/melonjs.mjs/application/application.js +3 -3
- package/dist/melonjs.mjs/application/header.js +1 -1
- package/dist/melonjs.mjs/application/resize.js +1 -1
- package/dist/melonjs.mjs/application/settings.js +1 -1
- package/dist/melonjs.mjs/audio/audio.js +1 -1
- package/dist/melonjs.mjs/camera/camera2d.js +3 -3
- package/dist/melonjs.mjs/const.js +1 -1
- package/dist/melonjs.mjs/entity/entity.js +1 -1
- package/dist/melonjs.mjs/geometries/ellipse.js +1 -1
- package/dist/melonjs.mjs/geometries/line.js +1 -1
- package/dist/melonjs.mjs/geometries/path2d.js +1 -1
- package/dist/melonjs.mjs/geometries/point.js +1 -1
- package/dist/melonjs.mjs/geometries/poly.js +1 -1
- package/dist/melonjs.mjs/geometries/rectangle.js +1 -1
- package/dist/melonjs.mjs/geometries/roundrect.js +1 -1
- package/dist/melonjs.mjs/index.js +12 -7
- package/dist/melonjs.mjs/input/gamepad.js +1 -1
- package/dist/melonjs.mjs/input/input.js +1 -1
- package/dist/melonjs.mjs/input/keyboard.js +1 -1
- package/dist/melonjs.mjs/input/pointer.js +1 -1
- package/dist/melonjs.mjs/input/pointerevent.js +1 -1
- package/dist/melonjs.mjs/lang/console.js +1 -1
- package/dist/melonjs.mjs/lang/deprecated.js +3 -2
- package/dist/melonjs.mjs/level/level.js +5 -4
- package/dist/melonjs.mjs/level/tiled/TMXGroup.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXLayer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXObject.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXTile.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXTileMap.js +3 -3
- package/dist/melonjs.mjs/level/tiled/TMXTileset.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXTilesetGroup.js +1 -1
- package/dist/melonjs.mjs/level/tiled/TMXUtils.js +6 -8
- package/dist/melonjs.mjs/level/tiled/constants.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXRenderer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
- package/dist/melonjs.mjs/level/tiled/renderer/autodetect.js +1 -1
- package/dist/melonjs.mjs/loader/cache.js +1 -1
- package/dist/melonjs.mjs/loader/loader.js +1 -1
- package/dist/melonjs.mjs/loader/loadingscreen.js +1 -1
- package/dist/melonjs.mjs/loader/melonjs_logo.png.js +1 -1
- package/dist/melonjs.mjs/loader/parser.js +1 -1
- package/dist/melonjs.mjs/loader/settings.js +1 -1
- package/dist/melonjs.mjs/math/color.js +1 -1
- package/dist/melonjs.mjs/math/math.js +1 -1
- package/dist/melonjs.mjs/math/matrix2.js +1 -1
- package/dist/melonjs.mjs/math/matrix3.js +1 -1
- package/dist/melonjs.mjs/math/observable_vector2.js +1 -1
- package/dist/melonjs.mjs/math/observable_vector3.js +1 -1
- package/dist/melonjs.mjs/math/vector2.js +1 -1
- package/dist/melonjs.mjs/math/vector3.js +1 -1
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/esm/arraymultimap.js +1 -1
- package/dist/melonjs.mjs/node_modules/@teppeis/multimaps/dist/esm/multimap.js +1 -1
- package/dist/melonjs.mjs/node_modules/earcut/src/earcut.js +1 -1
- package/dist/melonjs.mjs/node_modules/eventemitter3/index.js +1 -1
- package/dist/melonjs.mjs/node_modules/eventemitter3/index2.js +1 -1
- package/dist/melonjs.mjs/node_modules/howler/dist/howler.js +1 -1
- package/dist/melonjs.mjs/particles/emitter.js +1 -1
- package/dist/melonjs.mjs/particles/particle.js +1 -1
- package/dist/melonjs.mjs/particles/settings.js +1 -1
- package/dist/melonjs.mjs/physics/body.js +3 -3
- package/dist/melonjs.mjs/physics/bounds.js +1 -1
- package/dist/melonjs.mjs/physics/collision.js +1 -1
- package/dist/melonjs.mjs/physics/detector.js +4 -4
- package/dist/melonjs.mjs/physics/quadtree.js +1 -1
- package/dist/melonjs.mjs/physics/response.js +1 -1
- package/dist/melonjs.mjs/physics/sat.js +9 -9
- package/dist/melonjs.mjs/physics/world.js +1 -1
- package/dist/melonjs.mjs/plugin/plugin.js +106 -108
- package/dist/melonjs.mjs/renderable/collectable.js +1 -1
- package/dist/melonjs.mjs/renderable/colorlayer.js +1 -1
- package/dist/melonjs.mjs/renderable/container.js +20 -19
- package/dist/melonjs.mjs/renderable/draggable.js +121 -0
- package/dist/melonjs.mjs/renderable/dragndrop.js +3 -113
- package/dist/melonjs.mjs/renderable/imagelayer.js +1 -1
- package/dist/melonjs.mjs/renderable/light2d.js +1 -1
- package/dist/melonjs.mjs/renderable/nineslicesprite.js +1 -1
- package/dist/melonjs.mjs/renderable/renderable.js +2 -2
- package/dist/melonjs.mjs/renderable/sprite.js +1 -1
- package/dist/melonjs.mjs/renderable/trigger.js +2 -2
- package/dist/melonjs.mjs/renderable/ui/uibaseelement.js +1 -1
- package/dist/melonjs.mjs/renderable/ui/uispriteelement.js +1 -1
- package/dist/melonjs.mjs/renderable/ui/uitextbutton.js +1 -1
- package/dist/melonjs.mjs/state/stage.js +1 -1
- package/dist/melonjs.mjs/state/state.js +1 -1
- package/dist/melonjs.mjs/system/device.js +1 -1
- package/dist/melonjs.mjs/system/dom.js +1 -1
- package/dist/melonjs.mjs/system/event.js +1 -1
- package/dist/melonjs.mjs/system/platform.js +1 -1
- package/dist/melonjs.mjs/system/pooling.js +1 -1
- package/dist/melonjs.mjs/system/save.js +1 -1
- package/dist/melonjs.mjs/system/timer.js +4 -4
- package/dist/melonjs.mjs/text/bitmaptext.js +1 -1
- package/dist/melonjs.mjs/text/bitmaptextdata.js +1 -1
- package/dist/melonjs.mjs/text/glyph.js +1 -1
- package/dist/melonjs.mjs/text/text.js +1 -1
- package/dist/melonjs.mjs/text/textmetrics.js +1 -1
- package/dist/melonjs.mjs/text/textstyle.js +1 -1
- package/dist/melonjs.mjs/tweens/easing.js +1 -1
- package/dist/melonjs.mjs/tweens/interpolation.js +1 -1
- package/dist/melonjs.mjs/tweens/tween.js +1 -1
- package/dist/melonjs.mjs/utils/agent.js +1 -1
- package/dist/melonjs.mjs/utils/array.js +1 -1
- package/dist/melonjs.mjs/utils/file.js +1 -1
- package/dist/melonjs.mjs/utils/function.js +1 -1
- package/dist/melonjs.mjs/utils/string.js +1 -1
- package/dist/melonjs.mjs/utils/utils.js +105 -111
- package/dist/melonjs.mjs/video/canvas/canvas_renderer.js +1 -1
- package/dist/melonjs.mjs/video/renderer.js +1 -1
- package/dist/melonjs.mjs/video/texture/atlas.js +1 -1
- package/dist/melonjs.mjs/video/texture/cache.js +1 -1
- package/dist/melonjs.mjs/video/texture/canvas_texture.js +1 -1
- package/dist/melonjs.mjs/video/utils/autodetect.js +1 -1
- package/dist/melonjs.mjs/video/video.js +4 -4
- package/dist/melonjs.mjs/video/webgl/buffer/vertex.js +1 -1
- package/dist/melonjs.mjs/video/webgl/compositors/compositor.js +1 -1
- package/dist/melonjs.mjs/video/webgl/compositors/primitive_compositor.js +1 -1
- package/dist/melonjs.mjs/video/webgl/compositors/quad_compositor.js +1 -1
- package/dist/melonjs.mjs/video/webgl/glshader.js +1 -1
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.frag.js +1 -1
- package/dist/melonjs.mjs/video/webgl/shaders/primitive.vert.js +1 -1
- package/dist/melonjs.mjs/video/webgl/shaders/quad.frag.js +1 -1
- package/dist/melonjs.mjs/video/webgl/shaders/quad.vert.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/attributes.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/precision.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/program.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/string.js +1 -1
- package/dist/melonjs.mjs/video/webgl/utils/uniforms.js +1 -1
- package/dist/melonjs.mjs/video/webgl/webgl_renderer.js +1 -1
- package/dist/melonjs.module.js +516 -500
- package/dist/types/camera/camera2d.d.ts +4 -4
- package/dist/types/index.d.ts +4 -4
- package/dist/types/physics/body.d.ts +4 -4
- package/dist/types/physics/detector.d.ts +5 -5
- package/dist/types/physics/sat.d.ts +12 -12
- package/dist/types/plugin/plugin.d.ts +60 -5
- package/dist/types/renderable/container.d.ts +25 -25
- package/dist/types/renderable/draggable.d.ts +53 -0
- package/dist/types/renderable/dragndrop.d.ts +0 -52
- package/dist/types/renderable/renderable.d.ts +2 -2
- package/dist/types/utils/utils.d.ts +56 -17
- package/package.json +8 -8
- package/src/application/application.js +2 -2
- package/src/camera/camera2d.js +2 -2
- package/src/index.js +5 -3
- package/src/lang/deprecated.js +2 -1
- package/src/level/level.js +4 -3
- package/src/level/tiled/TMXTileMap.js +2 -2
- package/src/level/tiled/TMXUtils.js +4 -3
- package/src/physics/body.js +2 -2
- package/src/physics/detector.js +3 -3
- package/src/physics/sat.js +8 -8
- package/src/plugin/plugin.js +103 -105
- package/src/renderable/container.js +19 -18
- package/src/renderable/draggable.js +114 -0
- package/src/renderable/dragndrop.js +0 -109
- package/src/renderable/renderable.js +1 -1
- package/src/renderable/trigger.js +1 -1
- package/src/system/timer.js +3 -3
- package/src/utils/utils.js +102 -111
- package/src/video/video.js +3 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* melonJS Game Engine - v15.
|
|
2
|
+
* melonJS Game Engine - v15.2.0
|
|
3
3
|
* http://www.melonjs.org
|
|
4
4
|
* melonjs is licensed under the MIT License.
|
|
5
5
|
* http://www.opensource.org/licenses/mit-license
|
|
@@ -23,7 +23,7 @@ import Point from '../geometries/point.js';
|
|
|
23
23
|
*/
|
|
24
24
|
class Body {
|
|
25
25
|
/**
|
|
26
|
-
* @param {Renderable} ancestor - the parent object this body is attached to
|
|
26
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} ancestor - the parent object this body is attached to
|
|
27
27
|
* @param {Rect|Rect[]|Polygon|Polygon[]|Line|Line[]|Ellipse|Ellipse[]|Point|Point[]|Bounds|Bounds[]|object} [shapes] - a initial shape, list of shapes, or JSON object defining the body
|
|
28
28
|
* @param {Function} [onBodyUpdate] - callback for when the body is updated (e.g. add/remove shapes)
|
|
29
29
|
*/
|
|
@@ -33,7 +33,7 @@ import Point from '../geometries/point.js';
|
|
|
33
33
|
* a reference to the parent object that contains this body,
|
|
34
34
|
* or undefined if it has not been added to one.
|
|
35
35
|
* @public
|
|
36
|
-
* @type {Renderable}
|
|
36
|
+
* @type {Renderable|Container|Entity|Sprite|NineSliceSprite}
|
|
37
37
|
* @default undefined
|
|
38
38
|
*/
|
|
39
39
|
this.ancestor = ancestor;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* melonJS Game Engine - v15.
|
|
2
|
+
* melonJS Game Engine - v15.2.0
|
|
3
3
|
* http://www.melonjs.org
|
|
4
4
|
* melonjs is licensed under the MIT License.
|
|
5
5
|
* http://www.opensource.org/licenses/mit-license
|
|
@@ -47,8 +47,8 @@ class Detector {
|
|
|
47
47
|
/**
|
|
48
48
|
* determine if two objects should collide (based on both respective objects body collision mask and type).<br>
|
|
49
49
|
* you can redefine this function if you need any specific rules over what should collide with what.
|
|
50
|
-
* @param {Renderable} a - a reference to the object A.
|
|
51
|
-
* @param {Renderable} b - a reference to the object B.
|
|
50
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} a - a reference to the object A.
|
|
51
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} b - a reference to the object B.
|
|
52
52
|
* @returns {boolean} true if they should collide, false otherwise
|
|
53
53
|
*/
|
|
54
54
|
shouldCollide(a, b) {
|
|
@@ -101,7 +101,7 @@ class Detector {
|
|
|
101
101
|
/**
|
|
102
102
|
* find all the collisions for the specified object using a broadphase algorithm
|
|
103
103
|
* @ignore
|
|
104
|
-
* @param {Renderable} objA - object to be tested for collision
|
|
104
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} objA - object to be tested for collision
|
|
105
105
|
* @returns {boolean} in case of collision, false otherwise
|
|
106
106
|
*/
|
|
107
107
|
collisions(objA) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* melonJS Game Engine - v15.
|
|
2
|
+
* melonJS Game Engine - v15.2.0
|
|
3
3
|
* http://www.melonjs.org
|
|
4
4
|
* melonjs is licensed under the MIT License.
|
|
5
5
|
* http://www.opensource.org/licenses/mit-license
|
|
@@ -195,9 +195,9 @@ function vornoiRegion(line, point) {
|
|
|
195
195
|
/**
|
|
196
196
|
* Checks whether polygons collide.
|
|
197
197
|
* @ignore
|
|
198
|
-
* @param {Renderable} a - a reference to the object A.
|
|
198
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} a - a reference to the object A.
|
|
199
199
|
* @param {Polygon} polyA - a reference to the object A Polygon to be tested
|
|
200
|
-
* @param {Renderable} b - a reference to the object B.
|
|
200
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} b - a reference to the object B.
|
|
201
201
|
* @param {Polygon} polyB - a reference to the object B Polygon to be tested
|
|
202
202
|
* @param {Response=} response - Response object (optional) that will be populated if they intersect.
|
|
203
203
|
* @returns {boolean} true if they intersect, false if they don't.
|
|
@@ -249,9 +249,9 @@ function testPolygonPolygon(a, polyA, b, polyB, response) {
|
|
|
249
249
|
/**
|
|
250
250
|
* Check if two Ellipse collide.
|
|
251
251
|
* @ignore
|
|
252
|
-
* @param {Renderable} a - a reference to the object A.
|
|
252
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} a - a reference to the object A.
|
|
253
253
|
* @param {Ellipse} ellipseA - a reference to the object A Ellipse to be tested
|
|
254
|
-
* @param {Renderable} b - a reference to the object B.
|
|
254
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} b - a reference to the object B.
|
|
255
255
|
* @param {Ellipse} ellipseB - a reference to the object B Ellipse to be tested
|
|
256
256
|
* @param {Response=} response - Response object (optional) that will be populated if
|
|
257
257
|
* the circles intersect.
|
|
@@ -290,9 +290,9 @@ function testEllipseEllipse(a, ellipseA, b, ellipseB, response) {
|
|
|
290
290
|
/**
|
|
291
291
|
* Check if a polygon and an ellipse collide.
|
|
292
292
|
* @ignore
|
|
293
|
-
* @param {Renderable} a - a reference to the object A.
|
|
293
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} a - a reference to the object A.
|
|
294
294
|
* @param {Polygon} polyA - a reference to the object A Polygon to be tested
|
|
295
|
-
* @param {Renderable} b - a reference to the object B.
|
|
295
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} b - a reference to the object B.
|
|
296
296
|
* @param {Ellipse} ellipseB - a reference to the object B Ellipse to be tested
|
|
297
297
|
* @param {Response=} response - Response object (optional) that will be populated if they intersect.
|
|
298
298
|
* @returns {boolean} true if they intersect, false if they don't.
|
|
@@ -456,9 +456,9 @@ function testPolygonEllipse(a, polyA, b, ellipseB, response) {
|
|
|
456
456
|
* **NOTE:** This is slightly less efficient than testPolygonEllipse as it just
|
|
457
457
|
* runs testPolygonEllipse and reverses the response at the end.
|
|
458
458
|
* @ignore
|
|
459
|
-
* @param {Renderable} a - a reference to the object A.
|
|
459
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} a - a reference to the object A.
|
|
460
460
|
* @param {Ellipse} ellipseA - a reference to the object A Ellipse to be tested
|
|
461
|
-
* @param {Renderable} b - a reference to the object B.
|
|
461
|
+
* @param {Renderable|Container|Entity|Sprite|NineSliceSprite} b - a reference to the object B.
|
|
462
462
|
* @param {Polygon} polyB - a reference to the object B Polygon to be tested
|
|
463
463
|
* @param {Response=} response - Response object (optional) that will be populated if
|
|
464
464
|
* they intersect.
|
|
@@ -1,141 +1,139 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* melonJS Game Engine - v15.
|
|
2
|
+
* melonJS Game Engine - v15.2.0
|
|
3
3
|
* http://www.melonjs.org
|
|
4
4
|
* melonjs is licensed under the MIT License.
|
|
5
5
|
* http://www.opensource.org/licenses/mit-license
|
|
6
6
|
* @copyright (C) 2011 - 2023 Olivier Biot (AltByte Pte Ltd)
|
|
7
7
|
*/
|
|
8
|
-
import
|
|
8
|
+
import { checkVersion } from '../utils/utils.js';
|
|
9
9
|
import { version } from '../index.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @
|
|
14
|
-
* @
|
|
12
|
+
* Contains all registered plugins.
|
|
13
|
+
* @name cache
|
|
14
|
+
* @memberof plugin
|
|
15
15
|
*/
|
|
16
|
-
let
|
|
16
|
+
let cache = {};
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* @namespace plugin
|
|
20
|
+
*/
|
|
18
21
|
|
|
22
|
+
/**
|
|
23
|
+
* a base Object class for plugin
|
|
24
|
+
* (plugin must be installed using the register function)
|
|
25
|
+
* @class
|
|
26
|
+
* @name BasePlugin
|
|
27
|
+
* @memberof plugin
|
|
28
|
+
*/
|
|
19
29
|
class BasePlugin {
|
|
20
30
|
|
|
21
31
|
constructor() {
|
|
22
32
|
/**
|
|
23
33
|
* define the minimum required version of melonJS<br>
|
|
24
34
|
* this can be overridden by the plugin
|
|
25
|
-
* @public
|
|
26
35
|
* @type {string}
|
|
27
|
-
* @default "15.
|
|
28
|
-
* @name plugin.Base#version
|
|
36
|
+
* @default "15.2.0"
|
|
29
37
|
*/
|
|
30
|
-
this.version = "15.
|
|
38
|
+
this.version = "15.2.0";
|
|
31
39
|
}
|
|
32
40
|
}
|
|
33
41
|
|
|
42
|
+
|
|
34
43
|
/**
|
|
35
|
-
* @
|
|
44
|
+
* @class
|
|
45
|
+
* @name Base
|
|
46
|
+
* @memberof plugin
|
|
47
|
+
* @deprecated since 15.1.6, see {@link plugin.BasePlugin}
|
|
36
48
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* a base Object for plugin <br>
|
|
41
|
-
* plugin must be installed using the register function
|
|
42
|
-
* @see plugin
|
|
43
|
-
* @class
|
|
44
|
-
* @name Base
|
|
45
|
-
* @memberof plugin
|
|
46
|
-
*/
|
|
47
|
-
Base : BasePlugin,
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* patch a melonJS function
|
|
51
|
-
* @name patch
|
|
52
|
-
* @memberof plugin
|
|
53
|
-
* @public
|
|
54
|
-
* @param {object} proto - target object
|
|
55
|
-
* @param {string} name - target function
|
|
56
|
-
* @param {Function} fn - replacement function
|
|
57
|
-
* @example
|
|
58
|
-
* // redefine the me.game.update function with a new one
|
|
59
|
-
* me.plugin.patch(me.game, "update", function () {
|
|
60
|
-
* // display something in the console
|
|
61
|
-
* console.log("duh");
|
|
62
|
-
* // call the original me.game.update function
|
|
63
|
-
* this._patched();
|
|
64
|
-
* });
|
|
65
|
-
*/
|
|
66
|
-
patch : function (proto, name, fn) {
|
|
67
|
-
// use the object prototype if possible
|
|
68
|
-
if (typeof proto.prototype !== "undefined") {
|
|
69
|
-
proto = proto.prototype;
|
|
70
|
-
}
|
|
71
|
-
// reuse the logic behind object extends
|
|
72
|
-
if (typeof(proto[name]) === "function") {
|
|
73
|
-
// save the original function
|
|
74
|
-
let _parent = proto[name];
|
|
75
|
-
// override the function with the new one
|
|
76
|
-
Object.defineProperty(proto, name, {
|
|
77
|
-
"configurable" : true,
|
|
78
|
-
"value" : (function (name, fn) {
|
|
79
|
-
return function () {
|
|
80
|
-
this._patched = _parent;
|
|
81
|
-
let ret = fn.apply(this, arguments);
|
|
82
|
-
this._patched = null;
|
|
83
|
-
return ret;
|
|
84
|
-
};
|
|
85
|
-
})(name, fn)
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
throw new Error(name + " is not an existing function");
|
|
90
|
-
}
|
|
91
|
-
},
|
|
49
|
+
class Base extends BasePlugin {}
|
|
92
50
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
51
|
+
/**
|
|
52
|
+
* patch a melonJS function
|
|
53
|
+
* @name patch
|
|
54
|
+
* @memberof plugin
|
|
55
|
+
* @param {object} proto - target object
|
|
56
|
+
* @param {string} name - target function
|
|
57
|
+
* @param {Function} fn - replacement function
|
|
58
|
+
* @example
|
|
59
|
+
* // redefine the me.game.update function with a new one
|
|
60
|
+
* me.plugin.patch(me.game, "update", function () {
|
|
61
|
+
* // display something in the console
|
|
62
|
+
* console.log("duh");
|
|
63
|
+
* // call the original me.game.update function
|
|
64
|
+
* this._patched();
|
|
65
|
+
* });
|
|
66
|
+
*/
|
|
67
|
+
function patch(proto, name, fn) {
|
|
68
|
+
// use the object prototype if possible
|
|
69
|
+
if (typeof proto.prototype !== "undefined") {
|
|
70
|
+
proto = proto.prototype;
|
|
71
|
+
}
|
|
72
|
+
// reuse the logic behind object extends
|
|
73
|
+
if (typeof(proto[name]) === "function") {
|
|
74
|
+
// save the original function
|
|
75
|
+
let _parent = proto[name];
|
|
76
|
+
// override the function with the new one
|
|
77
|
+
Object.defineProperty(proto, name, {
|
|
78
|
+
"configurable" : true,
|
|
79
|
+
"value" : (function (name, fn) {
|
|
80
|
+
return function () {
|
|
81
|
+
this._patched = _parent;
|
|
82
|
+
let ret = fn.apply(this, arguments);
|
|
83
|
+
this._patched = null;
|
|
84
|
+
return ret;
|
|
85
|
+
};
|
|
86
|
+
})(name, fn)
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
throw new Error(name + " is not an existing function");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
114
93
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Register a plugin.
|
|
96
|
+
* @name register
|
|
97
|
+
* @memberof plugin
|
|
98
|
+
* @param {plugin.BasePlugin} plugin - Plugin object to instantiate and register
|
|
99
|
+
* @param {string} [name=plugin.constructor.name] - a unique name for this plugin
|
|
100
|
+
* @param {object} [...arguments] - all extra parameters will be passed to the plugin constructor
|
|
101
|
+
* @example
|
|
102
|
+
* // register a new plugin
|
|
103
|
+
* me.plugin.register(TestPlugin, "testPlugin");
|
|
104
|
+
* // the plugin then also become available
|
|
105
|
+
* // under then me.plugins namespace
|
|
106
|
+
* me.plugins.testPlugin.myfunction ();
|
|
107
|
+
*/
|
|
108
|
+
function register(plugin, name = plugin.toString().match(/ (\w+)/)[1]) {
|
|
109
|
+
// ensure me.plugins[name] is not already "used"
|
|
110
|
+
if (cache[name]) {
|
|
111
|
+
throw new Error("plugin " + name + " already registered");
|
|
112
|
+
}
|
|
121
113
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
114
|
+
// get extra arguments
|
|
115
|
+
let _args = [];
|
|
116
|
+
if (arguments.length > 2) {
|
|
117
|
+
// store extra arguments if any
|
|
118
|
+
_args = Array.prototype.slice.call(arguments, 1);
|
|
119
|
+
}
|
|
125
120
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
121
|
+
// try to instantiate the plugin
|
|
122
|
+
_args[0] = plugin;
|
|
123
|
+
let instance = new (plugin.bind.apply(plugin, _args))();
|
|
130
124
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
125
|
+
// inheritance check
|
|
126
|
+
if (typeof instance === "undefined" || !(instance instanceof BasePlugin)) {
|
|
127
|
+
throw new Error("Plugin should extend the BasePlugin Class !");
|
|
128
|
+
}
|
|
135
129
|
|
|
136
|
-
|
|
137
|
-
|
|
130
|
+
// compatibility testing
|
|
131
|
+
if (checkVersion(instance.version, version) > 0) {
|
|
132
|
+
throw new Error("Plugin version mismatch, expected: " + instance.version + ", got: " + version);
|
|
138
133
|
}
|
|
139
|
-
};
|
|
140
134
|
|
|
141
|
-
|
|
135
|
+
// create a reference to the new plugin
|
|
136
|
+
cache[name] = instance;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { Base, BasePlugin, cache, patch, register };
|