diff --git a/src/body/Body.js b/src/body/Body.js index 94997eb..47b8c24 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -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 }, diff --git a/src/body/Composite.js b/src/body/Composite.js index d0b1409..59b8164 100644 --- a/src/body/Composite.js +++ b/src/body/Composite.js @@ -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; + }; + })(); \ No newline at end of file diff --git a/src/constraint/Constraint.js b/src/constraint/Constraint.js index 194d946..0bcaa56 100644 --- a/src/constraint/Constraint.js +++ b/src/constraint/Constraint.js @@ -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; diff --git a/src/module/Outro.js b/src/module/Outro.js index 9ebd2d5..3ba5c32 100644 --- a/src/module/Outro.js +++ b/src/module/Outro.js @@ -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; diff --git a/src/render/Gui.js b/src/render/Gui.js index 9a80431..55b26ab 100644 --- a/src/render/Gui.js +++ b/src/render/Gui.js @@ -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)); } },