Skip to content

Commit 8e7c310

Browse files
committed
Moved the fromJSON code to its own external parser
1 parent f3bbf4f commit 8e7c310

3 files changed

Lines changed: 152 additions & 39 deletions

File tree

src/physics/matter-js/Factory.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
var Bodies = require('./lib/factory/Bodies');
8-
var Body = require('./lib/body/Body');
98
var Class = require('../../utils/Class');
109
var Composites = require('./lib/factory/Composites');
1110
var Constraint = require('./lib/constraint/Constraint');
@@ -15,6 +14,7 @@ var MatterImage = require('./MatterImage');
1514
var MatterSprite = require('./MatterSprite');
1615
var MatterTileBody = require('./MatterTileBody');
1716
var PhysicsEditorParser = require('./PhysicsEditorParser');
17+
var PhysicsJSONParser = require('./PhysicsJSONParser');
1818
var PointerConstraint = require('./PointerConstraint');
1919
var Vertices = require('./lib/geometry/Vertices');
2020

@@ -309,61 +309,62 @@ var Factory = new Class({
309309
},
310310

311311
/**
312-
* **This function is still in development**
312+
* Creates a body using the supplied physics data, as provided by a JSON file.
313313
*
314-
* Creates a body using the supplied body data, as provided by a JSON file.
314+
* The data file should be loaded as JSON:
315+
*
316+
* ```javascript
317+
* preload ()
318+
* {
319+
* this.load.json('ninjas', 'assets/ninjas.json);
320+
* }
321+
*
322+
* create ()
323+
* {
324+
* const ninjaShapes = this.cache.json.get('ninjas');
325+
*
326+
* this.matter.add.fromJSON(400, 300, ninjaShapes.shinobi);
327+
* }
328+
* ```
329+
*
330+
* Do not pass the entire JSON file to this method, but instead pass one of the shapes contained within it.
331+
*
332+
* If you pas in an `options` object, any settings in there will override those in the config object.
333+
*
334+
* The structure of the JSON file is as follows:
335+
*
336+
* ```text
337+
* {
338+
* 'generator_info': // The name of the application that created the JSON data
339+
* 'shapeName': {
340+
* 'type': // The type of body
341+
* 'label': // Optional body label
342+
* 'vertices': // An array, or an array of arrays, containing the vertex data in x/y object pairs
343+
* }
344+
* }
345+
* ```
346+
*
347+
* At the time of writing, only the Phaser Physics Tracer App exports in this format.
315348
*
316349
* @method Phaser.Physics.Matter.Factory#fromJSON
317350
* @since 3.22.0
318351
*
319352
* @param {number} x - The X coordinate of the body.
320353
* @param {number} y - The Y coordinate of the body.
321-
* @param {object} data - The body data object as parsed from the JSON body format.
354+
* @param {any} config - The JSON physics data.
322355
* @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
323356
* @param {boolean} [addToWorld=true] - Should the newly created body be immediately added to the World?
324357
*
325358
* @return {MatterJS.Body} A Matter JS Body.
326359
*/
327-
fromJSON: function (x, y, data, options, addToWorld)
360+
fromJSON: function (x, y, config, options, addToWorld)
328361
{
329362
if (options === undefined) { options = {}; }
330363
if (addToWorld === undefined) { addToWorld = true; }
331364

332-
var body;
333-
var vertexSets = data.verts;
334-
335-
if (vertexSets.length === 1)
336-
{
337-
// Just a single Body
338-
options.vertices = vertexSets[0];
339-
340-
body = Body.create(options);
341-
342-
Bodies.flagCoincidentParts(body.parts);
343-
}
344-
else
345-
{
346-
var parts = [];
347-
348-
for (var i = 0; i < vertexSets.length; i++)
349-
{
350-
var part = Body.create({
351-
vertices: vertexSets[i]
352-
});
353-
354-
parts.push(part);
355-
}
356-
357-
Bodies.flagCoincidentParts(parts);
358-
359-
options.parts = parts;
365+
var body = PhysicsJSONParser.parseBody(x, y, config, options);
360366

361-
body = Body.create(options);
362-
}
363-
364-
Body.setPosition(body, { x: x, y: y });
365-
366-
if (addToWorld)
367+
if (body && addToWorld)
367368
{
368369
this.world.add(body);
369370
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var Bodies = require('./lib/factory/Bodies');
8+
var Body = require('./lib/body/Body');
9+
10+
/**
11+
* Creates a body using the supplied physics data, as provided by a JSON file.
12+
*
13+
* The data file should be loaded as JSON:
14+
*
15+
* ```javascript
16+
* preload ()
17+
* {
18+
* this.load.json('ninjas', 'assets/ninjas.json);
19+
* }
20+
*
21+
* create ()
22+
* {
23+
* const ninjaShapes = this.cache.json.get('ninjas');
24+
*
25+
* this.matter.add.fromJSON(400, 300, ninjaShapes.shinobi);
26+
* }
27+
* ```
28+
*
29+
* Do not pass the entire JSON file to this method, but instead pass one of the shapes contained within it.
30+
*
31+
* If you pas in an `options` object, any settings in there will override those in the config object.
32+
*
33+
* The structure of the JSON file is as follows:
34+
*
35+
* ```text
36+
* {
37+
* 'generator_info': // The name of the application that created the JSON data
38+
* 'shapeName': {
39+
* 'type': // The type of body
40+
* 'label': // Optional body label
41+
* 'vertices': // An array, or an array of arrays, containing the vertex data in x/y object pairs
42+
* }
43+
* }
44+
* ```
45+
*
46+
* At the time of writing, only the Phaser Physics Tracer App exports in this format.
47+
*
48+
* @namespace Phaser.Physics.Matter.PhysicsJSONParser
49+
* @since 3.22.0
50+
*/
51+
var PhysicsJSONParser = {
52+
53+
/**
54+
* Parses a body element from the given JSON data.
55+
*
56+
* @function Phaser.Physics.Matter.PhysicsJSONParser.parseBody
57+
* @since 3.22.0
58+
*
59+
* @param {number} x - The horizontal world location of the body.
60+
* @param {number} y - The vertical world location of the body.
61+
* @param {object} config - The body configuration data.
62+
* @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
63+
*
64+
* @return {MatterJS.Body} A Matter JS Body.
65+
*/
66+
parseBody: function (x, y, config, options)
67+
{
68+
if (options === undefined) { options = {}; }
69+
70+
var body;
71+
var vertexSets = config.vertices;
72+
73+
if (vertexSets.length === 1)
74+
{
75+
// Just a single Body
76+
options.vertices = vertexSets[0];
77+
78+
body = Body.create(options);
79+
80+
Bodies.flagCoincidentParts(body.parts);
81+
}
82+
else
83+
{
84+
var parts = [];
85+
86+
for (var i = 0; i < vertexSets.length; i++)
87+
{
88+
var part = Body.create({
89+
vertices: vertexSets[i]
90+
});
91+
92+
parts.push(part);
93+
}
94+
95+
Bodies.flagCoincidentParts(parts);
96+
97+
options.parts = parts;
98+
99+
body = Body.create(options);
100+
}
101+
102+
body.label = config.label;
103+
104+
Body.setPosition(body, { x: x, y: y });
105+
106+
return body;
107+
}
108+
109+
};
110+
111+
module.exports = PhysicsJSONParser;

src/physics/matter-js/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
Sprite: require('./MatterSprite'),
2020
TileBody: require('./MatterTileBody'),
2121
PhysicsEditorParser: require('./PhysicsEditorParser'),
22+
PhysicsJSONParser: require('./PhysicsJSONParser'),
2223
World: require('./World')
2324

2425
};

0 commit comments

Comments
 (0)