Skip to content

Commit ca293d7

Browse files
committed
Proxy the add and remove events and better handle debug draw injection.
1 parent bdbd749 commit ca293d7

1 file changed

Lines changed: 186 additions & 72 deletions

File tree

src/physics/matter-js/World.js

Lines changed: 186 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,6 @@ var World = new Class({
9696
*/
9797
this.walls = { left: null, right: null, top: null, bottom: null };
9898

99-
if (GetFastValue(config, 'setBounds', false))
100-
{
101-
var boundsConfig = config['setBounds'];
102-
103-
if (typeof boundsConfig === 'boolean')
104-
{
105-
this.setBounds();
106-
}
107-
else
108-
{
109-
var x = GetFastValue(boundsConfig, 'x', 0);
110-
var y = GetFastValue(boundsConfig, 'y', 0);
111-
var width = GetFastValue(boundsConfig, 'width', scene.sys.scale.width);
112-
var height = GetFastValue(boundsConfig, 'height', scene.sys.scale.height);
113-
var thickness = GetFastValue(boundsConfig, 'thickness', 64);
114-
var left = GetFastValue(boundsConfig, 'left', true);
115-
var right = GetFastValue(boundsConfig, 'right', true);
116-
var top = GetFastValue(boundsConfig, 'top', true);
117-
var bottom = GetFastValue(boundsConfig, 'bottom', true);
118-
119-
this.setBounds(x, y, width, height, thickness, left, right, top, bottom);
120-
}
121-
}
122-
12399
/**
124100
* A flag that toggles if the world is enabled or not.
125101
*
@@ -243,6 +219,15 @@ var World = new Class({
243219

244220
/**
245221
* The debug configuration object.
222+
*
223+
* The values stored in this object are read from the Matter World Config `debug` property.
224+
*
225+
* When a new Body or Constraint is added to the World, they are given the values stored in this object,
226+
* unless they have their own `render` object that will override them.
227+
*
228+
* Note that while you can modify the values of properties in this object at run-time, it will not change
229+
* any of the Matter objects _already added_. It will only impact objects newly added to the world, or one
230+
* that is removed and then re-added at a later time.
246231
*
247232
* @name Phaser.Physics.Matter.World#debugConfig
248233
* @type {Phaser.Types.Physics.Matter.MatterDebugConfig}
@@ -290,6 +275,32 @@ var World = new Class({
290275
}
291276

292277
this.setEventsProxy();
278+
279+
// Create the walls
280+
281+
if (GetFastValue(config, 'setBounds', false))
282+
{
283+
var boundsConfig = config['setBounds'];
284+
285+
if (typeof boundsConfig === 'boolean')
286+
{
287+
this.setBounds();
288+
}
289+
else
290+
{
291+
var x = GetFastValue(boundsConfig, 'x', 0);
292+
var y = GetFastValue(boundsConfig, 'y', 0);
293+
var width = GetFastValue(boundsConfig, 'width', scene.sys.scale.width);
294+
var height = GetFastValue(boundsConfig, 'height', scene.sys.scale.height);
295+
var thickness = GetFastValue(boundsConfig, 'thickness', 64);
296+
var left = GetFastValue(boundsConfig, 'left', true);
297+
var right = GetFastValue(boundsConfig, 'right', true);
298+
var top = GetFastValue(boundsConfig, 'top', true);
299+
var bottom = GetFastValue(boundsConfig, 'bottom', true);
300+
301+
this.setBounds(x, y, width, height, thickness, left, right, top, bottom);
302+
}
303+
}
293304
},
294305

295306
/**
@@ -302,7 +313,105 @@ var World = new Class({
302313
setEventsProxy: function ()
303314
{
304315
var _this = this;
316+
var debugConfig = this.debugConfig;
305317
var engine = this.engine;
318+
var world = this.localWorld;
319+
320+
// Inject debug styles
321+
322+
if (this.drawDebug)
323+
{
324+
MatterEvents.on(world, 'beforeAdd', function (event)
325+
{
326+
var objects = [].concat(event.object);
327+
328+
for (var i = 0; i < objects.length; i++)
329+
{
330+
var obj = objects[i];
331+
var render = obj.render;
332+
333+
if (obj.type === 'body')
334+
{
335+
if (render.fillColor === null)
336+
{
337+
render.fillColor = (obj.isStatic) ? debugConfig.staticFillColor : debugConfig.fillColor;
338+
}
339+
340+
if (render.strokeColor === null)
341+
{
342+
render.strokeColor = (obj.isStatic) ? debugConfig.staticStrokeColor : debugConfig.strokeColor;
343+
}
344+
345+
if (render.lineThickness === null)
346+
{
347+
render.lineThickness = debugConfig.lineThickness;
348+
}
349+
}
350+
else if (obj.type === 'constraint')
351+
{
352+
var type = render.type;
353+
354+
if (render.strokeColor === null)
355+
{
356+
if (type === 'line')
357+
{
358+
render.strokeColor = debugConfig.jointColor;
359+
}
360+
else if (type === 'pin')
361+
{
362+
render.strokeColor = debugConfig.pinColor;
363+
}
364+
else if (type === 'spring')
365+
{
366+
render.strokeColor = debugConfig.springColor;
367+
}
368+
}
369+
370+
if (render.lineThickness === null)
371+
{
372+
render.lineThickness = debugConfig.jointLineThickness;
373+
}
374+
375+
if (render.pinSize === null)
376+
{
377+
render.pinSize = debugConfig.pinSize;
378+
}
379+
380+
if (render.anchorColor === null)
381+
{
382+
render.anchorColor = debugConfig.anchorColor;
383+
}
384+
385+
if (render.anchorSize === null)
386+
{
387+
render.anchorSize = debugConfig.anchorSize;
388+
}
389+
390+
console.log(obj);
391+
}
392+
}
393+
});
394+
}
395+
396+
MatterEvents.on(world, 'beforeAdd', function (event)
397+
{
398+
_this.emit(Events.BEFORE_ADD, event);
399+
});
400+
401+
MatterEvents.on(world, 'afterAdd', function (event)
402+
{
403+
_this.emit(Events.AFTER_ADD, event);
404+
});
405+
406+
MatterEvents.on(world, 'beforeRemove', function (event)
407+
{
408+
_this.emit(Events.BEFORE_REMOVE, event);
409+
});
410+
411+
MatterEvents.on(world, 'afterRemove', function (event)
412+
{
413+
_this.emit(Events.AFTER_REMOVE, event);
414+
});
306415

307416
MatterEvents.on(engine, 'beforeUpdate', function (event)
308417
{
@@ -823,6 +932,45 @@ var World = new Class({
823932
return 1000 / 30;
824933
},
825934

935+
/**
936+
* Returns all the bodies in the Matter World, including all bodies in children, recursively.
937+
*
938+
* @method Phaser.Physics.Matter.World#getAllBodies
939+
* @since 3.22.0
940+
*
941+
* @return {MatterJS.Body[]} An array of all the Matter JS Bodies in this World.
942+
*/
943+
getAllBodies: function ()
944+
{
945+
return Composite.allBodies(this.localWorld);
946+
},
947+
948+
/**
949+
* Returns all the constraints in the Matter World, including all constraints in children, recursively.
950+
*
951+
* @method Phaser.Physics.Matter.World#getAllConstraints
952+
* @since 3.22.0
953+
*
954+
* @return {MatterJS.Constraint[]} An array of all the Matter JS Constraints in this World.
955+
*/
956+
getAllConstraints: function ()
957+
{
958+
return Composite.allConstraints(this.localWorld);
959+
},
960+
961+
/**
962+
* Returns all the composites in the Matter World, including all composites in children, recursively.
963+
*
964+
* @method Phaser.Physics.Matter.World#getAllComposites
965+
* @since 3.22.0
966+
*
967+
* @return {MatterJS.Composite[]} An array of all the Matter JS Composites in this World.
968+
*/
969+
getAllComposites: function ()
970+
{
971+
return Composite.allComposites(this.localWorld);
972+
},
973+
826974
/**
827975
* Handles the rendering of bodies and debug information to the debug Graphics object, if enabled.
828976
*
@@ -883,13 +1031,6 @@ var World = new Class({
8831031
var renderFill = config.renderFill;
8841032
var renderStroke = config.renderStroke;
8851033

886-
var fillColor = config.fillColor;
887-
var strokeColor = config.strokeColor;
888-
var lineThickness = config.lineThickness;
889-
890-
var staticFillColor = config.staticFillColor;
891-
var staticStrokeColor = config.staticStrokeColor;
892-
8931034
var staticBodySleepOpacity = config.staticBodySleepOpacity;
8941035
var sleepFillColor = config.sleepFillColor;
8951036
var sleepStrokeColor = config.sleepStrokeColor;
@@ -914,8 +1055,9 @@ var World = new Class({
9141055
}
9151056

9161057
var opacity = body.render.opacity;
917-
var lineStyle = strokeColor;
918-
var fillStyle = fillColor;
1058+
var lineStyle = body.render.strokeColor;
1059+
var fillStyle = body.render.fillColor;
1060+
var lineThickness = body.render.lineThickness;
9191061

9201062
if (showSleeping && body.isSleeping)
9211063
{
@@ -930,12 +1072,6 @@ var World = new Class({
9301072
}
9311073
}
9321074

933-
if (body.isStatic)
934-
{
935-
lineStyle = staticStrokeColor;
936-
fillStyle = staticFillColor;
937-
}
938-
9391075
if (!renderFill)
9401076
{
9411077
fillStyle = null;
@@ -1119,22 +1255,21 @@ var World = new Class({
11191255
renderJoints: function ()
11201256
{
11211257
var graphics = this.debugGraphic;
1122-
var config = this.debugConfig;
1123-
1124-
var jointColor = config.jointColor;
1125-
var jointLineThickness = config.jointLineThickness;
1126-
var pinSize = config.pinSize;
1127-
var pinColor = config.pinColor;
1128-
var springColor = config.springColor;
1129-
var anchorColor = config.anchorColor;
1130-
var anchorSize = config.anchorSize;
11311258

11321259
// Render constraints
11331260
var constraints = Composite.allConstraints(this.localWorld);
11341261

11351262
for (var i = 0; i < constraints.length; i++)
11361263
{
1137-
this.renderConstraint(constraints[i], graphics, jointColor, jointLineThickness, springColor, pinColor, pinSize, anchorColor, anchorSize);
1264+
var config = constraints[i].render;
1265+
1266+
var strokeColor = config.strokeColor;
1267+
var lineThickness = config.lineThickness;
1268+
var pinSize = config.pinSize;
1269+
var anchorColor = config.anchorColor;
1270+
var anchorSize = config.anchorSize;
1271+
1272+
this.renderConstraint(constraints[i], graphics, strokeColor, lineThickness, pinSize, anchorColor, anchorSize);
11381273
}
11391274
},
11401275

@@ -1151,15 +1286,13 @@ var World = new Class({
11511286
* @param {Phaser.GameObjects.Graphics} graphics - The Graphics object to render to.
11521287
* @param {number} lineColor - The line color used when rendering this constraint.
11531288
* @param {number} lineThickness - The line thickness.
1154-
* @param {number} springColor - The color used when rendering, if this constraint is a spring.
1155-
* @param {number} pinColor - The color used when rendering, if this constraint is a pin.
11561289
* @param {number} pinSize - If this constraint is a pin, this sets the size of the pin circle.
11571290
* @param {number} anchorColor - The color used when rendering this constraints anchors. Set to `null` to not render anchors.
11581291
* @param {number} anchorSize - The size of the anchor circle, if this constraint has anchors and is rendering them.
11591292
*
11601293
* @return {this} This Matter World instance for method chaining.
11611294
*/
1162-
renderConstraint: function (constraint, graphics, lineColor, lineThickness, springColor, pinColor, pinSize, anchorColor, anchorSize)
1295+
renderConstraint: function (constraint, graphics, lineColor, lineThickness, pinSize, anchorColor, anchorSize)
11631296
{
11641297
var render = constraint.render;
11651298

@@ -1168,16 +1301,7 @@ var World = new Class({
11681301
return this;
11691302
}
11701303

1171-
var custom = render.custom;
1172-
1173-
if (custom)
1174-
{
1175-
graphics.lineStyle(render.lineWidth, Common.colorToNumber(render.strokeStyle));
1176-
}
1177-
else
1178-
{
1179-
graphics.lineStyle(lineThickness, lineColor);
1180-
}
1304+
graphics.lineStyle(lineThickness, lineColor);
11811305

11821306
var bodyA = constraint.bodyA;
11831307
var bodyB = constraint.bodyB;
@@ -1195,11 +1319,6 @@ var World = new Class({
11951319

11961320
if (render.type === 'pin')
11971321
{
1198-
if (!custom)
1199-
{
1200-
graphics.lineStyle(lineThickness, pinColor);
1201-
}
1202-
12031322
graphics.strokeCircle(start.x, start.y, pinSize);
12041323
}
12051324
else
@@ -1218,11 +1337,6 @@ var World = new Class({
12181337

12191338
if (render.type === 'spring')
12201339
{
1221-
if (!custom)
1222-
{
1223-
graphics.lineStyle(lineThickness, springColor);
1224-
}
1225-
12261340
var delta = Vector.sub(end, start);
12271341
var normal = Vector.perp(Vector.normalise(delta));
12281342
var coils = Math.ceil(Common.clamp(constraint.length / 5, 12, 20));

0 commit comments

Comments
 (0)