Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some changes regarding performance / memory #66

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions src/easystar.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ EasyStar.js = function() {
if (directionalConditions[y] === undefined) {
directionalConditions[y] = {};
}
directionalConditions[y][x] = allowedDirections;
directionalConditions[y][x] = allowedDirections
.map(function(c){return c|0}) // force integer
.reduce(function(p,c){return p|c},0);
};

/**
Expand Down Expand Up @@ -468,23 +470,14 @@ EasyStar.js = function() {
// Helpers
var isTileWalkable = function(collisionGrid, acceptableTiles, x, y, sourceNode) {
var directionalCondition = directionalConditions[y] && directionalConditions[y][x];
if (directionalCondition) {
if (directionalCondition !== undefined) {
var direction = calculateDirection(sourceNode.x - x, sourceNode.y - y)
var directionIncluded = function () {
for (var i = 0; i < directionalCondition.length; i++) {
if (directionalCondition[i] === direction) return true
}
return false
}
if (!directionIncluded()) return false
}
for (var i = 0; i < acceptableTiles.length; i++) {
if (collisionGrid[y][x] === acceptableTiles[i]) {
return true;
}
return (direction&directionalCondition) > 0;
}

return false;
return acceptableTiles.indexOf(collisionGrid[y][x]) != -1

//return true;
};

/**
Expand Down Expand Up @@ -546,11 +539,11 @@ EasyStar.js = function() {
};
}

EasyStar.TOP = 'TOP'
EasyStar.TOP_RIGHT = 'TOP_RIGHT'
EasyStar.RIGHT = 'RIGHT'
EasyStar.BOTTOM_RIGHT = 'BOTTOM_RIGHT'
EasyStar.BOTTOM = 'BOTTOM'
EasyStar.BOTTOM_LEFT = 'BOTTOM_LEFT'
EasyStar.LEFT = 'LEFT'
EasyStar.TOP_LEFT = 'TOP_LEFT'
EasyStar.TOP = 1
EasyStar.TOP_RIGHT = 2
EasyStar.RIGHT = 4
EasyStar.BOTTOM_RIGHT = 8
EasyStar.BOTTOM = 16
EasyStar.BOTTOM_LEFT = 32
EasyStar.LEFT = 64
EasyStar.TOP_LEFT = 128
18 changes: 10 additions & 8 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
* @param {Number} costSoFar How far this node is in moves*cost from the start.
* @param {Number} simpleDistanceToTarget Manhatten distance to the end point.
**/
module.exports = function(parent, x, y, costSoFar, simpleDistanceToTarget) {
function Node(parent, x, y, costSoFar, simpleDistanceToTarget) {
this.parent = parent;
this.x = x;
this.y = y;
this.costSoFar = costSoFar;
this.simpleDistanceToTarget = simpleDistanceToTarget;
};

/**
* @return {Number} Best guess distance of a cost using this node.
**/
this.bestGuessDistance = function() {
return this.costSoFar + this.simpleDistanceToTarget;
}
};
/**
* @return {Number} Best guess distance of a cost using this node.
**/
Node.prototype.bestGuessDistance = function(){
return this.costSoFar + this.simpleDistanceToTarget;
}

module.exports = Node;