Skip to content

Commit

Permalink
Update readme, make branch PR ready, fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelle Jacobs authored and Pelle Jacobs committed Sep 10, 2016
1 parent 5baac35 commit 4de593a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 98 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ easystar.enableSync();
easystar.setDirectionalCondition(x, y, [EasyStar.TOP, EasyStar.BOTTOM]); // only accessible from the top and left
```

For more control over a specific condition, on a tile, use the `customCondition`

```javascript
easystar.setCustomCondition(x, y, function(sourceNode, thisNode, grid) {
return grid[sourceNode.y][sourceNode.x] === 2 // only allow '2' nodes to access the node a [x, y]
})

easystar.removeAllCustomConditions() // unset all custom conditions
```

## Usage

First create EasyStar.
Expand Down
102 changes: 13 additions & 89 deletions bin/easystar-0.3.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var EasyStar =
* EasyStar.js
* github.com/prettymuchbryce/EasyStarJS
* Licensed under the MIT license.
*
*
* Implementation By Bryce Neal (@prettymuchbryce)
**/

Expand All @@ -71,8 +71,6 @@ var EasyStar =
var collisionGrid;
var costMap = {};
var pointsToCost = {};
var directionalConditions = {};
var customConditions = {};
var allowCornerCutting = true;
var iterationsSoFar;
var instances = [];
Expand All @@ -82,8 +80,8 @@ var EasyStar =

/**
* Sets the collision grid that EasyStar uses.
*
* @param {Array|Number} tiles An array of numbers that represent
*
* @param {Array|Number} tiles An array of numbers that represent
* which tiles in your grid should be considered
* acceptable, or "walkable".
**/
Expand Down Expand Up @@ -128,8 +126,8 @@ var EasyStar =

/**
* Sets the collision grid that EasyStar uses.
*
* @param {Array} grid The collision grid that this EasyStar instance will read from.
*
* @param {Array} grid The collision grid that this EasyStar instance will read from.
* This should be a 2D Array of Numbers.
**/
this.setGrid = function (grid) {
Expand Down Expand Up @@ -185,57 +183,19 @@ var EasyStar =
};

/**
* Sets a directional condition on a tile
*
* @param {Number} x The x value of the point.
* @param {Number} y The y value of the point.
* @param {Array.<String>} allowedDirections A list of all the allowed directions that can access
* the tile.
**/
this.setDirectionalCondition = function (x, y, allowedDirections) {
directionalConditions[x + '_' + y] = allowedDirections;
};

/**
* Remove all directional conditions
**/
this.removeAllDirectionalConditions = function () {
directionalConditions = {};
};

/**
* Sets a custom condition on a tile
*
* @param {Number} x The x value of the point.
* @param {Number} y The y value of the point.
* @param {Function} customCheck Function that takes the sourceNode and the grid as an argument
* and returns a boolean.
**/
this.setCustomCondition = function (x, y, customCheck) {
customConditions[x + '_' + y] = customCheck;
};

/**
* Remove all custom conditions
**/
this.removeAllCustomConditions = function () {
customConditions = {};
};

/**
* Sets the number of search iterations per calculation.
* A lower number provides a slower result, but more practical if you
* Sets the number of search iterations per calculation.
* A lower number provides a slower result, but more practical if you
* have a large tile-map and don't want to block your thread while
* finding a path.
*
*
* @param {Number} iterations The number of searches to prefrom per calculate() call.
**/
this.setIterationsPerCalculation = function (iterations) {
iterationsPerCalculation = iterations;
};

/**
* Avoid a particular point on the grid,
* Avoid a particular point on the grid,
* regardless of whether or not it is an acceptable tile.
*
* @param {Number} x The x value of the point to avoid.
Expand Down Expand Up @@ -278,14 +238,14 @@ var EasyStar =

/**
* Find a path.
*
*
* @param {Number} startX The X position of the starting point.
* @param {Number} startY The Y position of the starting point.
* @param {Number} endX The X position of the ending point.
* @param {Number} endY The Y position of the ending point.
* @param {Function} callback A function that is called when your path
* is found, or no path is found.
*
*
**/
this.findPath = function (startX, startY, endX, endY, callback) {
// Wraps the callback for sync vs async logic
Expand Down Expand Up @@ -476,7 +436,7 @@ var EasyStar =
var adjacentCoordinateX = searchNode.x + x;
var adjacentCoordinateY = searchNode.y + y;

if (pointsToAvoid[adjacentCoordinateX + "_" + adjacentCoordinateY] === undefined && isTileWalkable(collisionGrid, acceptableTiles, adjacentCoordinateX, adjacentCoordinateY, searchNode)) {
if (pointsToAvoid[adjacentCoordinateX + "_" + adjacentCoordinateY] === undefined && isTileWalkable(collisionGrid, acceptableTiles, adjacentCoordinateX, adjacentCoordinateY)) {
var node = coordinateToNode(instance, adjacentCoordinateX, adjacentCoordinateY, searchNode, cost);

if (node.list === undefined) {
Expand All @@ -491,22 +451,7 @@ var EasyStar =
};

// Helpers
var isTileWalkable = function (collisionGrid, acceptableTiles, x, y, sourceNode) {
if (directionalConditions[x + "_" + y]) {
var direction = EasyStar.calculateDirection(sourceNode, { x: x, y: y });
var directionIncluded = function () {
for (var i = 0; i < directionalConditions[x + "_" + y].length; i++) {
if (directionalConditions[x + "_" + y][i] === direction) return true;
}
return false;
};
if (!directionIncluded()) return false;
}

if (customConditions[x + "_" + y] && !customConditions[x + "_" + y](sourceNode, { x: x, y: y }, collisionGrid)) {
return false;
}

var isTileWalkable = function (collisionGrid, acceptableTiles, x, y) {
for (var i = 0; i < acceptableTiles.length; i++) {
if (collisionGrid[y][x] === acceptableTiles[i]) {
return true;
Expand Down Expand Up @@ -554,27 +499,6 @@ var EasyStar =
};
};

/**
* -1, -1 | 0, -1 | 1, -1
* -1, 0 | SOURCE | 1, 0
* -1, 1 | 0, 1 | 1, 1
*/
EasyStar.calculateDirection = function (sourceNode, destinationNode) {
var diffX = sourceNode.x - destinationNode.x;
var diffY = sourceNode.y - destinationNode.y;
if (diffX === 0, diffY === -1) return 'bottom';else if (diffX === 1, diffY === -1) return 'bottom-left';else if (diffX === 1, diffY === 0) return 'left';else if (diffX === 1, diffY === 1) return 'top-left';else if (diffX === 0, diffY === 1) return 'top';else if (diffX === -1, diffY === 1) return 'top-right';else if (diffX === -1, diffY === 0) return 'right';else if (diffX === -1, diffY === -1) return 'bottom-right';
throw new Error('These differences are not valid: ' + diffX + ', ' + diffY);
};

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';

/***/ },
/* 1 */
/***/ function(module, exports) {
Expand Down
16 changes: 8 additions & 8 deletions src/easystar.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,14 +534,14 @@ EasyStar.js = function() {
EasyStar.calculateDirection = function (sourceNode, destinationNode) {
var diffX = sourceNode.x - destinationNode.x
var diffY = sourceNode.y - destinationNode.y
if (diffX === 0, diffY === -1) return 'bottom'
else if (diffX === 1, diffY === -1) return 'bottom-left'
else if (diffX === 1, diffY === 0) return 'left'
else if (diffX === 1, diffY === 1) return 'top-left'
else if (diffX === 0, diffY === 1) return 'top'
else if (diffX === -1, diffY === 1) return 'top-right'
else if (diffX === -1, diffY === 0) return 'right'
else if (diffX === -1, diffY === -1) return 'bottom-right'
if (diffX === 0, diffY === -1) return EasyStar.BOTTOM
else if (diffX === 1, diffY === -1) return EasyStar.BOTTOM_LEFT
else if (diffX === 1, diffY === 0) return EasyStar.LEFT
else if (diffX === 1, diffY === 1) return EasyStar.TOP_LEFT
else if (diffX === 0, diffY === 1) return EasyStar.TOP
else if (diffX === -1, diffY === 1) return EasyStar.TOP_RIGHT
else if (diffX === -1, diffY === 0) return EasyStar.RIGHT
else if (diffX === -1, diffY === -1) return EasyStar.BOTTOM_RIGHT
throw new Error('These differences are not valid: ' + diffX + ', ' + diffY)
};

Expand Down
2 changes: 1 addition & 1 deletion test/easystartest.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ describe("EasyStar.js", function() {
return grid[source.y][source.x] === 4
})
easyStar.setCustomCondition(1, 2, function (source, thisNode, grid) {
return EasyStar.calculateDirection(source, thisNode) === 'left' && grid[source.y][source.x] === 4
return EasyStar.calculateDirection(source, thisNode) === 'LEFT' && grid[source.y][source.x] === 4
})

easyStar.findPath(0, 0, 2, 0, function (path) {
Expand Down

0 comments on commit 4de593a

Please sign in to comment.