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

optimised Matter.Composite

This commit is contained in:
liabru 2021-12-05 19:11:29 +00:00
parent d8a6380899
commit 52e797791b

View file

@ -39,7 +39,12 @@ var Body = require('./Body');
constraints: [], constraints: [],
composites: [], composites: [],
label: 'Composite', label: 'Composite',
plugin: {} plugin: {},
cache: {
allBodies: null,
allConstraints: null,
allComposites: null
}
}, options); }, options);
}; };
@ -47,6 +52,7 @@ var Body = require('./Body');
* Sets the composite's `isModified` flag. * Sets the composite's `isModified` flag.
* If `updateParents` is true, all parents will be set (default: false). * If `updateParents` is true, all parents will be set (default: false).
* If `updateChildren` is true, all children will be set (default: false). * If `updateChildren` is true, all children will be set (default: false).
* @private
* @method setModified * @method setModified
* @param {composite} composite * @param {composite} composite
* @param {boolean} isModified * @param {boolean} isModified
@ -56,6 +62,12 @@ var Body = require('./Body');
Composite.setModified = function(composite, isModified, updateParents, updateChildren) { Composite.setModified = function(composite, isModified, updateParents, updateChildren) {
composite.isModified = isModified; composite.isModified = isModified;
if (isModified) {
composite.cache.allBodies = null;
composite.cache.allConstraints = null;
composite.cache.allComposites = null;
}
if (updateParents && composite.parent) { if (updateParents && composite.parent) {
Composite.setModified(composite.parent, isModified, updateParents, updateChildren); Composite.setModified(composite.parent, isModified, updateParents, updateChildren);
} }
@ -182,7 +194,6 @@ var Body = require('./Body');
var position = Common.indexOf(compositeA.composites, compositeB); var position = Common.indexOf(compositeA.composites, compositeB);
if (position !== -1) { if (position !== -1) {
Composite.removeCompositeAt(compositeA, position); Composite.removeCompositeAt(compositeA, position);
Composite.setModified(compositeA, true, true, false);
} }
if (deep) { if (deep) {
@ -235,7 +246,6 @@ var Body = require('./Body');
var position = Common.indexOf(composite.bodies, body); var position = Common.indexOf(composite.bodies, body);
if (position !== -1) { if (position !== -1) {
Composite.removeBodyAt(composite, position); Composite.removeBodyAt(composite, position);
Composite.setModified(composite, true, true, false);
} }
if (deep) { if (deep) {
@ -336,6 +346,7 @@ var Body = require('./Body');
composite.constraints.length = 0; composite.constraints.length = 0;
composite.composites.length = 0; composite.composites.length = 0;
Composite.setModified(composite, true, true, false); Composite.setModified(composite, true, true, false);
return composite; return composite;
@ -348,11 +359,17 @@ var Body = require('./Body');
* @return {body[]} All the bodies * @return {body[]} All the bodies
*/ */
Composite.allBodies = function(composite) { Composite.allBodies = function(composite) {
if (composite.cache.allBodies) {
return composite.cache.allBodies;
}
var bodies = [].concat(composite.bodies); var bodies = [].concat(composite.bodies);
for (var i = 0; i < composite.composites.length; i++) for (var i = 0; i < composite.composites.length; i++)
bodies = bodies.concat(Composite.allBodies(composite.composites[i])); bodies = bodies.concat(Composite.allBodies(composite.composites[i]));
composite.cache.allBodies = bodies;
return bodies; return bodies;
}; };
@ -363,11 +380,17 @@ var Body = require('./Body');
* @return {constraint[]} All the constraints * @return {constraint[]} All the constraints
*/ */
Composite.allConstraints = function(composite) { Composite.allConstraints = function(composite) {
if (composite.cache.allConstraints) {
return composite.cache.allConstraints;
}
var constraints = [].concat(composite.constraints); var constraints = [].concat(composite.constraints);
for (var i = 0; i < composite.composites.length; i++) for (var i = 0; i < composite.composites.length; i++)
constraints = constraints.concat(Composite.allConstraints(composite.composites[i])); constraints = constraints.concat(Composite.allConstraints(composite.composites[i]));
composite.cache.allConstraints = constraints;
return constraints; return constraints;
}; };
@ -378,11 +401,17 @@ var Body = require('./Body');
* @return {composite[]} All the composites * @return {composite[]} All the composites
*/ */
Composite.allComposites = function(composite) { Composite.allComposites = function(composite) {
if (composite.cache.allComposites) {
return composite.cache.allComposites;
}
var composites = [].concat(composite.composites); var composites = [].concat(composite.composites);
for (var i = 0; i < composite.composites.length; i++) for (var i = 0; i < composite.composites.length; i++)
composites = composites.concat(Composite.allComposites(composite.composites[i])); composites = composites.concat(Composite.allComposites(composite.composites[i]));
composite.cache.allComposites = composites;
return composites; return composites;
}; };
@ -449,8 +478,6 @@ var Body = require('./Body');
objects[i].id = Common.nextId(); objects[i].id = Common.nextId();
} }
Composite.setModified(composite, true, true, false);
return composite; return composite;
}; };
@ -469,8 +496,6 @@ var Body = require('./Body');
Body.translate(bodies[i], translation); Body.translate(bodies[i], translation);
} }
Composite.setModified(composite, true, true, false);
return composite; return composite;
}; };
@ -500,8 +525,6 @@ var Body = require('./Body');
Body.rotate(body, rotation); Body.rotate(body, rotation);
} }
Composite.setModified(composite, true, true, false);
return composite; return composite;
}; };
@ -530,8 +553,6 @@ var Body = require('./Body');
Body.scale(body, scaleX, scaleY); Body.scale(body, scaleX, scaleY);
} }
Composite.setModified(composite, true, true, false);
return composite; return composite;
}; };
@ -631,8 +652,7 @@ var Body = require('./Body');
/** /**
* A flag that specifies whether the composite has been modified during the current step. * 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. * This is automatically managed when bodies, constraints or composites are added or removed.
* If you need to change it manually, you should use the `Composite.setModified` method.
* *
* @property isModified * @property isModified
* @type boolean * @type boolean
@ -684,4 +704,13 @@ var Body = require('./Body');
* @type {} * @type {}
*/ */
/**
* An object used for storing cached results for performance reasons.
* This is used internally only and is automatically managed.
*
* @private
* @property cache
* @type {}
*/
})(); })();