Skip to content

Commit

Permalink
change plugins to use best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Nov 3, 2016
1 parent 50ad7ca commit 37d4258
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
28 changes: 9 additions & 19 deletions examples/attractorsPlugin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
(function() {

var Body = Matter.Body,
Common = Matter.Common,
Composite = Matter.Composite;

var MatterAttractors = {
name: 'matter-attractors',

Expand All @@ -12,19 +8,13 @@
for: 'matter-js@^0.10.0',

install: function(base) {
base.Body.create = Common.chain(
Matter.Body.create,
function() {
MatterAttractors.Body.init(this);
}
);
base.after('Body.create', function() {
MatterAttractors.Body.init(this);
});

base.Engine.update = Common.chain(
Matter.Engine.update,
function() {
MatterAttractors.Engine.update(this);
}
);
base.after('Engine.update', function() {
MatterAttractors.Engine.update(this);
});
},

Body: {
Expand All @@ -36,7 +26,7 @@
Engine: {
update: function(engine) {
var world = engine.world,
bodies = Composite.allBodies(world);
bodies = Matter.Composite.allBodies(world);

for (var i = 0; i < bodies.length; i += 1) {
var bodyA = bodies[i],
Expand All @@ -50,12 +40,12 @@
var attractor = attractors[k],
forceVector = attractor;

if (Common.isFunction(attractor)) {
if (Matter.Common.isFunction(attractor)) {
forceVector = attractor(bodyA, bodyB);
}

if (forceVector) {
Body.applyForce(bodyB, bodyB.position, forceVector);
Matter.Body.applyForce(bodyB, bodyB.position, forceVector);
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions examples/gravityPlugin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
(function() {

var Body = Matter.Body,
Common = Matter.Common,
Vector = Matter.Vector;

var MatterGravity = {
name: 'matter-gravity',

Expand All @@ -16,12 +12,9 @@
],

install: function(base) {
base.Body.create = Common.chain(
Matter.Body.create,
function() {
MatterGravity.Body.init(this);
}
);
base.after('Body.create', function() {
MatterGravity.Body.init(this);
});
},

Body: {
Expand All @@ -32,7 +25,9 @@
},

applyGravity: function(bodyA, bodyB) {
var bToA = Vector.sub(bodyB.position, bodyA.position),
var Vector = Matter.Vector,
Body = Matter.Body,
bToA = Vector.sub(bodyB.position, bodyA.position),
distanceSq = Vector.magnitudeSquared(bToA) || 0.0001,
normal = Vector.normalise(bToA),
magnitude = -bodyA.gravity * (bodyA.mass * bodyB.mass / distanceSq),
Expand Down
9 changes: 3 additions & 6 deletions examples/wrapPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@
for: 'matter-js@^0.10.0',

install: function(base) {
base.Engine.update = Common.chain(
Matter.Engine.update,
function() {
MatterWrap.Engine.update(this);
}
);
base.after('Engine.update', function() {
MatterWrap.Engine.update(this);
});
},

Engine: {
Expand Down

0 comments on commit 37d4258

Please sign in to comment.