2017-11-21 16:53:36 +00:00
|
|
|
var Bodies = require('./lib/factory/Bodies');
|
2017-11-20 16:54:26 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-11-22 02:25:42 +00:00
|
|
|
var Composites = require('./lib/factory/Composites');
|
2017-11-22 16:23:47 +00:00
|
|
|
var Constraint = require('./lib/constraint/Constraint');
|
2017-11-21 16:53:36 +00:00
|
|
|
var MatterImage = require('./MatterImage');
|
|
|
|
var MatterSprite = require('./MatterSprite');
|
2017-11-23 01:45:58 +00:00
|
|
|
var PointerConstraint = require('./PointerConstraint');
|
2017-11-20 16:54:26 +00:00
|
|
|
|
|
|
|
// When registering a factory function 'this' refers to the GameObjectFactory context.
|
|
|
|
//
|
|
|
|
// There are several properties available to use:
|
|
|
|
//
|
|
|
|
// this.scene - a reference to the Scene that owns the GameObjectFactory
|
|
|
|
// this.displayList - a reference to the Display List the Scene owns
|
|
|
|
// this.updateList - a reference to the Update List the Scene owns
|
|
|
|
|
|
|
|
var Factory = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function Factory (world)
|
|
|
|
{
|
|
|
|
this.world = world;
|
|
|
|
|
|
|
|
this.scene = world.scene;
|
|
|
|
|
|
|
|
this.sys = world.scene.sys;
|
|
|
|
},
|
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
rectangle: function (x, y, width, height, options)
|
2017-11-20 16:54:26 +00:00
|
|
|
{
|
2017-11-21 16:53:36 +00:00
|
|
|
var body = Bodies.rectangle(x, y, width, height, options);
|
|
|
|
|
|
|
|
this.world.add(body);
|
|
|
|
|
|
|
|
return body;
|
2017-11-20 16:54:26 +00:00
|
|
|
},
|
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
trapezoid: function (x, y, width, height, slope, options)
|
2017-11-20 16:54:26 +00:00
|
|
|
{
|
2017-11-21 16:53:36 +00:00
|
|
|
var body = Bodies.trapezoid(x, y, width, height, slope, options);
|
|
|
|
|
|
|
|
this.world.add(body);
|
|
|
|
|
|
|
|
return body;
|
2017-11-20 16:54:26 +00:00
|
|
|
},
|
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
circle: function (x, y, radius, options, maxSides)
|
2017-11-20 16:54:26 +00:00
|
|
|
{
|
2017-11-21 16:53:36 +00:00
|
|
|
var body = Bodies.circle(x, y, radius, options, maxSides);
|
2017-11-20 16:54:26 +00:00
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
this.world.add(body);
|
2017-11-20 16:54:26 +00:00
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
return body;
|
2017-11-20 16:54:26 +00:00
|
|
|
},
|
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
polygon: function (x, y, sides, radius, options)
|
2017-11-20 16:54:26 +00:00
|
|
|
{
|
2017-11-21 16:53:36 +00:00
|
|
|
var body = Bodies.polygon(x, y, sides, radius, options);
|
2017-11-20 16:54:26 +00:00
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
this.world.add(body);
|
2017-11-20 16:54:26 +00:00
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
return body;
|
2017-11-20 16:54:26 +00:00
|
|
|
},
|
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
fromVertices: function (x, y, vertexSets, options, flagInternal, removeCollinear, minimumArea)
|
2017-11-20 16:54:26 +00:00
|
|
|
{
|
2017-11-21 16:53:36 +00:00
|
|
|
var body = Bodies.fromVertices(x, y, vertexSets, options, flagInternal, removeCollinear, minimumArea);
|
2017-11-20 16:54:26 +00:00
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
this.world.add(body);
|
2017-11-20 16:54:26 +00:00
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
return body;
|
|
|
|
},
|
2017-11-20 16:54:26 +00:00
|
|
|
|
2017-11-23 16:55:28 +00:00
|
|
|
imageStack: function (key, frame, x, y, columns, rows, columnGap, rowGap, options)
|
|
|
|
{
|
|
|
|
if (columnGap === undefined) { columnGap = 0; }
|
|
|
|
if (rowGap === undefined) { rowGap = 0; }
|
|
|
|
if (options === undefined) { options = {}; }
|
|
|
|
|
|
|
|
var world = this.world;
|
|
|
|
var displayList = this.sys.displayList;
|
|
|
|
|
|
|
|
options.addToWorld = false;
|
|
|
|
|
|
|
|
var stack = Composites.stack(x, y, columns, rows, columnGap, rowGap, function (x, y)
|
|
|
|
{
|
|
|
|
var image = new MatterImage(world, x, y, key, frame, options);
|
|
|
|
|
|
|
|
displayList.add(image);
|
|
|
|
|
|
|
|
return image.body;
|
|
|
|
});
|
|
|
|
|
|
|
|
world.add(stack);
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
},
|
|
|
|
|
2017-11-22 02:25:42 +00:00
|
|
|
stack: function (x, y, columns, rows, columnGap, rowGap, callback)
|
|
|
|
{
|
|
|
|
var stack = Composites.stack(x, y, columns, rows, columnGap, rowGap, callback);
|
|
|
|
|
|
|
|
this.world.add(stack);
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
},
|
|
|
|
|
2017-11-27 16:29:43 +00:00
|
|
|
pyramid: function (x, y, columns, rows, columnGap, rowGap, callback)
|
|
|
|
{
|
|
|
|
var stack = Composites.pyramid(x, y, columns, rows, columnGap, rowGap, callback);
|
|
|
|
|
|
|
|
this.world.add(stack);
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
},
|
|
|
|
|
|
|
|
chain: function (composite, xOffsetA, yOffsetA, xOffsetB, yOffsetB, options)
|
|
|
|
{
|
|
|
|
return Composites.chain(composite, xOffsetA, yOffsetA, xOffsetB, yOffsetB, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
mesh: function (composite, columns, rows, crossBrace, options)
|
|
|
|
{
|
|
|
|
return Composites.mesh(composite, columns, rows, crossBrace, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
newtonsCradle: function (x, y, number, size, length)
|
|
|
|
{
|
|
|
|
var composite = Composites.newtonsCradle(x, y, number, size, length);
|
|
|
|
|
|
|
|
this.world.add(composite);
|
|
|
|
|
|
|
|
return composite;
|
|
|
|
},
|
|
|
|
|
|
|
|
car: function (x, y, width, height, wheelSize)
|
|
|
|
{
|
|
|
|
var composite = Composites.car(x, y, width, height, wheelSize);
|
|
|
|
|
|
|
|
this.world.add(composite);
|
|
|
|
|
|
|
|
return composite;
|
|
|
|
},
|
|
|
|
|
|
|
|
softBody: function (x, y, columns, rows, columnGap, rowGap, crossBrace, particleRadius, particleOptions, constraintOptions)
|
|
|
|
{
|
|
|
|
var composite = Composites.softBody(x, y, columns, rows, columnGap, rowGap, crossBrace, particleRadius, particleOptions, constraintOptions);
|
|
|
|
|
|
|
|
this.world.add(composite);
|
|
|
|
|
|
|
|
return composite;
|
|
|
|
},
|
|
|
|
|
2017-11-22 16:23:47 +00:00
|
|
|
// To help those used to Box2D
|
|
|
|
joint: function (bodyA, bodyB, length, stiffness, options)
|
|
|
|
{
|
|
|
|
return this.constraint(bodyA, bodyB, length, stiffness, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
spring: function (bodyA, bodyB, length, stiffness, options)
|
|
|
|
{
|
|
|
|
return this.constraint(bodyA, bodyB, length, stiffness, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
constraint: function (bodyA, bodyB, length, stiffness, options)
|
|
|
|
{
|
|
|
|
if (stiffness === undefined) { stiffness = 1; }
|
|
|
|
if (options === undefined) { options = {}; }
|
|
|
|
|
|
|
|
options.bodyA = (bodyA.type === 'body') ? bodyA : bodyA.body;
|
|
|
|
options.bodyB = (bodyB.type === 'body') ? bodyB : bodyB.body;
|
|
|
|
options.length = length;
|
|
|
|
options.stiffness = stiffness;
|
|
|
|
|
|
|
|
var constraint = Constraint.create(options);
|
|
|
|
|
|
|
|
this.world.add(constraint);
|
|
|
|
|
|
|
|
return constraint;
|
|
|
|
},
|
|
|
|
|
2017-11-27 16:29:43 +00:00
|
|
|
worldConstraint: function (bodyB, length, stiffness, options)
|
|
|
|
{
|
|
|
|
if (stiffness === undefined) { stiffness = 1; }
|
|
|
|
if (options === undefined) { options = {}; }
|
|
|
|
|
|
|
|
options.bodyB = (bodyB.type === 'body') ? bodyB : bodyB.body;
|
|
|
|
options.length = length;
|
|
|
|
options.stiffness = stiffness;
|
|
|
|
|
|
|
|
var constraint = Constraint.create(options);
|
|
|
|
|
|
|
|
this.world.add(constraint);
|
|
|
|
|
|
|
|
return constraint;
|
|
|
|
},
|
|
|
|
|
2017-11-23 01:45:58 +00:00
|
|
|
mouseSpring: function (options)
|
|
|
|
{
|
|
|
|
return this.pointerConstraint(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
pointerConstraint: function (options)
|
|
|
|
{
|
|
|
|
var pointerConstraint = new PointerConstraint(this.scene, this.world, options);
|
|
|
|
|
|
|
|
this.world.add(pointerConstraint.constraint);
|
|
|
|
|
|
|
|
return pointerConstraint;
|
|
|
|
},
|
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
image: function (x, y, key, frame, options)
|
|
|
|
{
|
|
|
|
var image = new MatterImage(this.world, x, y, key, frame, options);
|
|
|
|
|
|
|
|
this.sys.displayList.add(image);
|
|
|
|
|
|
|
|
return image;
|
2017-11-20 16:54:26 +00:00
|
|
|
},
|
|
|
|
|
2017-11-21 16:53:36 +00:00
|
|
|
sprite: function (x, y, key, frame, options)
|
2017-11-20 16:54:26 +00:00
|
|
|
{
|
2017-11-21 16:53:36 +00:00
|
|
|
var sprite = new MatterSprite(this.world, x, y, key, frame, options);
|
2017-11-20 16:54:26 +00:00
|
|
|
|
|
|
|
this.sys.displayList.add(sprite);
|
|
|
|
this.sys.updateList.add(sprite);
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Factory;
|