PhaserJS Path Finding plugin, with optional use of web worker configuration. Fast and Easy to use.
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });
function preload() {
}
function create() {
var asyncPath = game.plugins.add(Phaser.Plugin.asyncPath);
var PointA = {x: 13, y: 14}; // works fine with Sprite, Point, or any object with x and y properties
PointB = {x: 22, y: 44};
Block = {
Origin: PointA,
Destination: PointB,
found: function(path){
console.log(path);
},
notfound: function(){
console.log('No path found');
}
}
asyncPath.getPath(Block);
}
asyncPath = game.plugins.add(Phaser.Plugin.asyncPath);
Block = {
Origin: {x:{number}, y:{number}},
Destination: {x:{number}, y:{number}},
keepTrack: {boolean}, Optional
trackBy: {string}, Optional
Diagonals: {boolean}, Optional
debugpath: {boolean}, Optional
Algorithm: {string}, Optional
forcemain: {boolean}, Optional
found: {Function}, Optional
notfound: {Function} Optional
}
Origin: {x:{number}, y:{number}}
Destination: {x:{number}, y:{number}}
keepTrack: {boolean}
The offset is Calculated on tracked blocks and property. If tracked property is greater than or less than offset then new path is calculated from Origin to detination
trackBy: {string} 'Origin' OR 'Destination'
Forces path Manager to set new Diagonal setting for this block
Diagonals: {boolean}
Set the debugging for this block
debugpath: {boolean}
Set the Algorithm for this block
Algorithm: {string} 'Manhattan' OR 'Euclidean'
Forces this block to be solved on the main UI thread even if there is a web worker
forcemain: {boolean}
found
function is fired each time a path is found
found: {Function}
notfound
function is fired each time a path is not found
notfound: {Function}
##Plugin Configurations
Setting the plugin with a map
map = game.add.tilemap('mapJSON');
asyncPath.tileMap = map;
Setting up Default watchers offset values in X and Y directions that would trigger a pathfinding calculation on tracked blocks
asyncPath.xyOffset = {number}
X offset trigger
asyncPath.xOffset = {number}
Y offset trigger
asyncPath.xOffset = {number}
Setting up the returned path configuration default pixel
asyncPath.xOffset = {string} 'units' OR 'pixel'
Setting up the algorithm to use in pathFinding
asyncPath.algorithm = {string} 'Manhattan' OR 'Euclidean'
Setting up the return path configuration (if the return path should point to the center of each tile or the edges) default true
asyncPath.centerPaths = {boolean} true OR false
Setting up the number of paths to be solved on each Phaser frame
asyncPath.pathsPerFrame = {numeber}
Would debug each block except turned off on a block
asyncPath.defaultdebug = {boolean} true OR false
Setting custom Cost for vertical and Horizontal movement Default set to 10
asyncPath.VertHorCost = {number}
Setting Diagonal movement Default set to false
asyncPath.useDiagonal = {boolean} true OR false
Setting Non walkable tiles
asyncPath.nonWalkableTile = {number} OR {[number]}
Setting Path Debuging color
asyncPath.debugColor = {HEX_NUMBER}
Setting non walkable layer
asyncPath.nonWalkableLayer = {string}
#Creating a web worker for faster path calculations
A good choice will be using a web Worker only if you have too many path
finding calculations to be done instantly, else setting timers for number of calculations to be done in each Phaser Display frame would be much more efficient. The
newWorker()
method returns a new webworker instance. Webworkers are managed internally by the plugin;
If there are no web workers, all path finding calculations run on the main UI thread. The returned webworker instance is managed internally by the plugin
asyncPath.newWorker();
Sets Algorithm for webworker
asyncPath.webWorkerAlgorithm = {string} 'Manhattan' OR 'Euclidean'
Set the use of Diagonals in webWorker
asyncPath.webWorkerDiagonals = {boolean} true OR false
Set the cost of Vertical and Horizontal movement in webWorker
asyncPath.webWorkerVerHorCost = {number}
Set the cost of Diagonal movement in webWorker
asyncPath.webWorkerDiagonalsCost = {number}