Skip to content

Commit

Permalink
moved all private functions to module namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Nov 26, 2017
1 parent 2dc7a0b commit 64be5a5
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ var Axes = require('../geometry/Axes');
}

// sum the properties of all compound parts of the parent body
var total = _totalProperties(body);
var total = Body._totalProperties(body);

body.area = total.area;
body.parent = body;
Expand Down Expand Up @@ -658,7 +658,7 @@ var Axes = require('../geometry/Axes');
* @param {body} body
* @return {}
*/
var _totalProperties = function(body) {
Body._totalProperties = function(body) {
// from equations at:
// https://ecourses.ou.edu/cgi-bin/ebook.cgi?doc=&topic=st&chap_sec=07.2&page=theory
// http://output.to/sideway/default.asp?qno=121100087
Expand Down
34 changes: 17 additions & 17 deletions src/collision/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var Common = require('../core/Common');
|| body.bounds.max.y < world.bounds.min.y || body.bounds.min.y > world.bounds.max.y)
continue;

var newRegion = _getRegion(grid, body);
var newRegion = Grid._getRegion(grid, body);

// if the body has changed grid region
if (!body.region || newRegion.id !== body.region.id || forceUpdate) {
Expand All @@ -94,13 +94,13 @@ var Common = require('../core/Common');
if (!body.region || forceUpdate)
body.region = newRegion;

var union = _regionUnion(newRegion, body.region);
var union = Grid._regionUnion(newRegion, body.region);

// update grid buckets affected by region change
// iterate over the union of both regions
for (col = union.startCol; col <= union.endCol; col++) {
for (row = union.startRow; row <= union.endRow; row++) {
bucketId = _getBucketId(col, row);
bucketId = Grid._getBucketId(col, row);
bucket = buckets[bucketId];

var isInsideNewRegion = (col >= newRegion.startCol && col <= newRegion.endCol
Expand All @@ -113,15 +113,15 @@ var Common = require('../core/Common');
if (!isInsideNewRegion && isInsideOldRegion) {
if (isInsideOldRegion) {
if (bucket)
_bucketRemoveBody(grid, bucket, body);
Grid._bucketRemoveBody(grid, bucket, body);
}
}

// add to new region buckets
if (body.region === newRegion || (isInsideNewRegion && !isInsideOldRegion) || forceUpdate) {
if (!bucket)
bucket = _createBucket(buckets, bucketId);
_bucketAddBody(grid, bucket, body);
bucket = Grid._createBucket(buckets, bucketId);
Grid._bucketAddBody(grid, bucket, body);
}
}
}
Expand All @@ -136,7 +136,7 @@ var Common = require('../core/Common');

// update pairs list only if pairs changed (i.e. a body changed region)
if (gridChanged)
grid.pairsList = _createActivePairsList(grid);
grid.pairsList = Grid._createActivePairsList(grid);
};

/**
Expand All @@ -158,13 +158,13 @@ var Common = require('../core/Common');
* @param {} regionB
* @return {} region
*/
var _regionUnion = function(regionA, regionB) {
Grid._regionUnion = function(regionA, regionB) {
var startCol = Math.min(regionA.startCol, regionB.startCol),
endCol = Math.max(regionA.endCol, regionB.endCol),
startRow = Math.min(regionA.startRow, regionB.startRow),
endRow = Math.max(regionA.endRow, regionB.endRow);

return _createRegion(startCol, endCol, startRow, endRow);
return Grid._createRegion(startCol, endCol, startRow, endRow);
};

/**
Expand All @@ -175,14 +175,14 @@ var Common = require('../core/Common');
* @param {} body
* @return {} region
*/
var _getRegion = function(grid, body) {
Grid._getRegion = function(grid, body) {
var bounds = body.bounds,
startCol = Math.floor(bounds.min.x / grid.bucketWidth),
endCol = Math.floor(bounds.max.x / grid.bucketWidth),
startRow = Math.floor(bounds.min.y / grid.bucketHeight),
endRow = Math.floor(bounds.max.y / grid.bucketHeight);

return _createRegion(startCol, endCol, startRow, endRow);
return Grid._createRegion(startCol, endCol, startRow, endRow);
};

/**
Expand All @@ -195,7 +195,7 @@ var Common = require('../core/Common');
* @param {} endRow
* @return {} region
*/
var _createRegion = function(startCol, endCol, startRow, endRow) {
Grid._createRegion = function(startCol, endCol, startRow, endRow) {
return {
id: startCol + ',' + endCol + ',' + startRow + ',' + endRow,
startCol: startCol,
Expand All @@ -213,7 +213,7 @@ var Common = require('../core/Common');
* @param {} row
* @return {string} bucket id
*/
var _getBucketId = function(column, row) {
Grid._getBucketId = function(column, row) {
return 'C' + column + 'R' + row;
};

Expand All @@ -225,7 +225,7 @@ var Common = require('../core/Common');
* @param {} bucketId
* @return {} bucket
*/
var _createBucket = function(buckets, bucketId) {
Grid._createBucket = function(buckets, bucketId) {
var bucket = buckets[bucketId] = [];
return bucket;
};
Expand All @@ -238,7 +238,7 @@ var Common = require('../core/Common');
* @param {} bucket
* @param {} body
*/
var _bucketAddBody = function(grid, bucket, body) {
Grid._bucketAddBody = function(grid, bucket, body) {
// add new pairs
for (var i = 0; i < bucket.length; i++) {
var bodyB = bucket[i];
Expand Down Expand Up @@ -270,7 +270,7 @@ var Common = require('../core/Common');
* @param {} bucket
* @param {} body
*/
var _bucketRemoveBody = function(grid, bucket, body) {
Grid._bucketRemoveBody = function(grid, bucket, body) {
// remove from bucket
bucket.splice(Common.indexOf(bucket, body), 1);

Expand All @@ -294,7 +294,7 @@ var Common = require('../core/Common');
* @param {} grid
* @return [] pairs
*/
var _createActivePairsList = function(grid) {
Grid._createActivePairsList = function(grid) {
var pairKeys,
pair,
pairs = [];
Expand Down
4 changes: 2 additions & 2 deletions src/collision/Pairs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var Common = require('../core/Common');

(function() {

var _pairMaxIdleLife = 1000;
Pairs._pairMaxIdleLife = 1000;

/**
* Creates a new pairs structure.
Expand Down Expand Up @@ -124,7 +124,7 @@ var Common = require('../core/Common');
}

// if pair is inactive for too long, mark it to be removed
if (timestamp - pair.timeUpdated > _pairMaxIdleLife) {
if (timestamp - pair.timeUpdated > Pairs._pairMaxIdleLife) {
indexesToRemove.push(i);
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/collision/SAT.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var Vector = require('../geometry/Vector');
axisBodyB = axisBodyA === bodyA ? bodyB : bodyA,
axes = [axisBodyA.axes[previousCollision.axisNumber]];

minOverlap = _overlapAxes(axisBodyA.vertices, axisBodyB.vertices, axes);
minOverlap = SAT._overlapAxes(axisBodyA.vertices, axisBodyB.vertices, axes);
collision.reused = true;

if (minOverlap.overlap <= 0) {
Expand All @@ -64,14 +64,14 @@ var Vector = require('../geometry/Vector');
} else {
// if we can't reuse a result, perform a full SAT test

overlapAB = _overlapAxes(bodyA.vertices, bodyB.vertices, bodyA.axes);
overlapAB = SAT._overlapAxes(bodyA.vertices, bodyB.vertices, bodyA.axes);

if (overlapAB.overlap <= 0) {
collision.collided = false;
return collision;
}

overlapBA = _overlapAxes(bodyB.vertices, bodyA.vertices, bodyB.axes);
overlapBA = SAT._overlapAxes(bodyB.vertices, bodyA.vertices, bodyB.axes);

if (overlapBA.overlap <= 0) {
collision.collided = false;
Expand Down Expand Up @@ -120,7 +120,7 @@ var Vector = require('../geometry/Vector');
collision.penetration.y = collision.normal.y * collision.depth;

// find support points, there is always either exactly one or two
var verticesB = _findSupports(bodyA, bodyB, collision.normal),
var verticesB = SAT._findSupports(bodyA, bodyB, collision.normal),
supports = [];

// find the supports from bodyB that are inside bodyA
Expand All @@ -132,7 +132,7 @@ var Vector = require('../geometry/Vector');

// find the supports from bodyA that are inside bodyB
if (supports.length < 2) {
var verticesA = _findSupports(bodyB, bodyA, Vector.neg(collision.normal));
var verticesA = SAT._findSupports(bodyB, bodyA, Vector.neg(collision.normal));

if (Vertices.contains(bodyB.vertices, verticesA[0]))
supports.push(verticesA[0]);
Expand All @@ -159,7 +159,7 @@ var Vector = require('../geometry/Vector');
* @param {} axes
* @return result
*/
var _overlapAxes = function(verticesA, verticesB, axes) {
SAT._overlapAxes = function(verticesA, verticesB, axes) {
var projectionA = Vector._temp[0],
projectionB = Vector._temp[1],
result = { overlap: Number.MAX_VALUE },
Expand All @@ -169,8 +169,8 @@ var Vector = require('../geometry/Vector');
for (var i = 0; i < axes.length; i++) {
axis = axes[i];

_projectToAxis(projectionA, verticesA, axis);
_projectToAxis(projectionB, verticesB, axis);
SAT._projectToAxis(projectionA, verticesA, axis);
SAT._projectToAxis(projectionB, verticesB, axis);

overlap = Math.min(projectionA.max - projectionB.min, projectionB.max - projectionA.min);

Expand All @@ -197,7 +197,7 @@ var Vector = require('../geometry/Vector');
* @param {} vertices
* @param {} axis
*/
var _projectToAxis = function(projection, vertices, axis) {
SAT._projectToAxis = function(projection, vertices, axis) {
var min = Vector.dot(vertices[0], axis),
max = min;

Expand All @@ -224,7 +224,7 @@ var Vector = require('../geometry/Vector');
* @param {} normal
* @return [vector]
*/
var _findSupports = function(bodyA, bodyB, normal) {
SAT._findSupports = function(bodyA, bodyB, normal) {
var nearestDistance = Number.MAX_VALUE,
vertexToBody = Vector._temp[0],
vertices = bodyB.vertices,
Expand Down
4 changes: 2 additions & 2 deletions src/constraint/MouseConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var Bounds = require('../geometry/Bounds');
Events.on(engine, 'beforeUpdate', function() {
var allBodies = Composite.allBodies(engine.world);
MouseConstraint.update(mouseConstraint, allBodies);
_triggerEvents(mouseConstraint);
MouseConstraint._triggerEvents(mouseConstraint);
});

return mouseConstraint;
Expand Down Expand Up @@ -136,7 +136,7 @@ var Bounds = require('../geometry/Bounds');
* @private
* @param {mouse} mouseConstraint
*/
var _triggerEvents = function(mouseConstraint) {
MouseConstraint._triggerEvents = function(mouseConstraint) {
var mouse = mouseConstraint.mouse,
mouseEvents = mouse.sourceEvents;

Expand Down
6 changes: 3 additions & 3 deletions src/core/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,14 @@ module.exports = Common;

for (var node in graph) {
if (!visited[node] && !temp[node]) {
_topologicalSort(node, visited, temp, graph, result);
Common._topologicalSort(node, visited, temp, graph, result);
}
}

return result;
};

var _topologicalSort = function(node, visited, temp, graph, result) {
Common._topologicalSort = function(node, visited, temp, graph, result) {
var neighbors = graph[node] || [];
temp[node] = true;

Expand All @@ -444,7 +444,7 @@ module.exports = Common;
}

if (!visited[neighbor]) {
_topologicalSort(neighbor, visited, temp, graph, result);
Common._topologicalSort(neighbor, visited, temp, graph, result);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/Mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var Common = require('../core/Common');
};

mouse.mousemove = function(event) {
var position = _getRelativeMousePosition(event, mouse.element, mouse.pixelRatio),
var position = Mouse._getRelativeMousePosition(event, mouse.element, mouse.pixelRatio),
touches = event.changedTouches;

if (touches) {
Expand All @@ -60,7 +60,7 @@ var Common = require('../core/Common');
};

mouse.mousedown = function(event) {
var position = _getRelativeMousePosition(event, mouse.element, mouse.pixelRatio),
var position = Mouse._getRelativeMousePosition(event, mouse.element, mouse.pixelRatio),
touches = event.changedTouches;

if (touches) {
Expand All @@ -80,7 +80,7 @@ var Common = require('../core/Common');
};

mouse.mouseup = function(event) {
var position = _getRelativeMousePosition(event, mouse.element, mouse.pixelRatio),
var position = Mouse._getRelativeMousePosition(event, mouse.element, mouse.pixelRatio),
touches = event.changedTouches;

if (touches) {
Expand Down Expand Up @@ -176,7 +176,7 @@ var Common = require('../core/Common');
* @param {number} pixelRatio
* @return {}
*/
var _getRelativeMousePosition = function(event, element, pixelRatio) {
Mouse._getRelativeMousePosition = function(event, element, pixelRatio) {
var elementBounds = element.getBoundingClientRect(),
rootNode = (document.documentElement || document.body.parentNode || document.body),
scrollX = (window.pageXOffset !== undefined) ? window.pageXOffset : rootNode.scrollLeft,
Expand Down
4 changes: 2 additions & 2 deletions src/geometry/Svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var Bounds = require('../geometry/Bounds');
};

// ensure path is absolute
_svgPathToAbsolute(path);
Svg._svgPathToAbsolute(path);

// get total length
total = path.getTotalLength();
Expand Down Expand Up @@ -149,7 +149,7 @@ var Bounds = require('../geometry/Bounds');
return points;
};

var _svgPathToAbsolute = function(path) {
Svg._svgPathToAbsolute = function(path) {
// http://phrogz.net/convert-svg-path-to-all-absolute-commands
// Copyright (c) Gavin Kistner
// http://phrogz.net/js/_ReuseLicense.txt
Expand Down

0 comments on commit 64be5a5

Please sign in to comment.