2
0
Fork 0
mirror of https://github.com/photonstorm/phaser synced 2024-12-22 11:03:23 +00:00
phaser/src/physics/matter-js/Factory.js

582 lines
17 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 MatterTileBody = require('./MatterTileBody');
2018-02-12 13:14:50 +00:00
var PointerConstraint = require('./PointerConstraint');
2017-11-20 16:54:26 +00:00
2018-02-12 13:14:50 +00:00
/**
* @classdesc
* [description]
*
* @class Factory
* @memberOf Phaser.Physics.Matter
* @constructor
* @since 3.0.0
*
* @param {Phaser.Physics.Matter.World} world - [description]
*/
2017-11-20 16:54:26 +00:00
var Factory = new Class({
initialize:
function Factory (world)
{
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @name Phaser.Physics.Matter.Factory#world
* @type {Phaser.Physics.Matter.World}
* @since 3.0.0
*/
2017-11-20 16:54:26 +00:00
this.world = world;
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @name Phaser.Physics.Matter.Factory#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
2017-11-20 16:54:26 +00:00
this.scene = world.scene;
2018-02-12 13:14:50 +00:00
/**
* A reference to the Scene.Systems this Matter Physics instance belongs to.
*
* @name Phaser.Physics.Matter.Factory#sys
* @type {Phaser.Scenes.Systems}
* @since 3.0.0
*/
2017-11-20 16:54:26 +00:00
this.sys = world.scene.sys;
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#rectangle
* @since 3.0.0
*
* @param {number} x - [description]
* @param {number} y - [description]
* @param {number} width - [description]
* @param {number} height - [description]
* @param {object} options - [description]
*
* @return {object} A Matter JS Body.
*/
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
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#trapezoid
* @since 3.0.0
*
* @param {number} x - [description]
* @param {number} y - [description]
* @param {number} width - [description]
* @param {number} height - [description]
* @param {number} slope - [description]
* @param {object} options - [description]
*
* @return {object} A Matter JS Body.
*/
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
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#circle
* @since 3.0.0
*
* @param {number} x - [description]
* @param {number} y - [description]
* @param {number} radius - [description]
* @param {object} options - [description]
* @param {number} maxSides - [description]
*
* @return {object} A Matter JS Body.
*/
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
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#polygon
* @since 3.0.0
*
* @param {number} x - [description]
* @param {number} y - [description]
* @param {number} sides - [description]
* @param {number} radius - [description]
* @param {object} options - [description]
*
* @return {object} A Matter JS Body.
*/
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
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#fromVertices
* @since 3.0.0
*
* @param {number} x - [description]
* @param {number} y - [description]
* @param {array} vertexSets - [description]
* @param {object} options - [description]
* @param {boolean} flagInternal - [description]
* @param {boolean} removeCollinear - [description]
* @param {number} minimumArea - [description]
*
* @return {object} A Matter JS Body.
*/
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
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#imageStack
* @since 3.0.0
*
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
* @param {string|integer} [frame] - An optional frame from the Texture this Game Object is rendering with.
* @param {number} x - The horizontal position of this composite in the world.
* @param {number} y - The vertical position of this composite in the world.
* @param {number} columns - The number of columns in the Composite.
* @param {number} rows - The number of rows in the Composite.
* @param {number} columnGap - The distance between each column.
* @param {number} rowGap - The distance between each row.
* @param {object} options - [description]
*
* @return {object} A Matter JS Composite Stack.
*/
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;
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#stack
* @since 3.0.0
*
* @param {number} x - The horizontal position of this composite in the world.
* @param {number} y - The vertical position of this composite in the world.
* @param {number} columns - The number of columns in the Composite.
* @param {number} rows - The number of rows in the Composite.
* @param {number} columnGap - The distance between each column.
* @param {number} rowGap - The distance between each row.
* @param {object} options - [description]
* @param {function} callback - [description]
*
* @return {object} A Matter JS Composite 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;
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#pyramid
* @since 3.0.0
*
* @param {number} x - The horizontal position of this composite in the world.
* @param {number} y - The vertical position of this composite in the world.
* @param {number} columns - The number of columns in the Composite.
* @param {number} rows - The number of rows in the Composite.
* @param {number} columnGap - The distance between each column.
* @param {number} rowGap - The distance between each row.
* @param {function} callback - [description]
*
* @return {object} A Matter JS Composite pyramid.
*/
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;
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#chain
* @since 3.0.0
*
* @param {[type]} composite - [description]
* @param {[type]} xOffsetA - [description]
* @param {[type]} yOffsetA - [description]
* @param {[type]} xOffsetB - [description]
* @param {[type]} yOffsetB - [description]
* @param {object} options - [description]
*
* @return {object} A Matter JS Composite.
*/
chain: function (composite, xOffsetA, yOffsetA, xOffsetB, yOffsetB, options)
{
return Composites.chain(composite, xOffsetA, yOffsetA, xOffsetB, yOffsetB, options);
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#mesh
* @since 3.0.0
*
* @param {[type]} composite - [description]
* @param {[type]} columns - [description]
* @param {[type]} rows - [description]
* @param {[type]} crossBrace - [description]
* @param {object} options - [description]
*
* @return {object} A Matter JS Composite.
*/
mesh: function (composite, columns, rows, crossBrace, options)
{
return Composites.mesh(composite, columns, rows, crossBrace, options);
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#newtonsCradle
* @since 3.0.0
*
* @param {number} x - [description]
* @param {number} y - [description]
* @param {number} number - [description]
* @param {number} size - [description]
* @param {number} length - [description]
*
* @return {object} A Matter JS Composite.
*/
newtonsCradle: function (x, y, number, size, length)
{
var composite = Composites.newtonsCradle(x, y, number, size, length);
this.world.add(composite);
return composite;
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#car
* @since 3.0.0
*
* @param {number} x - [description]
* @param {number} y - [description]
* @param {number} width - [description]
* @param {number} height - [description]
* @param {number} wheelSize - [description]
*
* @return {object} A Matter JS Composite.
*/
car: function (x, y, width, height, wheelSize)
{
var composite = Composites.car(x, y, width, height, wheelSize);
this.world.add(composite);
return composite;
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#softBody
* @since 3.0.0
*
* @param {number} x - The horizontal position of this composite in the world.
* @param {number} y - The vertical position of this composite in the world.
* @param {number} columns - The number of columns in the Composite.
* @param {number} rows - The number of rows in the Composite.
* @param {number} columnGap - The distance between each column.
* @param {number} rowGap - The distance between each row.
* @param {[type]} crossBrace - [description]
* @param {number} particleRadius - [description]
* @param {object} particleOptions - [description]
* @param {object} constraintOptions - [description]
*
* @return {object} A Matter JS 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
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#joint
* @since 3.0.0
*
* @param {[type]} bodyA - [description]
* @param {[type]} bodyB - [description]
* @param {[type]} length - [description]
* @param {[type]} stiffness - [description]
* @param {object} options - [description]
*
* @return {object} A Matter JS Constraint.
*/
2017-11-22 16:23:47 +00:00
joint: function (bodyA, bodyB, length, stiffness, options)
{
return this.constraint(bodyA, bodyB, length, stiffness, options);
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#spring
* @since 3.0.0
*
* @param {[type]} bodyA - [description]
* @param {[type]} bodyB - [description]
* @param {[type]} length - [description]
* @param {[type]} stiffness - [description]
* @param {object} options - [description]
*
* @return {object} A Matter JS Constraint.
*/
2017-11-22 16:23:47 +00:00
spring: function (bodyA, bodyB, length, stiffness, options)
{
return this.constraint(bodyA, bodyB, length, stiffness, options);
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#constraint
* @since 3.0.0
*
* @param {[type]} bodyA - [description]
* @param {[type]} bodyB - [description]
* @param {[type]} length - [description]
* @param {[type]} stiffness - [description]
* @param {object} options - [description]
*
* @return {object} A Matter JS Constraint.
*/
2017-11-22 16:23:47 +00:00
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;
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#worldConstraint
* @since 3.0.0
*
* @param {[type]} bodyB - [description]
* @param {[type]} length - [description]
* @param {[type]} stiffness - [description]
* @param {object} options - [description]
*
* @return {object} A Matter JS Constraint.
*/
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;
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#mouseSpring
* @since 3.0.0
*
* @param {object} options - [description]
*
* @return {object} A Matter JS Constraint.
*/
mouseSpring: function (options)
{
return this.pointerConstraint(options);
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#pointerConstraint
* @since 3.0.0
*
* @param {object} options - [description]
*
* @return {object} A Matter JS Constraint.
*/
pointerConstraint: function (options)
{
var pointerConstraint = new PointerConstraint(this.scene, this.world, options);
this.world.add(pointerConstraint.constraint);
return pointerConstraint;
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#image
* @since 3.0.0
*
* @param {number} x - The horizontal position of this Game Object in the world.
* @param {number} y - The vertical position of this Game Object in the world.
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
* @param {string|integer} [frame] - An optional frame from the Texture this Game Object is rendering with.
* @param {object} options - [description]
*
* @return {Phaser.Physics.Matter.MatterImage} [description]
*/
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
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#tileBody
* @since 3.0.0
*
* @param {[type]} tile - [description]
* @param {object} options - [description]
*
* @return {Phaser.Physics.Matter.MatterTileBody} [description]
*/
tileBody: function (tile, options)
{
var tileBody = new MatterTileBody(this.world, tile, options);
return tileBody;
},
2018-02-12 13:14:50 +00:00
/**
* [description]
*
* @method Phaser.Physics.Matter.Factory#sprite
* @since 3.0.0
*
* @param {number} x - The horizontal position of this Game Object in the world.
* @param {number} y - The vertical position of this Game Object in the world.
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
* @param {string|integer} [frame] - An optional frame from the Texture this Game Object is rendering with.
* @param {object} options - [description]
*
* @return {Phaser.Physics.Matter.MatterSprite} [description]
*/
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;
}
});
module.exports = Factory;