From df2f217d2f70ec398f6a14b53f47b3452c06960c Mon Sep 17 00:00:00 2001 From: PSeitz Date: Sat, 7 Oct 2017 22:34:45 +0200 Subject: [PATCH 1/4] return result on sync --- src/easystar.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/easystar.js b/src/easystar.js index d64627a..d269cb6 100755 --- a/src/easystar.js +++ b/src/easystar.js @@ -238,9 +238,11 @@ EasyStar.js = function() { * **/ this.findPath = function(startX, startY, endX, endY, callback) { + let val; // Wraps the callback for sync vs async logic var callbackWrapper = function(result) { if (syncEnabled) { + val = result; callback(result); } else { setTimeout(function() { @@ -305,7 +307,12 @@ EasyStar.js = function() { var instanceId = nextInstanceId ++; instances[instanceId] = instance; instanceQueue.push(instanceId); - return instanceId; + if (syncEnabled) { + this.calculate() + return val + }else{ + return instanceId + } }; /** From 2b328c93f481cad9a0f9baf9c648205f69915391 Mon Sep 17 00:00:00 2001 From: PSeitz Date: Sat, 7 Oct 2017 22:35:09 +0200 Subject: [PATCH 2/4] optional callback --- src/easystar.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/easystar.js b/src/easystar.js index d269cb6..7e49761 100755 --- a/src/easystar.js +++ b/src/easystar.js @@ -243,7 +243,7 @@ EasyStar.js = function() { var callbackWrapper = function(result) { if (syncEnabled) { val = result; - callback(result); + if(callback) callback(result); } else { setTimeout(function() { callback(result); From 20017fd880f1880a57621c2c74b5c29ccc46615d Mon Sep 17 00:00:00 2001 From: PSeitz Date: Sat, 7 Oct 2017 22:35:31 +0200 Subject: [PATCH 3/4] update definition --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index a31b48e..50a5d00 100644 --- a/index.d.ts +++ b/index.d.ts @@ -134,7 +134,7 @@ export class js { * @return {Number} A numeric, non-zero value which identifies the created instance. This value can be passed to cancelPath to cancel the path calculation. * */ - findPath(startX: number, startY: number, endX: number, endY: number, callback: (path: { x: number, y: number }[]) => void): number + findPath(startX: number, startY: number, endX: number, endY: number, callback?: (path: { x: number, y: number }[]) => void): number /** * Cancel a path calculation. From dde38f8d777c6b50c6de81e974bb8bde56cd09cd Mon Sep 17 00:00:00 2001 From: Pascal Seitz Date: Thu, 12 Oct 2017 15:32:48 +0200 Subject: [PATCH 4/4] switch to findPathSync --- index.d.ts | 25 ++++++++++--------- src/easystar.js | 66 +++++++++++++++++++++---------------------------- 2 files changed, 41 insertions(+), 50 deletions(-) diff --git a/index.d.ts b/index.d.ts index 50a5d00..55aa4a0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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. */ @@ -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. * @@ -134,7 +135,7 @@ export class js { * @return {Number} A numeric, non-zero value which identifies the created instance. This value can be passed to cancelPath to cancel the path calculation. * */ - findPath(startX: number, startY: number, endX: number, endY: number, callback?: (path: { x: number, y: number }[]) => void): number + findPath(startX: number, startY: number, endX: number, endY: number, callback: (path: { x: number, y: number }[]) => void): number /** * Cancel a path calculation. diff --git a/src/easystar.js b/src/easystar.js index 7e49761..27f3875 100755 --- a/src/easystar.js +++ b/src/easystar.js @@ -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 = {}; @@ -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. */ @@ -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. * @@ -238,18 +243,6 @@ EasyStar.js = function() { * **/ this.findPath = function(startX, startY, endX, endY, callback) { - let val; - // Wraps the callback for sync vs async logic - var callbackWrapper = function(result) { - if (syncEnabled) { - val = result; - if(callback) callback(result); - } else { - setTimeout(function() { - callback(result); - }); - } - } // No acceptable tiles were set if (acceptableTiles === undefined) { @@ -269,7 +262,7 @@ EasyStar.js = function() { // Start and end are the same tile. if (startX===endX && startY===endY) { - callbackWrapper([]); + callback([]); return; } @@ -284,7 +277,7 @@ EasyStar.js = function() { } if (isAcceptable === false) { - callbackWrapper(null); + callback(null); return; } @@ -299,7 +292,7 @@ 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)); @@ -307,12 +300,9 @@ EasyStar.js = function() { var instanceId = nextInstanceId ++; instances[instanceId] = instance; instanceQueue.push(instanceId); - if (syncEnabled) { - this.calculate() - return val - }else{ - return instanceId - } + + return instanceId + }; /** @@ -337,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; }