0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00

composites now have generic multi add / remove methods

This commit is contained in:
liabru 2014-03-25 15:18:54 +00:00
parent eecc29abbe
commit 3dfe9b553e
5 changed files with 122 additions and 1 deletions

View file

@ -21,6 +21,7 @@ var Body = {};
Body.create = function(options) {
var defaults = {
id: Body.nextId(),
type: 'body',
angle: 0,
position: { x: 0, y: 0 },
force: { x: 0, y: 0 },

View file

@ -22,6 +22,7 @@ var Composite = {};
Composite.create = function(options) {
return Common.extend({
id: Composite.nextId(),
type: 'composite',
parent: null,
isModified: false,
bodies: [],
@ -64,6 +65,70 @@ var Composite = {};
}
};
/**
* Generic add function. Adds one or many body(s), constraint(s) or a composite(s) to the given composite.
* @method add
* @param {composite} composite
* @param {} object
* @return {composite} The original composite with the objects added
*/
Composite.add = function(composite, object) {
var objects = [].concat(object);
for (var i = 0; i < objects.length; i++) {
var obj = objects[i];
switch (obj.type) {
case 'body':
Composite.addBody(composite, obj);
break;
case 'constraint':
Composite.addConstraint(composite, obj);
break;
case 'composite':
Composite.addComposite(composite, obj);
break;
}
}
return composite;
};
/**
* Generic remove function. Removes one or many body(s), constraint(s) or a composite(s) to the given composite.
* Optionally searching its children recursively.
* @method remove
* @param {composite} composite
* @param {} object
* @param {boolean} deep
* @return {composite} The original composite with the objects removed
*/
Composite.remove = function(composite, object, deep) {
var objects = [].concat(object);
for (var i = 0; i < objects.length; i++) {
var obj = objects[i];
switch (obj.type) {
case 'body':
Composite.removeBody(composite, obj, deep);
break;
case 'constraint':
Composite.removeConstraint(composite, obj, deep);
break;
case 'composite':
Composite.removeComposite(composite, obj, deep);
break;
}
}
return composite;
};
/**
* Description
* @method addComposite
@ -78,6 +143,43 @@ var Composite = {};
return compositeA;
};
/**
* Removes a composite from the given composite, and optionally searching its children recursively
* @method removeComposite
* @param {composite} compositeA
* @param {composite} compositeB
* @param {boolean} deep
* @return {composite} The original compositeA with the composite removed
*/
Composite.removeComposite = function(compositeA, compositeB, deep) {
var position = compositeA.composites.indexOf(compositeB);
if (position !== -1) {
Composite.removeCompositeAt(compositeA, position);
Composite.setModified(compositeA, true, true, false);
}
if (deep) {
for (var i = 0; i < compositeA.composites.length; i++){
Composite.removeComposite(compositeA.composites[i], compositeB, true);
}
}
return compositeA;
};
/**
* Removes a composite from the given composite
* @method removeCompositeAt
* @param {composite} composite
* @param {number} position
* @return {composite} The original composite with the composite removed
*/
Composite.removeCompositeAt = function(composite, position) {
composite.composites.splice(position, 1);
Composite.setModified(composite, true, true, false);
return composite;
};
/**
* Description
* @method addBody
@ -235,4 +337,19 @@ var Composite = {};
return constraints;
};
/**
* Returns all composites in the given composite, including all composites in its children, recursively
* @method allComposites
* @param {composite} composite
* @return {composite[]} All the composites
*/
Composite.allComposites = function(composite) {
var composites = [].concat(composite.composites);
for (var i = 0; i < composite.composites.length; i++)
composites = composites.concat(Composite.allComposites(composite.composites[i]));
return composites;
};
})();

View file

@ -53,6 +53,7 @@ var Constraint = {};
// option defaults
constraint.id = constraint.id || Constraint.nextId();
constraint.type = 'constraint';
constraint.stiffness = constraint.stiffness || 1;
constraint.angularStiffness = constraint.angularStiffness || 0;
constraint.angleA = constraint.bodyA ? constraint.bodyA.angle : constraint.angleA;

View file

@ -1,5 +1,7 @@
// aliases
World.add = Composite.add;
World.remove = Composite.remove;
World.addComposite = Composite.addComposite;
World.addBody = Composite.addBody;
World.addConstraint = Composite.addConstraint;

View file

@ -57,7 +57,7 @@ var Gui = {};
};
for (var i = 0; i < gui.amount; i++) {
World.addBody(engine.world, Bodies.polygon(120 + i * gui.size + i * 50, 200, gui.sides, gui.size, options));
World.add(engine.world, Bodies.polygon(120 + i * gui.size + i * 50, 200, gui.sides, gui.size, options));
}
},