UNPKG

rot-js

Version:

A roguelike toolkit in JavaScript

63 lines (62 loc) 1.86 kB
import { DIRS } from "../constants.js"; ; ; export default class FOV { /** * @class Abstract FOV algorithm * @param {function} lightPassesCallback Does the light pass through x,y? * @param {object} [options] * @param {int} [options.topology=8] 4/6/8 */ constructor(lightPassesCallback, options = {}) { this._lightPasses = lightPassesCallback; this._options = Object.assign({ topology: 8 }, options); } /** * Return all neighbors in a concentric ring * @param {int} cx center-x * @param {int} cy center-y * @param {int} r range */ _getCircle(cx, cy, r) { let result = []; let dirs, countFactor, startOffset; switch (this._options.topology) { case 4: countFactor = 1; startOffset = [0, 1]; dirs = [ DIRS[8][7], DIRS[8][1], DIRS[8][3], DIRS[8][5] ]; break; case 6: dirs = DIRS[6]; countFactor = 1; startOffset = [-1, 1]; break; case 8: dirs = DIRS[4]; countFactor = 2; startOffset = [-1, 1]; break; default: throw new Error("Incorrect topology for FOV computation"); break; } /* starting neighbor */ let x = cx + startOffset[0] * r; let y = cy + startOffset[1] * r; /* circle */ for (let i = 0; i < dirs.length; i++) { for (let j = 0; j < r * countFactor; j++) { result.push([x, y]); x += dirs[i][0]; y += dirs[i][1]; } } return result; } }