mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-25 13:39:06 -05:00
optimised Matter.Composite
This commit is contained in:
parent
d8a6380899
commit
52e797791b
1 changed files with 42 additions and 13 deletions
|
@ -39,7 +39,12 @@ var Body = require('./Body');
|
|||
constraints: [],
|
||||
composites: [],
|
||||
label: 'Composite',
|
||||
plugin: {}
|
||||
plugin: {},
|
||||
cache: {
|
||||
allBodies: null,
|
||||
allConstraints: null,
|
||||
allComposites: null
|
||||
}
|
||||
}, options);
|
||||
};
|
||||
|
||||
|
@ -47,6 +52,7 @@ var Body = require('./Body');
|
|||
* Sets the composite's `isModified` flag.
|
||||
* If `updateParents` is true, all parents will be set (default: false).
|
||||
* If `updateChildren` is true, all children will be set (default: false).
|
||||
* @private
|
||||
* @method setModified
|
||||
* @param {composite} composite
|
||||
* @param {boolean} isModified
|
||||
|
@ -56,6 +62,12 @@ var Body = require('./Body');
|
|||
Composite.setModified = function(composite, isModified, updateParents, updateChildren) {
|
||||
composite.isModified = isModified;
|
||||
|
||||
if (isModified) {
|
||||
composite.cache.allBodies = null;
|
||||
composite.cache.allConstraints = null;
|
||||
composite.cache.allComposites = null;
|
||||
}
|
||||
|
||||
if (updateParents && composite.parent) {
|
||||
Composite.setModified(composite.parent, isModified, updateParents, updateChildren);
|
||||
}
|
||||
|
@ -182,7 +194,6 @@ var Body = require('./Body');
|
|||
var position = Common.indexOf(compositeA.composites, compositeB);
|
||||
if (position !== -1) {
|
||||
Composite.removeCompositeAt(compositeA, position);
|
||||
Composite.setModified(compositeA, true, true, false);
|
||||
}
|
||||
|
||||
if (deep) {
|
||||
|
@ -235,7 +246,6 @@ var Body = require('./Body');
|
|||
var position = Common.indexOf(composite.bodies, body);
|
||||
if (position !== -1) {
|
||||
Composite.removeBodyAt(composite, position);
|
||||
Composite.setModified(composite, true, true, false);
|
||||
}
|
||||
|
||||
if (deep) {
|
||||
|
@ -336,6 +346,7 @@ var Body = require('./Body');
|
|||
|
||||
composite.constraints.length = 0;
|
||||
composite.composites.length = 0;
|
||||
|
||||
Composite.setModified(composite, true, true, false);
|
||||
|
||||
return composite;
|
||||
|
@ -348,11 +359,17 @@ var Body = require('./Body');
|
|||
* @return {body[]} All the bodies
|
||||
*/
|
||||
Composite.allBodies = function(composite) {
|
||||
if (composite.cache.allBodies) {
|
||||
return composite.cache.allBodies;
|
||||
}
|
||||
|
||||
var bodies = [].concat(composite.bodies);
|
||||
|
||||
for (var i = 0; i < composite.composites.length; i++)
|
||||
bodies = bodies.concat(Composite.allBodies(composite.composites[i]));
|
||||
|
||||
composite.cache.allBodies = bodies;
|
||||
|
||||
return bodies;
|
||||
};
|
||||
|
||||
|
@ -363,11 +380,17 @@ var Body = require('./Body');
|
|||
* @return {constraint[]} All the constraints
|
||||
*/
|
||||
Composite.allConstraints = function(composite) {
|
||||
if (composite.cache.allConstraints) {
|
||||
return composite.cache.allConstraints;
|
||||
}
|
||||
|
||||
var constraints = [].concat(composite.constraints);
|
||||
|
||||
for (var i = 0; i < composite.composites.length; i++)
|
||||
constraints = constraints.concat(Composite.allConstraints(composite.composites[i]));
|
||||
|
||||
composite.cache.allConstraints = constraints;
|
||||
|
||||
return constraints;
|
||||
};
|
||||
|
||||
|
@ -378,11 +401,17 @@ var Body = require('./Body');
|
|||
* @return {composite[]} All the composites
|
||||
*/
|
||||
Composite.allComposites = function(composite) {
|
||||
if (composite.cache.allComposites) {
|
||||
return composite.cache.allComposites;
|
||||
}
|
||||
|
||||
var composites = [].concat(composite.composites);
|
||||
|
||||
for (var i = 0; i < composite.composites.length; i++)
|
||||
composites = composites.concat(Composite.allComposites(composite.composites[i]));
|
||||
|
||||
composite.cache.allComposites = composites;
|
||||
|
||||
return composites;
|
||||
};
|
||||
|
||||
|
@ -449,8 +478,6 @@ var Body = require('./Body');
|
|||
objects[i].id = Common.nextId();
|
||||
}
|
||||
|
||||
Composite.setModified(composite, true, true, false);
|
||||
|
||||
return composite;
|
||||
};
|
||||
|
||||
|
@ -469,8 +496,6 @@ var Body = require('./Body');
|
|||
Body.translate(bodies[i], translation);
|
||||
}
|
||||
|
||||
Composite.setModified(composite, true, true, false);
|
||||
|
||||
return composite;
|
||||
};
|
||||
|
||||
|
@ -500,8 +525,6 @@ var Body = require('./Body');
|
|||
Body.rotate(body, rotation);
|
||||
}
|
||||
|
||||
Composite.setModified(composite, true, true, false);
|
||||
|
||||
return composite;
|
||||
};
|
||||
|
||||
|
@ -530,8 +553,6 @@ var Body = require('./Body');
|
|||
Body.scale(body, scaleX, scaleY);
|
||||
}
|
||||
|
||||
Composite.setModified(composite, true, true, false);
|
||||
|
||||
return composite;
|
||||
};
|
||||
|
||||
|
@ -631,8 +652,7 @@ var Body = require('./Body');
|
|||
|
||||
/**
|
||||
* A flag that specifies whether the composite has been modified during the current step.
|
||||
* Most `Matter.Composite` methods will automatically set this flag to `true` to inform the engine of changes to be handled.
|
||||
* If you need to change it manually, you should use the `Composite.setModified` method.
|
||||
* This is automatically managed when bodies, constraints or composites are added or removed.
|
||||
*
|
||||
* @property isModified
|
||||
* @type boolean
|
||||
|
@ -684,4 +704,13 @@ var Body = require('./Body');
|
|||
* @type {}
|
||||
*/
|
||||
|
||||
/**
|
||||
* An object used for storing cached results for performance reasons.
|
||||
* This is used internally only and is automatically managed.
|
||||
*
|
||||
* @private
|
||||
* @property cache
|
||||
* @type {}
|
||||
*/
|
||||
|
||||
})();
|
||||
|
|
Loading…
Reference in a new issue