Moved Matter over to use the plugin system.

This commit is contained in:
Richard Davey 2018-01-17 15:22:16 +00:00
parent bae63901d7
commit f41d016c9f
5 changed files with 161 additions and 84 deletions

View file

@ -4,7 +4,7 @@ module.exports = {
Arcade: require('./arcade'),
Impact: require('./impact'),
Matter: require('./matter-js/CustomMain'),
Matter: require('./matter-js'),
PolyDecomp: require('./poly-decomp')
};

View file

@ -1,57 +0,0 @@
var Class = require('../../utils/Class');
var Factory = require('./Factory');
var GetValue = require('../../utils/object/GetValue');
var MatterAttractors = require('./lib/plugins/MatterAttractors');
var MatterLib = require('./lib/core/Matter');
var MatterWrap = require('./lib/plugins/MatterWrap');
var Plugin = require('./lib/core/Plugin');
var World = require('./World');
var Matter = new Class({
initialize:
// Referenced from the Scene PhysicsManager as `system`
function Matter (physicsManager, config)
{
this.config = config;
physicsManager.world = new World(physicsManager.scene, config);
physicsManager.add = new Factory(physicsManager.world);
// Matter plugins
if (GetValue(config, 'plugins.attractors', false))
{
Plugin.register(MatterAttractors);
Plugin.use(MatterLib, MatterAttractors);
}
if (GetValue(config, 'plugins.wrap', false))
{
Plugin.register(MatterWrap);
Plugin.use(MatterLib, MatterWrap);
}
},
enableAttractorPlugin: function ()
{
Plugin.register(MatterAttractors);
Plugin.use(MatterLib, MatterAttractors);
return this;
},
enableWrapPlugin: function ()
{
Plugin.register(MatterWrap);
Plugin.use(MatterLib, MatterWrap);
return this;
}
});
module.exports = Matter;

View file

@ -0,0 +1,121 @@
var Class = require('../../utils/Class');
var Factory = require('./Factory');
var GetFastValue = require('../../utils/object/GetFastValue');
var GetValue = require('../../utils/object/GetValue');
var MatterAttractors = require('./lib/plugins/MatterAttractors');
var MatterLib = require('./lib/core/Matter');
var MatterWrap = require('./lib/plugins/MatterWrap');
var Merge = require('../../utils/object/Merge');
var Plugin = require('./lib/core/Plugin');
var PluginManager = require('../../plugins/PluginManager');
var World = require('./World');
// Phaser.Physics.Matter.MatterPhysics
var MatterPhysics = new Class({
initialize:
// Referenced from the Scene PhysicsManager as `system`
function MatterPhysics (scene)
{
// The Scene that owns this plugin
this.scene = scene;
this.systems = scene.sys;
this.mapping = 'matter';
this.systems.events.on('boot', this.boot, this);
this.config = this.getConfig();
this.world;
this.add;
},
getConfig: function ()
{
var gameConfig = this.systems.game.config.physics;
var sceneConfig = this.systems.settings.physics;
var config = Merge(
GetFastValue(sceneConfig, 'matter', {}),
GetFastValue(gameConfig, 'matter', {})
);
return config;
},
boot: function ()
{
var config = this.config;
this.world = new World(this.scene, config);
this.add = new Factory(this.world);
// Matter plugins
if (GetValue(config, 'plugins.attractors', false))
{
Plugin.register(MatterAttractors);
Plugin.use(MatterLib, MatterAttractors);
}
if (GetValue(config, 'plugins.wrap', false))
{
Plugin.register(MatterWrap);
Plugin.use(MatterLib, MatterWrap);
}
this.systems.inject(this);
this.systems.events.on('update', this.update, this);
this.systems.events.on('postupdate', this.postUpdate, this);
this.systems.events.on('shutdown', this.shutdown, this);
this.systems.events.on('destroy', this.destroy, this);
},
enableAttractorPlugin: function ()
{
Plugin.register(MatterAttractors);
Plugin.use(MatterLib, MatterAttractors);
return this;
},
enableWrapPlugin: function ()
{
Plugin.register(MatterWrap);
Plugin.use(MatterLib, MatterWrap);
return this;
},
update: function (time, delta)
{
this.world.update(time, delta);
},
postUpdate: function (time, delta)
{
this.world.postUpdate(time, delta);
},
shutdown: function ()
{
this.world.shutdown();
},
destroy: function ()
{
this.world.destroy();
}
});
PluginManager.register('matterPhysics', MatterPhysics);
module.exports = MatterPhysics;

View file

@ -307,36 +307,38 @@ var World = new Class({
postUpdate: function ()
{
if (this.drawDebug)
if (!this.drawDebug)
{
var graphics = this.debugGraphic;
var bodies = Composite.allBodies(this.localWorld);
return;
}
graphics.clear();
graphics.lineStyle(1, this.defaults.bodyDebugColor);
var graphics = this.debugGraphic;
var bodies = Composite.allBodies(this.localWorld);
for (var i = 0; i < bodies.length; i++)
graphics.clear();
graphics.lineStyle(1, this.defaults.bodyDebugColor);
for (var i = 0; i < bodies.length; i++)
{
var body = bodies[i];
if (!body.render.visible)
{
var body = bodies[i];
if (!body.render.visible)
{
continue;
}
var vertices = body.vertices;
graphics.moveTo(vertices[0].x, vertices[0].y);
for (var j = 1; j < vertices.length; j++)
{
graphics.lineTo(vertices[j].x, vertices[j].y);
}
graphics.lineTo(vertices[0].x, vertices[0].y);
graphics.strokePath();
continue;
}
var vertices = body.vertices;
graphics.moveTo(vertices[0].x, vertices[0].y);
for (var j = 1; j < vertices.length; j++)
{
graphics.lineTo(vertices[j].x, vertices[j].y);
}
graphics.lineTo(vertices[0].x, vertices[0].y);
graphics.strokePath();
}
},
@ -362,7 +364,6 @@ var World = new Class({
destroy: function ()
{
// TODO
this.shutdown();
}

View file

@ -0,0 +1,12 @@
// Phaser.Physics.Matter
module.exports = {
Factory: require('./Factory'),
Image: require('./MatterImage'),
Matter: require('./CustomMain'),
MatterPhysics: require('./MatterPhysics'),
Sprite: require('./MatterSprite'),
World: require('./World')
};