From f41d016c9f0541c54a081d88c555fa8da7ec2050 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Wed, 17 Jan 2018 15:22:16 +0000 Subject: [PATCH] Moved Matter over to use the plugin system. --- src/physics/index.js | 2 +- src/physics/matter-js/Matter.js | 57 ------------ src/physics/matter-js/MatterPhysics.js | 121 +++++++++++++++++++++++++ src/physics/matter-js/World.js | 53 +++++------ src/physics/matter-js/index.js | 12 +++ 5 files changed, 161 insertions(+), 84 deletions(-) delete mode 100644 src/physics/matter-js/Matter.js create mode 100644 src/physics/matter-js/MatterPhysics.js create mode 100644 src/physics/matter-js/index.js diff --git a/src/physics/index.js b/src/physics/index.js index 34ab4b87b..096f30cd3 100644 --- a/src/physics/index.js +++ b/src/physics/index.js @@ -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') }; diff --git a/src/physics/matter-js/Matter.js b/src/physics/matter-js/Matter.js deleted file mode 100644 index 32e85313a..000000000 --- a/src/physics/matter-js/Matter.js +++ /dev/null @@ -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; diff --git a/src/physics/matter-js/MatterPhysics.js b/src/physics/matter-js/MatterPhysics.js new file mode 100644 index 000000000..af89398a4 --- /dev/null +++ b/src/physics/matter-js/MatterPhysics.js @@ -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; diff --git a/src/physics/matter-js/World.js b/src/physics/matter-js/World.js index a77ad126e..18d945359 100644 --- a/src/physics/matter-js/World.js +++ b/src/physics/matter-js/World.js @@ -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(); } diff --git a/src/physics/matter-js/index.js b/src/physics/matter-js/index.js new file mode 100644 index 000000000..813aa2874 --- /dev/null +++ b/src/physics/matter-js/index.js @@ -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') + +};