amaccann / phaser-navmesh-generation

12 stars 1 forks source link

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="https://github.com/amaccann/phaser-navmesh-generation/raw/master/my/path/to/phaser.js"></script>
<script src="https://github.com/amaccann/phaser-navmesh-generation/raw/master/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(tileMap, tileLayer, {
  collisionIndices: [1, 2, 3],
  midPointThreshold: 0,
  useMidPoint: false,
  debug: {
    hulls: false,
    navMesh: false,
    navMeshNodes: false,
    polygonBounds: false,
    aStarPath: false
  }
});

Params:

  1. Then, to find a path between two Phaser.Point instances, call:
    navMesh.getPath(position, destination, offset);

    Params:

    • position is a Phaser.Point of your starting world position (required)
    • destination is a Phaser.Point of the destination / end world position (required)
    • offset is an offset value to keep a distance (optional, default 0)

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:

Returns:

plugin.removeSprite(uuid, refresh);

Params: