phaser-navmesh-generation v0.3.1
phaser-navmesh-generation
Warning: this plugin is still Work in Progress (WIP). It's possibly not stable enough for use in a production product - use at your own risk (for now!)
This Phaser ScenePlugin generates Navigation Mesh (navmesh) data from supplied Phaser.TilemapLayer data and collison indices thereof. Contains configuration options for fine-grain control
Getting Started:
import it as you would any other module & include it as a ScenePlugin within your game's configuration object:
import NavMeshPlugin from 'phaser-navmesh-generation';
const game = {
// ...
// other Game config values ...
// ...
plugins: {
scene: [
{ key: 'NavMeshPlugin', plugin: NavMeshPlugin, mapping: 'navMeshPlugin' }
]
},Usage:
- First, we need to generate a new navigation mesh based on the Tilemap / Tilelayer you want to use to calculate collision data from. Use the
mappingvalue provided within yourSceneto access the freshly injected plugin:
var navMesh = this.navMeshPlugin.buildFromTileLayer(tileMap, tileLayer, {
collisionIndices: [1, 2, 3],
midPointThreshold: 0,
useMidPoint: false,
debug: {
hulls: false,
navMesh: false,
navMeshNodes: false,
polygonBounds: false,
aStarPath: false
}
});Params:
collisionIndices: anArrayof collision indices that your tilemap uses for collisions (required)midPointThreshold: aNumbervalue telling how narrow a navmesh triangle needs to be before it's ignored during pathing (optional; default0)timingInfo: Show in the console how long it took to build the NavMesh - and search for paths (optional; defaultfalse)useMidPoint: aBooleanvalue on whether to include all triangle edge mid-points in calculating triangulation (optional; default:true)offsetHullsBy: aNumbervalue to offset (expand) each hull cluster by. Useful to use a small value to prevent excessively parallel edges (optional; default:0.1)debug: various optional debug options to Render the stages of NavMesh calculation:hulls: Every (recursive) 'chunk' of impassable tiles found on the tilemapnavMesh: Draw all the actual triangles generated for this navmeshnavMeshNodes: Draw all connections found between neighbouring trianglespolygonBounds: Draw the bonding radius between each navmesh triangleaStarPath: Draw the aStar path found between points (WIP debug, will remove later)
- Then, to find a path between two
Phaser.Geom.Pointinstances, call:
navMesh.getPath(position, destination, offset);Params:
positionis aPhaser.Geom.Pointof your starting world position (required)destinationis aPhaser.Geom.Pointof the destination / end world position (required)offsetis an offset value to keep a distance (optional, default0)
This method returns two useful pieces of data:
path an Array of Points that is the shortest path to your destination
offsetPath an Array containing the offset path, relative to the offset value given in getPath
Other methods:
const sprite = plugin.addSprite(x, y, width, height, refresh);
Your map may have Sprites that act as impassable areas (houses, trees etc), and you can mark this area of the map using the above method
Params:
xthe Tile X location of the sprite (required)ythe Tile Y location of the sprite (required)widththe Width of the sprite, expressed as tile units (required)heightthe Height of the sprite, expressed as tile units (required)refresh: If you wish the navMesh to be re-calculated after removing the sprite (optional, defaulttrue)
Returns:
- The internal instance of the sprite; includes a
uuidthat can be used for later removal
plugin.removeSprite(uuid, refresh);
Params:
uuid: the String UUID of the sprite you wish to remove (required)refresh: If you wish the navMesh to be re-calculated after removing the sprite (optional, defaulttrue)