Path finder
Introduction¶
Find moveable area or moving path, chess behavior of Board system.
- Author: Rex
- Application of Board system, or behavior of chess
Live demos¶
Usage¶
Install plugin¶
Load minify file¶
- Load plugin (minify file) in preload stage
scene.load.scenePlugin('rexboardplugin', 'https://raw.githubusercontent.com/rexrainbow/phaser3-rex-notes/master/dist/rexboardplugin.min.js', 'rexBoard', 'rexBoard');
- Add path-finder
var pathFinder = scene.rexBoard.add.pathFinder(config);
Import plugin¶
- Install rex plugins from npm
npm i phaser3-rex-plugins
- Install plugin in configuration of game
import BoardPlugin from 'phaser3-rex-plugins/plugins/board-plugin.js'; var config = { // ... plugins: { scene: [{ key: 'rexBoard', plugin: BoardPlugin, mapping: 'rexBoard' }, // ... ] } // ... }; var game = new Phaser.Game(config);
- Add path-finder
var pathFinder = scene.rexBoard.add.pathFinder(config);
Import class¶
- Install rex plugins from npm
npm i phaser3-rex-plugins
- Import class
import { PathFinder } from 'phaser3-rex-plugins/plugins/board-components.js';
- Add path-finder
var pathFinder = new PathFinder(config);
Create instance¶
var pathFinder = scene.rexBoard.add.pathFinder({
// occupiedTest: false,
// blockerTest: false,
// ** cost **
// cost: 1, // constant cost
// costCallback: undefined,
// costCallbackScope: undefined,
// cacheCost: true,
// pathMode: 10, // A*
// weight: 10, // weight for A* searching mode
// shuffleNeighbors: false,
})
occupiedTest
: Settrue
to test if target tile position is occupied or not, in cost function.blockerTest
: Settrue
to test blocker property in cost function.- Cost function
cost
: A constant cost for each non-blocked tilecostCallback
,costCallbackScope
: Get cost via callbackfunction(curTile, preTile, pathFinder) { return cost; }
- Board :
pathFinder.board
- Chess game object :
pathFinder.gameObject
- Cost of blocker :
pathFinder.BLOCKER
- Board :
pathMode
- Shortest path
'random'
, or0
'diagonal'
, or1
'straight'
, or2
'line'
, or3
- A* path
'A*'
, or10
'A*-random'
, or11
'A*-line'
, or12
- Shortest path
weight
: Weight parameter for A* searching mode.cacheCost
: Setfalse
to get cost every time. It is useful when cost is a function of (current tile, previous tile).shuffleNeighbors
: Shuffle neighbors.
Create behavior¶
var pathFinder = scene.rexBoard.add.pathFinder(chess, config);
Set chess¶
pathFinder.setChess(chess);
Note
Don't use this method if pathFinder is a behavior of Chess
Cost function¶
var callback = function(curTileXY, preTileXY, pathFinder) {
return cost;
}
cost
: Number cost.curTileXY
,preTileXY
: TileXY position{x, y}
. Cost of moving frompreTileXY
tocurTileXY
.preTileXY.pathCost
: Path cost ofpreTilexY
.preTileXY.preNodes
: Previous tiles ofpreTileXY
.
pathFinder
: Path finder object.pathFinder.board
: Board objectpathFinder.gameObject
: Chess game object.pathFinder.BLOCKER
: Cost of blocker. Return this value means that chess can not move tocurTileXY
.
Set cost function¶
- Constant cost for each non-blocked tile
pathFinder.setCostFunction(cost);
- Get cost via callback
pathFinder.setCostFunction(callback, scope);
Set path mode¶
pathFinder.setPathMode(pathMode)
pathMode
- Shortest path
'random'
, or0
'diagonal'
, or1
'straight'
, or2
'line'
, or3
- A* path
'A*'
, or10
'A*-random'
, or11
'A*-line'
, or12
- Shortest path
Find moveable area¶
var tileXYArray = pathFinder.findArea(movingPoints);
// var out = pathFinder.findArea(movingPoints, out);
movingPoints
pathFinder.INFINITY
(undefined) : Infinity moving points. Default value.
tileXYArray
: An array of moveable tile positions{x,y,pathCost}
Get shortest path to a moveable tile¶
var tileXYArray = pathFinder.getPath(endTileXY);
endTileXY
: Tile position of moveable area in last result ofpathFinder.findArea()
tileXYArray
: Moving path in an array of tile positions{x,y,pathCost}
- Uses moveTo behavior to move chess along path.
Path mode
- Path info of each tile is calculated during
pathFinder.findArea()
Find moving path¶
var tileXYArray = pathFinder.findPath(endTileXY);
// var tileXYArray = pathFinder.findPath(endTileXY, movingPoints, isClosest, out);
endTileXY
: Tile positiontileXYArray
: Moving path in an array of tile positions{x,y,pathCost}
- Uses moveTo behavior to move chess along path.
movingPoints
pathFinder.INFINITY
(undefined) : Infinity moving points. Default value.
isClosest
: Settrue
to get closest path.
Path mode
- Set
pathMode
to A* ('A*'
,'A*-random'
, or'A*-line'
) to speed up calculating.
Cost of tile¶
During or after finding moveable area...
- Get cost of path from chess to tile
var pathCost = pathFinder.tileXYToCost(tileX, tileY, true);
- Get cost of tile
var tileCost = pathFinder.tileXYToCost(tileX, tileY, false);