Skip to content

Commit f41d016

Browse files
committed
Moved Matter over to use the plugin system.
1 parent bae6390 commit f41d016

5 files changed

Lines changed: 157 additions & 80 deletions

File tree

src/physics/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44

55
Arcade: require('./arcade'),
66
Impact: require('./impact'),
7-
Matter: require('./matter-js/CustomMain'),
7+
Matter: require('./matter-js'),
88
PolyDecomp: require('./poly-decomp')
99

1010
};

src/physics/matter-js/Matter.js

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
var Class = require('../../utils/Class');
2+
var Factory = require('./Factory');
3+
var GetFastValue = require('../../utils/object/GetFastValue');
4+
var GetValue = require('../../utils/object/GetValue');
5+
var MatterAttractors = require('./lib/plugins/MatterAttractors');
6+
var MatterLib = require('./lib/core/Matter');
7+
var MatterWrap = require('./lib/plugins/MatterWrap');
8+
var Merge = require('../../utils/object/Merge');
9+
var Plugin = require('./lib/core/Plugin');
10+
var PluginManager = require('../../plugins/PluginManager');
11+
var World = require('./World');
12+
13+
// Phaser.Physics.Matter.MatterPhysics
14+
15+
var MatterPhysics = new Class({
16+
17+
initialize:
18+
19+
// Referenced from the Scene PhysicsManager as `system`
20+
21+
function MatterPhysics (scene)
22+
{
23+
// The Scene that owns this plugin
24+
this.scene = scene;
25+
26+
this.systems = scene.sys;
27+
28+
this.mapping = 'matter';
29+
30+
this.systems.events.on('boot', this.boot, this);
31+
32+
this.config = this.getConfig();
33+
34+
this.world;
35+
36+
this.add;
37+
},
38+
39+
getConfig: function ()
40+
{
41+
var gameConfig = this.systems.game.config.physics;
42+
var sceneConfig = this.systems.settings.physics;
43+
44+
var config = Merge(
45+
GetFastValue(sceneConfig, 'matter', {}),
46+
GetFastValue(gameConfig, 'matter', {})
47+
);
48+
49+
return config;
50+
},
51+
52+
boot: function ()
53+
{
54+
var config = this.config;
55+
56+
this.world = new World(this.scene, config);
57+
this.add = new Factory(this.world);
58+
59+
// Matter plugins
60+
61+
if (GetValue(config, 'plugins.attractors', false))
62+
{
63+
Plugin.register(MatterAttractors);
64+
Plugin.use(MatterLib, MatterAttractors);
65+
}
66+
67+
if (GetValue(config, 'plugins.wrap', false))
68+
{
69+
Plugin.register(MatterWrap);
70+
Plugin.use(MatterLib, MatterWrap);
71+
}
72+
73+
this.systems.inject(this);
74+
75+
this.systems.events.on('update', this.update, this);
76+
this.systems.events.on('postupdate', this.postUpdate, this);
77+
this.systems.events.on('shutdown', this.shutdown, this);
78+
this.systems.events.on('destroy', this.destroy, this);
79+
},
80+
81+
enableAttractorPlugin: function ()
82+
{
83+
Plugin.register(MatterAttractors);
84+
Plugin.use(MatterLib, MatterAttractors);
85+
86+
return this;
87+
},
88+
89+
enableWrapPlugin: function ()
90+
{
91+
Plugin.register(MatterWrap);
92+
Plugin.use(MatterLib, MatterWrap);
93+
94+
return this;
95+
},
96+
97+
update: function (time, delta)
98+
{
99+
this.world.update(time, delta);
100+
},
101+
102+
postUpdate: function (time, delta)
103+
{
104+
this.world.postUpdate(time, delta);
105+
},
106+
107+
shutdown: function ()
108+
{
109+
this.world.shutdown();
110+
},
111+
112+
destroy: function ()
113+
{
114+
this.world.destroy();
115+
}
116+
117+
});
118+
119+
PluginManager.register('matterPhysics', MatterPhysics);
120+
121+
module.exports = MatterPhysics;

src/physics/matter-js/World.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -307,36 +307,38 @@ var World = new Class({
307307

308308
postUpdate: function ()
309309
{
310-
if (this.drawDebug)
310+
if (!this.drawDebug)
311311
{
312-
var graphics = this.debugGraphic;
313-
var bodies = Composite.allBodies(this.localWorld);
314-
315-
graphics.clear();
316-
graphics.lineStyle(1, this.defaults.bodyDebugColor);
312+
return;
313+
}
317314

318-
for (var i = 0; i < bodies.length; i++)
319-
{
320-
var body = bodies[i];
315+
var graphics = this.debugGraphic;
316+
var bodies = Composite.allBodies(this.localWorld);
321317

322-
if (!body.render.visible)
323-
{
324-
continue;
325-
}
318+
graphics.clear();
319+
graphics.lineStyle(1, this.defaults.bodyDebugColor);
326320

327-
var vertices = body.vertices;
321+
for (var i = 0; i < bodies.length; i++)
322+
{
323+
var body = bodies[i];
328324

329-
graphics.moveTo(vertices[0].x, vertices[0].y);
325+
if (!body.render.visible)
326+
{
327+
continue;
328+
}
330329

331-
for (var j = 1; j < vertices.length; j++)
332-
{
333-
graphics.lineTo(vertices[j].x, vertices[j].y);
334-
}
330+
var vertices = body.vertices;
335331

336-
graphics.lineTo(vertices[0].x, vertices[0].y);
332+
graphics.moveTo(vertices[0].x, vertices[0].y);
337333

338-
graphics.strokePath();
334+
for (var j = 1; j < vertices.length; j++)
335+
{
336+
graphics.lineTo(vertices[j].x, vertices[j].y);
339337
}
338+
339+
graphics.lineTo(vertices[0].x, vertices[0].y);
340+
341+
graphics.strokePath();
340342
}
341343
},
342344

@@ -362,7 +364,6 @@ var World = new Class({
362364

363365
destroy: function ()
364366
{
365-
// TODO
366367
this.shutdown();
367368
}
368369

src/physics/matter-js/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Phaser.Physics.Matter
2+
3+
module.exports = {
4+
5+
Factory: require('./Factory'),
6+
Image: require('./MatterImage'),
7+
Matter: require('./CustomMain'),
8+
MatterPhysics: require('./MatterPhysics'),
9+
Sprite: require('./MatterSprite'),
10+
World: require('./World')
11+
12+
};

0 commit comments

Comments
 (0)