Skip to content

Commit 27930fb

Browse files
committed
JSDoc MatterTileBody
1 parent 92fb2a9 commit 27930fb

1 file changed

Lines changed: 106 additions & 4 deletions

File tree

src/physics/matter-js/MatterTileBody.js

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,52 @@ var MatterTileBody = new Class({
2323

2424
initialize:
2525

26+
/**
27+
* A wrapper around a Tile that provides access to a corresponding Matter body. A tile can only
28+
* have one Matter body associated with it. You can either pass in an existing Matter body for
29+
* the tile or allow the constructor to create the corresponding body for you. If the Tile has a
30+
* collision group (defined in Tiled), those shapes will be used to create the body. If not, the
31+
* tile's rectangle bounding box will be used.
32+
*
33+
* The corresponding body will be accessible on the Tile itself via Tile.physics.matterBody.
34+
*
35+
* Note: not all Tiled collision shapes are supported. See
36+
* Phaser.Physics.Matter.TileBody#setFromTileCollision for more information.
37+
*
38+
* @class MatterTileBody
39+
* @memberOf Phaser.Physics.Matter.TileBody
40+
* @constructor
41+
* @since 3.0.0
42+
*
43+
* @param {Phaser.Physics.Matter.World} world - [description]
44+
* @param {Phaser.GameObjects.Tile} tile - The target tile that should have a Matter body.
45+
* @param {object} [options] - Options to be used when creating the Matter body. See
46+
* Phaser.Physics.Matter.Matter.Body for a list of what Matter accepts.
47+
* @param {Phaser.Physics.Matter.Matter.Body} [options.body=null] - An existing Matter body to
48+
* be used instead of creating a new one.
49+
* @param {boolean} [options.isStatic=true] - Whether or not the newly created body should be
50+
* made static. This defaults to true since typically tiles should not be moved.
51+
* @param {boolean} [options.addToWorld=true] - Whether or not to add the newly created body (or
52+
* existing body if options.body is used) to the Matter world.
53+
*/
2654
function MatterTileBody (world, tile, options)
2755
{
56+
/**
57+
* The tile object the body is associated with.
58+
* @property {Phaser.GameObjects.Tile} tile
59+
* @since 3.0.0
60+
*/
2861
this.tile = tile;
62+
63+
/**
64+
* The Matter world the body exists within.
65+
* @property {Phaser.Physics.Matter.World} world
66+
* @since 3.0.0
67+
*/
2968
this.world = world;
3069

31-
// A tile is only allowed one matter body
70+
// Install a reference to 'this' on the tile and ensure there can only be one matter body
71+
// associated with the tile
3272
if (tile.physics.matterBody)
3373
{
3474
tile.physics.matterBody.destroy();
@@ -58,6 +98,20 @@ var MatterTileBody = new Class({
5898
}
5999
},
60100

101+
/**
102+
* Sets the current body to a rectangle that matches the bounds of the tile.
103+
*
104+
* @method Phaser.Physics.Matter.TileBody#setFromTileRectangle
105+
* @since 3.0.0
106+
*
107+
* @param {object} [options] - Options to be used when creating the Matter body. See
108+
* Phaser.Physics.Matter.Matter.Body for a list of what Matter accepts.
109+
* @param {boolean} [options.isStatic=true] - Whether or not the newly created body should be
110+
* made static. This defaults to true since typically tiles should not be moved.
111+
* @param {boolean} [options.addToWorld=true] - Whether or not to add the newly created body (or
112+
* existing body if options.body is used) to the Matter world.
113+
* @return {this}
114+
*/
61115
setFromTileRectangle: function (options)
62116
{
63117
if (options === undefined) { options = {}; }
@@ -73,6 +127,27 @@ var MatterTileBody = new Class({
73127
return this;
74128
},
75129

130+
/**
131+
* Sets the current body from the collision group associated with the Tile. This is typically
132+
* set up in Tiled's collision editor.
133+
*
134+
* Note: Matter doesn't support all shapes from Tiled. Rectangles and polygons are directly
135+
* supported. Ellipses are converted into circle bodies. Polylines are treated as if they are
136+
* closed polygons. If a tile has multiple shapes, a multi-part body will be created. Concave
137+
* shapes are supported if poly-decomp library is included, but it's usually best to manually
138+
* decompose a concave polygon into multiple convex polygons.
139+
*
140+
* @method Phaser.Physics.Matter.TileBody#setFromTileCollision
141+
* @since 3.0.0
142+
*
143+
* @param {object} [options] - Options to be used when creating the Matter body. See
144+
* Phaser.Physics.Matter.Matter.Body for a list of what Matter accepts.
145+
* @param {boolean} [options.isStatic=true] - Whether or not the newly created body should be
146+
* made static. This defaults to true since typically tiles should not be moved.
147+
* @param {boolean} [options.addToWorld=true] - Whether or not to add the newly created body (or
148+
* existing body if options.body is used) to the Matter world.
149+
* @return {this}
150+
*/
76151
setFromTileCollision: function (options)
77152
{
78153
if (options === undefined) { options = {}; }
@@ -104,9 +179,7 @@ var MatterTileBody = new Class({
104179
}
105180
else if (object.polygon || object.polyline)
106181
{
107-
// Polygons and polylines are both treated as closed polygons. Concave shapes are
108-
// supported if poly-decomp library is included, but it's best to manually create
109-
// convex polygons.
182+
// Polygons and polylines are both treated as closed polygons
110183
var originalPoints = object.polygon ? object.polygon : object.polyline;
111184
var points = originalPoints.map(function (p) {
112185
return { x: p[0], y: p[1] };
@@ -137,6 +210,17 @@ var MatterTileBody = new Class({
137210
return this;
138211
},
139212

213+
/**
214+
* Sets the current body to the given body. This will remove the previous body, if one already
215+
* exists.
216+
*
217+
* @method Phaser.Physics.Matter.TileBody#setBody
218+
* @since 3.0.0
219+
*
220+
* @param {Phaser.Physics.Matter.Matter.Body} body - The new Matter body to use.
221+
* @param {boolean} [addToWorld=true] - Whether or not to add the body to the Matter world.
222+
* @return {this}
223+
*/
140224
setBody: function (body, addToWorld)
141225
{
142226
if (addToWorld === undefined) { addToWorld = true; }
@@ -157,6 +241,14 @@ var MatterTileBody = new Class({
157241
return this;
158242
},
159243

244+
/**
245+
* Removes the current body from the MatterTileBody and from the Matter world
246+
*
247+
* @method Phaser.Physics.Matter.TileBody#removeBody
248+
* @since 3.0.0
249+
*
250+
* @return {this}
251+
*/
160252
removeBody: function ()
161253
{
162254
if (this.body)
@@ -165,8 +257,18 @@ var MatterTileBody = new Class({
165257
this.body.gameObject = undefined;
166258
this.body = undefined;
167259
}
260+
261+
return this;
168262
},
169263

264+
/**
265+
* Removes the current body from the tile and the world.
266+
*
267+
* @method Phaser.Physics.Matter.TileBody#removeBody
268+
* @since 3.0.0
269+
*
270+
* @return {this}
271+
*/
170272
destroy: function ()
171273
{
172274
this.removeBody();

0 commit comments

Comments
 (0)