0.5.1 • Published 4 years ago

@pixelburp/phaser3-navmesh-generation v0.5.1

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

phaser-navmesh-generation

Warning: this plugin is still heavy 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 plugin generates Navigation Meshes from supplied Phaser.TilemapLayer data and collison indices thereof. Contains configuration options

Current version: 0.2.2

Getting Started:

ES6 / Node

import it as you would any other project:

import NavMeshPlugin from 'phaser-navmesh-generation';

Legacy

If you're doing it the old fashioned way, simply add <script> tag after your main Phaser tag:

<script src="my/path/to/phaser.js"></script>
<script src="my/path/to/navmesh-plugin.js"></script>

Then in your game's JS code:

  preload() {
    var plugin = this.game.plugins.add(NavMeshPlugin);
  }

Usage:

  1. First, we need to generate a new navigation mesh using the following method (options are below)
var navMesh = plugin.buildFromTileLayer({
  collisionIndices: [1, 2, 3],
  midPointThreshold: 0,
  useMidPoint: false,
  scene: Phaser.Scene,
  tileMap: <Phaser.Tilemaps.Tilemap>,
  tileLayer: <Phaser.Tilemaps.DynamicTilemapLayer|Phaser.Tilemaps.StaticTilemapLayer>,
  debug: {
    hulls: false,
    navMesh: false,
    navMeshNodes: false,
    polygonBounds: false,
    aStarPath: false
  }
});
  1. Then, to find a path between two Phaser.Point instances, call:
navMesh.getPath(position, destination, offset);

navMeshPlugin.buildFromTileLayer()

Kind: instance method of NavMeshPlugin

ParamTypeDefaultDescription
options.tileMapPhaser.Tilemaps.Tilemap
options.tileLayerPhaser.Tilemaps.TilemapLayer
options.scenePhaser.Scene
options.collisionIndicesArray.<Number>an Array of collision indices that your tilemap uses for collisions
options.midPointThresholdNumber0a Number value telling how narrow a navmesh triangle needs to be before it's ignored during pathing
options.timingInfoBooleanfalseShow in the console how long it took to build the NavMesh - and search for paths
options.useMidPointBooleantruea Boolean value on whether to include all triangle edge mid-points in calculating triangulation
options.offsetHullsByNumber0.1a Number value to offset (expand) each hull cluster by. Useful to use a small value to prevent excessively parallel edges
options.debugObjectvarious optional debug options to Render the stages of NavMesh calculation:
options.debug.hulls:BooleanEvery (recursive) 'chunk' of impassable tiles found on the tilemap
options.debug.navMesh:BooleanDraw all the actual triangles generated for this navmesh
options.debug.navMeshNodes:BooleanDraw all connections found between neighbouring triangles
options.debug.polygonBounds:BooleanDraw the bonding radius between each navmesh triangle
options.debug.aStarPath:BooleanDraw the aStar path found between points (WIP debug, will remove later)

navMeshPlugin.getClosestPolygon(position, includeOutOfBounds)

Finds the closest NavMesh polygon, based on world point

Kind: instance method of NavMeshPlugin

ParamTypeDefaultDescription
positionPhaser.Geom.Point | Phaser.Math.Vector2The world point
includeOutOfBoundsBooleanfalsewhether to include "out of bounds" searches

navMeshPlugin.getPath(position, position, offset)

Finds Calculate the shortest path to a given destination

Kind: instance method of NavMeshPlugin

ParamTypeDescription
positionPhaser.Geom.Point | Phaser.Math.Vector2startPosition
positionPhaser.Geom.Point | Phaser.Math.Vector2endPosition
offsetNumberAn offset value to keep a distance (optional, default 0)

navMeshPlugin.getAllTilesWithin(worldX, worldY, spriteWidth, spriteHeight) ⇒

Given world coords & "sprite" size, find all overlapping Tiles in the tileLayer

Kind: instance method of NavMeshPlugin
Returns: Array

ParamTypeDescription
worldXNumberWorld X coordinate
worldYNumberWorld Y coordinate
spriteWidthNumberwidth (in pixels) of the Sprite you wish to add
spriteHeightNumberheight (in pixels) of the Sprite you wish to add

navMeshPlugin.addSprite(worldX, worldY, spriteWidth, spriteHeight)

Adds a "sprite" (like an immovable prop), that navmesh should include in its calculations.

Kind: instance method of NavMeshPlugin

ParamTypeDescription
worldXNumberWorld X coordinate
worldYNumberWorld Y coordinate
spriteWidthNumberwidth (in pixels) of the Sprite you wish to add
spriteHeightNumberheight (in pixels) of the Sprite you wish to add

navMeshPlugin.removeSprite(worldX, worldY, spriteWidth, spriteHeight)

Find any previously cached "sprites" within these bounds, and reset to the original value

Kind: instance method of NavMeshPlugin

ParamTypeDescription
worldXNumberWorld X coordinate
worldYNumberWorld Y coordinate
spriteWidthNumberwidth (in pixels) of the Sprite you wish to remove
spriteHeightNumberheight (in pixels) of the Sprite you wish to remove
0.5.1

4 years ago

0.5.0

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago