Skip to content
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
23 changes: 12 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ export class js {
*/
setAcceptableTiles(tiles: number[] | number): void

/**
* Enables sync mode for this EasyStar instance..
* if you're into that sort of thing.
*/
enableSync(): void

/**
* Disables sync mode for this EasyStar instance.
*/
disableSync(): void

/**
* Enable diagonal pathfinding.
*/
Expand Down Expand Up @@ -122,6 +111,18 @@ export class js {
*/
stopAvoidingAllAdditionalPoints(): void

/**
* 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.
* @return {Array} The path or null
*
*/
findPathSync(startX: number, startY: number, endX: number, endY: number): ({ x: number, y: number }[]) | null

/**
* Find a path.
*
Expand Down
59 changes: 28 additions & 31 deletions src/easystar.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var nextInstanceId = 1;
EasyStar.js = function() {
var STRAIGHT_COST = 1.0;
var DIAGONAL_COST = 1.4;
var syncEnabled = false;
var pointsToAvoid = {};
var collisionGrid;
var costMap = {};
Expand Down Expand Up @@ -52,21 +51,6 @@ EasyStar.js = function() {
}
};

/**
* Enables sync mode for this EasyStar instance..
* if you're into that sort of thing.
**/
this.enableSync = function() {
syncEnabled = true;
};

/**
* Disables sync mode for this EasyStar instance.
**/
this.disableSync = function() {
syncEnabled = false;
};

/**
* Enable diagonal pathfinding.
*/
Expand Down Expand Up @@ -225,6 +209,27 @@ EasyStar.js = function() {
pointsToAvoid = {};
};


/**
* 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.
* @return {Array} The path
*
**/
this.findPathSync = function(startX, startY, endX, endY) {

let val;
this.findPath(startX, startY, endX, endY, function(result){
val = result;
})
this.calculate(true);
return val;
}

/**
* Find a path.
*
Expand All @@ -238,16 +243,6 @@ EasyStar.js = function() {
*
**/
this.findPath = function(startX, startY, endX, endY, callback) {
// Wraps the callback for sync vs async logic
var callbackWrapper = function(result) {
if (syncEnabled) {
callback(result);
} else {
setTimeout(function() {
callback(result);
});
}
}

// No acceptable tiles were set
if (acceptableTiles === undefined) {
Expand All @@ -267,7 +262,7 @@ EasyStar.js = function() {

// Start and end are the same tile.
if (startX===endX && startY===endY) {
callbackWrapper([]);
callback([]);
return;
}

Expand All @@ -282,7 +277,7 @@ EasyStar.js = function() {
}

if (isAcceptable === false) {
callbackWrapper(null);
callback(null);
return;
}

Expand All @@ -297,15 +292,17 @@ EasyStar.js = function() {
instance.startY = startY;
instance.endX = endX;
instance.endY = endY;
instance.callback = callbackWrapper;
instance.callback = callback;

instance.openList.push(coordinateToNode(instance, instance.startX,
instance.startY, null, STRAIGHT_COST));

var instanceId = nextInstanceId ++;
instances[instanceId] = instance;
instanceQueue.push(instanceId);
return instanceId;

return instanceId

};

/**
Expand All @@ -330,7 +327,7 @@ EasyStar.js = function() {
* You can change the number of calculations done in a call by using
* easystar.setIteratonsPerCalculation().
**/
this.calculate = function() {
this.calculate = function(syncEnabled) {
if (instanceQueue.length === 0 || collisionGrid === undefined || acceptableTiles === undefined) {
return;
}
Expand Down