-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
MatterTileBody.js
327 lines (281 loc) · 10.8 KB
/
MatterTileBody.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Bodies = require('./lib/factory/Bodies');
var Body = require('./lib/body/Body');
var Class = require('../../utils/Class');
var Components = require('./components');
var DeepCopy = require('../../utils/object/DeepCopy');
var EventEmitter = require('eventemitter3');
var GetFastValue = require('../../utils/object/GetFastValue');
var HasValue = require('../../utils/object/HasValue');
var Vertices = require('./lib/geometry/Vertices');
/**
* @classdesc
* A wrapper around a Tile that provides access to a corresponding Matter body. A tile can only
* have one Matter body associated with it. You can either pass in an existing Matter body for
* the tile or allow the constructor to create the corresponding body for you. If the Tile has a
* collision group (defined in Tiled), those shapes will be used to create the body. If not, the
* tile's rectangle bounding box will be used.
*
* The corresponding body will be accessible on the Tile itself via Tile.physics.matterBody.
*
* Note: not all Tiled collision shapes are supported. See
* Phaser.Physics.Matter.TileBody#setFromTileCollision for more information.
*
* @class TileBody
* @memberof Phaser.Physics.Matter
* @extends Phaser.Events.EventEmitter
* @constructor
* @since 3.0.0
*
* @extends Phaser.Physics.Matter.Components.Bounce
* @extends Phaser.Physics.Matter.Components.Collision
* @extends Phaser.Physics.Matter.Components.Friction
* @extends Phaser.Physics.Matter.Components.Gravity
* @extends Phaser.Physics.Matter.Components.Mass
* @extends Phaser.Physics.Matter.Components.Sensor
* @extends Phaser.Physics.Matter.Components.Sleep
* @extends Phaser.Physics.Matter.Components.Static
*
* @param {Phaser.Physics.Matter.World} world - The Matter world instance this body belongs to.
* @param {Phaser.Tilemaps.Tile} tile - The target tile that should have a Matter body.
* @param {Phaser.Types.Physics.Matter.MatterTileOptions} [options] - Options to be used when creating the Matter body.
*/
var MatterTileBody = new Class({
Extends: EventEmitter,
Mixins: [
Components.Bounce,
Components.Collision,
Components.Friction,
Components.Gravity,
Components.Mass,
Components.Sensor,
Components.Sleep,
Components.Static
],
initialize:
function MatterTileBody (world, tile, options)
{
EventEmitter.call(this);
/**
* The tile object the body is associated with.
*
* @name Phaser.Physics.Matter.TileBody#tile
* @type {Phaser.Tilemaps.Tile}
* @since 3.0.0
*/
this.tile = tile;
/**
* The Matter world the body exists within.
*
* @name Phaser.Physics.Matter.TileBody#world
* @type {Phaser.Physics.Matter.World}
* @since 3.0.0
*/
this.world = world;
// Install a reference to 'this' on the tile and ensure there can only be one matter body
// associated with the tile
if (tile.physics.matterBody)
{
tile.physics.matterBody.destroy();
}
tile.physics.matterBody = this;
// Set the body either from an existing body (if provided), the shapes in the tileset
// collision layer (if it exists) or a rectangle matching the tile.
var body = GetFastValue(options, 'body', null);
var addToWorld = GetFastValue(options, 'addToWorld', true);
if (!body)
{
var collisionGroup = tile.getCollisionGroup();
var collisionObjects = GetFastValue(collisionGroup, 'objects', []);
if (collisionObjects.length > 0)
{
this.setFromTileCollision(options);
}
else
{
this.setFromTileRectangle(options);
}
}
else
{
this.setBody(body, addToWorld);
}
if (tile.flipX || tile.flipY)
{
var rotationPoint = { x: tile.getCenterX(), y: tile.getCenterY() };
var scaleX = (tile.flipX) ? -1 : 1;
var scaleY = (tile.flipY) ? -1 : 1;
Body.scale(body, scaleX, scaleY, rotationPoint);
}
},
/**
* Sets the current body to a rectangle that matches the bounds of the tile.
*
* @method Phaser.Physics.Matter.TileBody#setFromTileRectangle
* @since 3.0.0
*
* @param {Phaser.Types.Physics.Matter.MatterBodyTileOptions} [options] - Options to be used when creating the Matter body. See MatterJS.Body for a list of what Matter accepts.
*
* @return {Phaser.Physics.Matter.TileBody} This TileBody object.
*/
setFromTileRectangle: function (options)
{
if (options === undefined) { options = {}; }
if (!HasValue(options, 'isStatic')) { options.isStatic = true; }
if (!HasValue(options, 'addToWorld')) { options.addToWorld = true; }
var bounds = this.tile.getBounds();
var cx = bounds.x + (bounds.width / 2);
var cy = bounds.y + (bounds.height / 2);
var body = Bodies.rectangle(cx, cy, bounds.width, bounds.height, options);
this.setBody(body, options.addToWorld);
return this;
},
/**
* Sets the current body from the collision group associated with the Tile. This is typically
* set up in Tiled's collision editor.
*
* Note: Matter doesn't support all shapes from Tiled. Rectangles and polygons are directly
* supported. Ellipses are converted into circle bodies. Polylines are treated as if they are
* closed polygons. If a tile has multiple shapes, a multi-part body will be created. Concave
* shapes are supported if poly-decomp library is included. Decomposition is not guaranteed to
* work for complex shapes (e.g. holes), so it's often best to manually decompose a concave
* polygon into multiple convex polygons yourself.
*
* @method Phaser.Physics.Matter.TileBody#setFromTileCollision
* @since 3.0.0
*
* @param {Phaser.Types.Physics.Matter.MatterBodyTileOptions} [options] - Options to be used when creating the Matter body. See MatterJS.Body for a list of what Matter accepts.
*
* @return {Phaser.Physics.Matter.TileBody} This TileBody object.
*/
setFromTileCollision: function (options)
{
if (options === undefined) { options = {}; }
if (!HasValue(options, 'isStatic')) { options.isStatic = true; }
if (!HasValue(options, 'addToWorld')) { options.addToWorld = true; }
var sx = this.tile.tilemapLayer.scaleX;
var sy = this.tile.tilemapLayer.scaleY;
var tileX = this.tile.getLeft();
var tileY = this.tile.getTop();
var collisionGroup = this.tile.getCollisionGroup();
var collisionObjects = GetFastValue(collisionGroup, 'objects', []);
var parts = [];
for (var i = 0; i < collisionObjects.length; i++)
{
var object = collisionObjects[i];
var ox = tileX + (object.x * sx);
var oy = tileY + (object.y * sy);
var ow = object.width * sx;
var oh = object.height * sy;
var body = null;
if (object.rectangle)
{
body = Bodies.rectangle(ox + ow / 2, oy + oh / 2, ow, oh, options);
}
else if (object.ellipse)
{
body = Bodies.circle(ox + ow / 2, oy + oh / 2, ow / 2, options);
}
else if (object.polygon || object.polyline)
{
// Polygons and polylines are both treated as closed polygons
var originalPoints = object.polygon ? object.polygon : object.polyline;
var points = originalPoints.map(function (p)
{
return { x: p.x * sx, y: p.y * sy };
});
var vertices = Vertices.create(points);
// Points are relative to the object's origin (first point placed in Tiled), but
// matter expects points to be relative to the center of mass. This only applies to
// convex shapes. When a concave shape is decomposed, multiple parts are created and
// the individual parts are positioned relative to (ox, oy).
//
// Update: 8th January 2019 - the latest version of Matter needs the Vertices adjusted,
// regardless if convex or concave.
var center = Vertices.centre(vertices);
ox += center.x;
oy += center.y;
body = Bodies.fromVertices(ox, oy, vertices, options);
}
if (body)
{
parts.push(body);
}
}
if (parts.length === 1)
{
this.setBody(parts[0], options.addToWorld);
}
else if (parts.length > 1)
{
var tempOptions = DeepCopy(options);
tempOptions.parts = parts;
this.setBody(Body.create(tempOptions), tempOptions.addToWorld);
}
return this;
},
/**
* Sets the current body to the given body. This will remove the previous body, if one already
* exists.
*
* @method Phaser.Physics.Matter.TileBody#setBody
* @since 3.0.0
*
* @param {MatterJS.BodyType} body - The new Matter body to use.
* @param {boolean} [addToWorld=true] - Whether or not to add the body to the Matter world.
*
* @return {Phaser.Physics.Matter.TileBody} This TileBody object.
*/
setBody: function (body, addToWorld)
{
if (addToWorld === undefined) { addToWorld = true; }
if (this.body)
{
this.removeBody();
}
this.body = body;
this.body.gameObject = this;
if (addToWorld)
{
this.world.add(this.body);
}
return this;
},
/**
* Removes the current body from the TileBody and from the Matter world
*
* @method Phaser.Physics.Matter.TileBody#removeBody
* @since 3.0.0
*
* @return {Phaser.Physics.Matter.TileBody} This TileBody object.
*/
removeBody: function ()
{
if (this.body)
{
this.world.remove(this.body);
this.body.gameObject = undefined;
this.body = undefined;
}
return this;
},
/**
* Removes the current body from the tile and the world.
*
* @method Phaser.Physics.Matter.TileBody#destroy
* @since 3.0.0
*
* @return {Phaser.Physics.Matter.TileBody} This TileBody object.
*/
destroy: function ()
{
this.removeBody();
this.tile.physics.matterBody = undefined;
this.removeAllListeners();
}
});
module.exports = MatterTileBody;