phaser/v3/src/physics/matter-js/Factory.js

155 lines
3.9 KiB
JavaScript
Raw Normal View History

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');
var MatterImage = require('./MatterImage');
var MatterSprite = require('./MatterSprite');
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;
},
rectangle: function (x, y, width, height, options)
2017-11-20 16:54:26 +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
},
trapezoid: function (x, y, width, height, slope, options)
2017-11-20 16:54:26 +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
},
circle: function (x, y, radius, options, maxSides)
2017-11-20 16:54:26 +00:00
{
var body = Bodies.circle(x, y, radius, options, maxSides);
2017-11-20 16:54:26 +00:00
this.world.add(body);
2017-11-20 16:54:26 +00:00
return body;
2017-11-20 16:54:26 +00:00
},
polygon: function (x, y, sides, radius, options)
2017-11-20 16:54:26 +00:00
{
var body = Bodies.polygon(x, y, sides, radius, options);
2017-11-20 16:54:26 +00:00
this.world.add(body);
2017-11-20 16:54:26 +00:00
return body;
2017-11-20 16:54:26 +00:00
},
fromVertices: function (x, y, vertexSets, options, flagInternal, removeCollinear, minimumArea)
2017-11-20 16:54:26 +00:00
{
var body = Bodies.fromVertices(x, y, vertexSets, options, flagInternal, removeCollinear, minimumArea);
2017-11-20 16:54:26 +00:00
this.world.add(body);
2017-11-20 16:54:26 +00:00
return body;
},
2017-11-20 16:54:26 +00:00
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-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;
},
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;
},
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
},
sprite: function (x, y, key, frame, options)
2017-11-20 16:54:26 +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;
},
/*
2017-11-20 16:54:26 +00:00
group: function (children, config)
{
return this.sys.updateList.add(new PhysicsGroup(this.world, this.world.scene, children, config));
}
*/
});
module.exports = Factory;