UNPKG

rot-js

Version:

A roguelike toolkit in JavaScript

46 lines (45 loc) 1.41 kB
import { DIRS } from "../constants.js"; /** * @class Abstract pathfinder * @param {int} toX Target X coord * @param {int} toY Target Y coord * @param {function} passableCallback Callback to determine map passability * @param {object} [options] * @param {int} [options.topology=8] */ export default class Path { constructor(toX, toY, passableCallback, options = {}) { this._toX = toX; this._toY = toY; this._passableCallback = passableCallback; this._options = Object.assign({ topology: 8 }, options); this._dirs = DIRS[this._options.topology]; if (this._options.topology == 8) { /* reorder dirs for more aesthetic result (vertical/horizontal first) */ this._dirs = [ this._dirs[0], this._dirs[2], this._dirs[4], this._dirs[6], this._dirs[1], this._dirs[3], this._dirs[5], this._dirs[7] ]; } } _getNeighbors(cx, cy) { let result = []; for (let i = 0; i < this._dirs.length; i++) { let dir = this._dirs[i]; let x = cx + dir[0]; let y = cy + dir[1]; if (!this._passableCallback(x, y)) { continue; } result.push([x, y]); } return result; } }