Skip to content

Commit 6c947c9

Browse files
committed
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.
1 parent d63001d commit 6c947c9

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

src/physics/matter-js/PhysicsEditorParser.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* @author Joachim Grill <joachim@codeandweb.com>
3+
* @author Richard Davey <rich@photonstorm.com>
34
* @copyright 2018 CodeAndWeb GmbH
5+
* @copyright 2019 Photon Storm Ltd.
46
* @license {@link https://opensource.org/licenses/MIT|MIT License}
57
*/
68

@@ -25,16 +27,17 @@ var PhysicsEditorParser = {
2527
* @function Phaser.Physics.Matter.PhysicsEditorParser.parseBody
2628
* @since 3.10.0
2729
*
28-
* @param {number} x - x position.
29-
* @param {number} y - y position.
30-
* @param {number} w - width.
31-
* @param {number} h - height.
32-
* @param {object} config - body configuration and fixture (child body) definitions.
30+
* @param {number} x - The horizontal world location of the body.
31+
* @param {number} y - The vertical world location of the body.
32+
* @param {object} config - The body configuration and fixture (child body) definitions, as exported by PhysicsEditor.
33+
* @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
3334
*
34-
* @return {object} A matter body, consisting of several parts (child bodies)
35+
* @return {MatterJS.Body} A compound Matter JS Body.
3536
*/
36-
parseBody: function (x, y, w, h, config)
37+
parseBody: function (x, y, config, options)
3738
{
39+
if (options === undefined) { options = {}; }
40+
3841
var fixtureConfigs = GetFastValue(config, 'fixtures', []);
3942
var fixtures = [];
4043

@@ -48,7 +51,9 @@ var PhysicsEditorParser = {
4851
}
4952
}
5053

51-
var matterConfig = Common.extend({}, false, config);
54+
var matterConfig = Common.clone(config, true);
55+
56+
Common.extend(matterConfig, options, true);
5257

5358
delete matterConfig.fixtures;
5459
delete matterConfig.type;
@@ -57,9 +62,6 @@ var PhysicsEditorParser = {
5762

5863
Body.setParts(body, fixtures);
5964

60-
// body.render.sprite.xOffset = body.position.x / w;
61-
// body.render.sprite.yOffset = body.position.y / h;
62-
6365
Body.setPosition(body, { x: x, y: y });
6466

6567
return body;
@@ -71,9 +73,9 @@ var PhysicsEditorParser = {
7173
* @function Phaser.Physics.Matter.PhysicsEditorParser.parseFixture
7274
* @since 3.10.0
7375
*
74-
* @param {object} fixtureConfig - the fixture object to parse
76+
* @param {object} fixtureConfig - The fixture object to parse.
7577
*
76-
* @return {object[]} - A list of matter bodies
78+
* @return {MatterJS.Body[]} - An array of Matter JS Bodies.
7779
*/
7880
parseFixture: function (fixtureConfig)
7981
{
@@ -105,16 +107,16 @@ var PhysicsEditorParser = {
105107
* @function Phaser.Physics.Matter.PhysicsEditorParser.parseVertices
106108
* @since 3.10.0
107109
*
108-
* @param {object} vertexSets - The vertex lists to parse.
109-
* @param {object} options - Matter body options.
110+
* @param {array} vertexSets - The vertex lists to parse.
111+
* @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
110112
*
111-
* @return {object[]} - A list of matter bodies.
113+
* @return {MatterJS.Body[]} - An array of Matter JS Bodies.
112114
*/
113115
parseVertices: function (vertexSets, options)
114116
{
115-
var parts = [];
117+
if (options === undefined) { options = {}; }
116118

117-
options = options || {};
119+
var parts = [];
118120

119121
for (var v = 0; v < vertexSets.length; v++)
120122
{

0 commit comments

Comments
 (0)