/
SetBody.js
284 lines (244 loc) · 10.3 KB
/
SetBody.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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Bodies = require('../lib/factory/Bodies');
var Body = require('../lib/body/Body');
var FuzzyEquals = require('../../../math/fuzzy/Equal');
var GetFastValue = require('../../../utils/object/GetFastValue');
var PhysicsEditorParser = require('../PhysicsEditorParser');
var PhysicsJSONParser = require('../PhysicsJSONParser');
var Vertices = require('../lib/geometry/Vertices');
/**
* Enables a Matter-enabled Game Object to set its Body. Should be used as a mixin and not directly.
*
* @namespace Phaser.Physics.Matter.Components.SetBody
* @since 3.0.0
*/
var SetBody = {
/**
* Set the body on a Game Object to a rectangle.
*
* Calling this methods resets previous properties you may have set on the body, including
* plugins, mass, friction, etc. So be sure to re-apply these in the options object if needed.
*
* @method Phaser.Physics.Matter.Components.SetBody#setRectangle
* @since 3.0.0
*
* @param {number} width - Width of the rectangle.
* @param {number} height - Height of the rectangle.
* @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
*
* @return {this} This Game Object instance.
*/
setRectangle: function (width, height, options)
{
return this.setBody({ type: 'rectangle', width: width, height: height }, options);
},
/**
* Set the body on a Game Object to a circle.
*
* Calling this methods resets previous properties you may have set on the body, including
* plugins, mass, friction, etc. So be sure to re-apply these in the options object if needed.
*
* @method Phaser.Physics.Matter.Components.SetBody#setCircle
* @since 3.0.0
*
* @param {number} radius - The radius of the circle.
* @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
*
* @return {this} This Game Object instance.
*/
setCircle: function (radius, options)
{
return this.setBody({ type: 'circle', radius: radius }, options);
},
/**
* Set the body on the Game Object to a polygon shape.
*
* Calling this methods resets previous properties you may have set on the body, including
* plugins, mass, friction, etc. So be sure to re-apply these in the options object if needed.
*
* @method Phaser.Physics.Matter.Components.SetBody#setPolygon
* @since 3.0.0
*
* @param {number} radius - The "radius" of the polygon, i.e. the distance from its center to any vertex. This is also the radius of its circumcircle.
* @param {number} sides - The number of sides the polygon will have.
* @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
*
* @return {this} This Game Object instance.
*/
setPolygon: function (radius, sides, options)
{
return this.setBody({ type: 'polygon', sides: sides, radius: radius }, options);
},
/**
* Set the body on the Game Object to a trapezoid shape.
*
* Calling this methods resets previous properties you may have set on the body, including
* plugins, mass, friction, etc. So be sure to re-apply these in the options object if needed.
*
* @method Phaser.Physics.Matter.Components.SetBody#setTrapezoid
* @since 3.0.0
*
* @param {number} width - The width of the trapezoid Body.
* @param {number} height - The height of the trapezoid Body.
* @param {number} slope - The slope of the trapezoid. 0 creates a rectangle, while 1 creates a triangle. Positive values make the top side shorter, while negative values make the bottom side shorter.
* @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
*
* @return {this} This Game Object instance.
*/
setTrapezoid: function (width, height, slope, options)
{
return this.setBody({ type: 'trapezoid', width: width, height: height, slope: slope }, options);
},
/**
* Set this Game Object to use the given existing Matter Body.
*
* The body is first removed from the world before being added to this Game Object.
*
* @method Phaser.Physics.Matter.Components.SetBody#setExistingBody
* @since 3.0.0
*
* @param {MatterJS.BodyType} body - The Body this Game Object should use.
* @param {boolean} [addToWorld=true] - Should the body be immediately added to the World?
*
* @return {this} This Game Object instance.
*/
setExistingBody: function (body, addToWorld)
{
if (addToWorld === undefined) { addToWorld = true; }
if (this.body)
{
this.world.remove(this.body, true);
}
this.body = body;
for (var i = 0; i < body.parts.length; i++)
{
body.parts[i].gameObject = this;
}
var _this = this;
body.destroy = function destroy ()
{
_this.world.remove(_this.body, true);
_this.body.gameObject = null;
};
if (addToWorld)
{
if (this.world.has(body))
{
// Because it could be part of another Composite
this.world.remove(body, true);
}
this.world.add(body);
}
if (this._originComponent)
{
var rx = body.render.sprite.xOffset;
var ry = body.render.sprite.yOffset;
var comx = body.centerOfMass.x;
var comy = body.centerOfMass.y;
if (FuzzyEquals(comx, 0.5) && FuzzyEquals(comy, 0.5))
{
this.setOrigin(rx + 0.5, ry + 0.5);
}
else
{
var cx = body.centerOffset.x;
var cy = body.centerOffset.y;
this.setOrigin(rx + (cx / this.displayWidth), ry + (cy / this.displayHeight));
}
}
return this;
},
/**
* Set this Game Object to create and use a new Body based on the configuration object given.
*
* Calling this method resets previous properties you may have set on the body, including
* plugins, mass, friction, etc. So be sure to re-apply these in the options object if needed.
*
* @method Phaser.Physics.Matter.Components.SetBody#setBody
* @since 3.0.0
*
* @param {(string|Phaser.Types.Physics.Matter.MatterSetBodyConfig)} config - Either a string, such as `circle`, or a Matter Set Body Configuration object.
* @param {Phaser.Types.Physics.Matter.MatterBodyConfig} [options] - An optional Body configuration object that is used to set initial Body properties on creation.
*
* @return {this} This Game Object instance.
*/
setBody: function (config, options)
{
if (!config)
{
return this;
}
var body;
// Allow them to do: shape: 'circle' instead of shape: { type: 'circle' }
if (typeof config === 'string')
{
// Using defaults
config = { type: config };
}
var shapeType = GetFastValue(config, 'type', 'rectangle');
var bodyX = GetFastValue(config, 'x', this._tempVec2.x);
var bodyY = GetFastValue(config, 'y', this._tempVec2.y);
var bodyWidth = GetFastValue(config, 'width', this.width);
var bodyHeight = GetFastValue(config, 'height', this.height);
switch (shapeType)
{
case 'rectangle':
body = Bodies.rectangle(bodyX, bodyY, bodyWidth, bodyHeight, options);
break;
case 'circle':
var radius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
var maxSides = GetFastValue(config, 'maxSides', 25);
body = Bodies.circle(bodyX, bodyY, radius, options, maxSides);
break;
case 'trapezoid':
var slope = GetFastValue(config, 'slope', 0.5);
body = Bodies.trapezoid(bodyX, bodyY, bodyWidth, bodyHeight, slope, options);
break;
case 'polygon':
var sides = GetFastValue(config, 'sides', 5);
var pRadius = GetFastValue(config, 'radius', Math.max(bodyWidth, bodyHeight) / 2);
body = Bodies.polygon(bodyX, bodyY, sides, pRadius, options);
break;
case 'fromVertices':
case 'fromVerts':
var verts = GetFastValue(config, 'verts', null);
if (verts)
{
// Has the verts array come from Vertices.fromPath, or is it raw?
if (typeof verts === 'string')
{
verts = Vertices.fromPath(verts);
}
if (this.body && !this.body.hasOwnProperty('temp'))
{
Body.setVertices(this.body, verts);
body = this.body;
}
else
{
var flagInternal = GetFastValue(config, 'flagInternal', false);
var removeCollinear = GetFastValue(config, 'removeCollinear', 0.01);
var minimumArea = GetFastValue(config, 'minimumArea', 10);
body = Bodies.fromVertices(bodyX, bodyY, verts, options, flagInternal, removeCollinear, minimumArea);
}
}
break;
case 'fromPhysicsEditor':
body = PhysicsEditorParser.parseBody(bodyX, bodyY, config, options);
break;
case 'fromPhysicsTracer':
body = PhysicsJSONParser.parseBody(bodyX, bodyY, config, options);
break;
}
if (body)
{
this.setExistingBody(body, config.addToWorld);
}
return this;
}
};
module.exports = SetBody;