From 6c947c96774fd41769bd7d95004d7687129cb1e4 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 10 Jan 2020 08:55:31 +0000 Subject: [PATCH] Updated PhysicsEditor Parser Now allows you to specify options object to override the loaded config. Also removed un-used parameters and fixed JSDocs. Finally, using Common.clone to avoid mutating the loaded JSON. --- src/physics/matter-js/PhysicsEditorParser.js | 38 ++++++++++---------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/physics/matter-js/PhysicsEditorParser.js b/src/physics/matter-js/PhysicsEditorParser.js index c5be0ad45..60154d2ff 100644 --- a/src/physics/matter-js/PhysicsEditorParser.js +++ b/src/physics/matter-js/PhysicsEditorParser.js @@ -1,6 +1,8 @@ /** * @author Joachim Grill + * @author Richard Davey * @copyright 2018 CodeAndWeb GmbH + * @copyright 2019 Photon Storm Ltd. * @license {@link https://opensource.org/licenses/MIT|MIT License} */ @@ -25,16 +27,17 @@ var PhysicsEditorParser = { * @function Phaser.Physics.Matter.PhysicsEditorParser.parseBody * @since 3.10.0 * - * @param {number} x - x position. - * @param {number} y - y position. - * @param {number} w - width. - * @param {number} h - height. - * @param {object} config - body configuration and fixture (child body) definitions. + * @param {number} x - The horizontal world location of the body. + * @param {number} y - The vertical world location of the body. + * @param {object} config - The body configuration and fixture (child body) definitions, as exported by PhysicsEditor. + * @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation. * - * @return {object} A matter body, consisting of several parts (child bodies) + * @return {MatterJS.Body} A compound Matter JS Body. */ - parseBody: function (x, y, w, h, config) + parseBody: function (x, y, config, options) { + if (options === undefined) { options = {}; } + var fixtureConfigs = GetFastValue(config, 'fixtures', []); var fixtures = []; @@ -48,7 +51,9 @@ var PhysicsEditorParser = { } } - var matterConfig = Common.extend({}, false, config); + var matterConfig = Common.clone(config, true); + + Common.extend(matterConfig, options, true); delete matterConfig.fixtures; delete matterConfig.type; @@ -57,9 +62,6 @@ var PhysicsEditorParser = { Body.setParts(body, fixtures); - // body.render.sprite.xOffset = body.position.x / w; - // body.render.sprite.yOffset = body.position.y / h; - Body.setPosition(body, { x: x, y: y }); return body; @@ -71,9 +73,9 @@ var PhysicsEditorParser = { * @function Phaser.Physics.Matter.PhysicsEditorParser.parseFixture * @since 3.10.0 * - * @param {object} fixtureConfig - the fixture object to parse + * @param {object} fixtureConfig - The fixture object to parse. * - * @return {object[]} - A list of matter bodies + * @return {MatterJS.Body[]} - An array of Matter JS Bodies. */ parseFixture: function (fixtureConfig) { @@ -105,16 +107,16 @@ var PhysicsEditorParser = { * @function Phaser.Physics.Matter.PhysicsEditorParser.parseVertices * @since 3.10.0 * - * @param {object} vertexSets - The vertex lists to parse. - * @param {object} options - Matter body options. + * @param {array} vertexSets - The vertex lists to parse. + * @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation. * - * @return {object[]} - A list of matter bodies. + * @return {MatterJS.Body[]} - An array of Matter JS Bodies. */ parseVertices: function (vertexSets, options) { - var parts = []; + if (options === undefined) { options = {}; } - options = options || {}; + var parts = []; for (var v = 0; v < vertexSets.length; v++) {