From 52864fccf583773dfab5d0ed4467ab6833c71c1d Mon Sep 17 00:00:00 2001 From: Dominik Marczuk Date: Wed, 19 Dec 2018 00:38:38 +0100 Subject: [PATCH] reorganised imports for FOV module --- src/fov/discrete-shadowcasting.ts | 40 +++++++++++++++--------------- src/fov/fov.ts | 8 +++--- src/fov/index.ts | 12 ++++++--- src/fov/precise-shadowcasting.ts | 18 +++++++------- src/fov/recursive-shadowcasting.ts | 22 ++++++++-------- src/index.ts | 2 +- src/lighting.ts | 2 +- 7 files changed, 54 insertions(+), 50 deletions(-) diff --git a/src/fov/discrete-shadowcasting.ts b/src/fov/discrete-shadowcasting.ts index e633decf..dd4c6db2 100644 --- a/src/fov/discrete-shadowcasting.ts +++ b/src/fov/discrete-shadowcasting.ts @@ -1,20 +1,20 @@ -import FOV, { VisibilityCallback } from "./fov.js"; +import { FOV, VisibilityCallback } from "./fov"; /** * @class Discrete shadowcasting algorithm. Obsoleted by Precise shadowcasting. * @augments ROT.FOV */ -export default class DiscreteShadowcasting extends FOV { +export class DiscreteShadowcasting extends FOV { compute(x: number, y: number, R: number, callback: VisibilityCallback) { /* this place is always visible */ callback(x, y, 0, 1); /* standing in a dark place. FIXME is this a good idea? */ if (!this._lightPasses(x, y)) { return; } - + /* start and end angles */ let DATA: number[] = []; - + let A, B, cx, cy, blocks; /* analyze surrounding cells in concentric rings, starting from the center */ @@ -27,10 +27,10 @@ export default class DiscreteShadowcasting extends FOV { cy = neighbors[i][1]; A = angle * (i - 0.5); B = A + angle; - + blocks = !this._lightPasses(cx, cy); if (this._visibleCoords(Math.floor(A), Math.ceil(B), blocks, DATA)) { callback(cx, cy, r, 1); } - + if (DATA.length == 2 && DATA[0] == 0 && DATA[1] == 360) { return; } /* cutoff? */ } /* for all cells in this ring */ @@ -44,38 +44,38 @@ export default class DiscreteShadowcasting extends FOV { * @param {int[][]} DATA shadowed angle pairs */ _visibleCoords(A: number, B: number, blocks: boolean, DATA: number[]): boolean { - if (A < 0) { + if (A < 0) { let v1 = this._visibleCoords(0, B, blocks, DATA); let v2 = this._visibleCoords(360+A, 360, blocks, DATA); return v1 || v2; } - + let index = 0; while (index < DATA.length && DATA[index] < A) { index++; } - + if (index == DATA.length) { /* completely new shadow */ - if (blocks) { DATA.push(A, B); } + if (blocks) { DATA.push(A, B); } return true; } - + let count = 0; - + if (index % 2) { /* this shadow starts in an existing shadow, or within its ending boundary */ while (index < DATA.length && DATA[index] < B) { index++; count++; } - + if (count == 0) { return false; } - - if (blocks) { + + if (blocks) { if (count % 2) { DATA.splice(index-count, count, B); } else { DATA.splice(index-count, count); } } - + return true; } else { /* this shadow starts outside an existing shadow, or within a starting boundary */ @@ -83,18 +83,18 @@ export default class DiscreteShadowcasting extends FOV { index++; count++; } - + /* visible when outside an existing shadow, or when overlapping */ if (A == DATA[index-count] && count == 1) { return false; } - - if (blocks) { + + if (blocks) { if (count % 2) { DATA.splice(index-count, count, A); } else { DATA.splice(index-count, count, A, B); } } - + return true; } } diff --git a/src/fov/fov.ts b/src/fov/fov.ts index c9ecc1cf..f1fb0cc6 100644 --- a/src/fov/fov.ts +++ b/src/fov/fov.ts @@ -1,4 +1,4 @@ -import { DIRS } from "../constants.js"; +import { DIRS } from "../constants"; export interface LightPassesCallback { (x: number, y: number): boolean }; @@ -8,9 +8,9 @@ export interface Options { topology: 4 | 6 | 8 } -export default abstract class FOV { - _lightPasses: LightPassesCallback; - _options: Options; +export abstract class FOV { + protected _lightPasses: LightPassesCallback; + protected _options: Options; /** * @class Abstract FOV algorithm diff --git a/src/fov/index.ts b/src/fov/index.ts index f0fd8851..68439878 100644 --- a/src/fov/index.ts +++ b/src/fov/index.ts @@ -1,5 +1,9 @@ -import DiscreteShadowcasting from "./discrete-shadowcasting.js"; -import PreciseShadowcasting from "./precise-shadowcasting.js"; -import RecursiveShadowcasting from "./recursive-shadowcasting.js"; +import { DiscreteShadowcasting } from "./discrete-shadowcasting"; +import { PreciseShadowcasting } from "./precise-shadowcasting"; +import { RecursiveShadowcasting } from "./recursive-shadowcasting"; -export default { DiscreteShadowcasting, PreciseShadowcasting, RecursiveShadowcasting }; +export const FOV = { + DiscreteShadowcasting, + PreciseShadowcasting, + RecursiveShadowcasting +}; diff --git a/src/fov/precise-shadowcasting.ts b/src/fov/precise-shadowcasting.ts index fe7b6ded..1bc96919 100644 --- a/src/fov/precise-shadowcasting.ts +++ b/src/fov/precise-shadowcasting.ts @@ -1,4 +1,4 @@ -import FOV, { VisibilityCallback } from "./fov.js"; +import { FOV, VisibilityCallback } from "./fov"; type Arc = [number, number]; @@ -6,17 +6,17 @@ type Arc = [number, number]; * @class Precise shadowcasting algorithm * @augments ROT.FOV */ -export default class PreciseShadowcasting extends FOV { +export class PreciseShadowcasting extends FOV { compute(x: number, y: number, R: number, callback: VisibilityCallback) { /* this place is always visible */ callback(x, y, 0, 1); /* standing in a dark place. FIXME is this a good idea? */ if (!this._lightPasses(x, y)) { return; } - + /* list of all shadows */ let SHADOWS: Arc[] = []; - + let cx, cy, blocks, A1, A2, visibility; /* analyze surrounding cells in concentric rings, starting from the center */ @@ -29,8 +29,8 @@ export default class PreciseShadowcasting extends FOV { cy = neighbors[i][1]; /* shift half-an-angle backwards to maintain consistency of 0-th cells */ A1 = [i ? 2*i-1 : 2*neighborCount-1, 2*neighborCount]; - A2 = [2*i+1, 2*neighborCount]; - + A2 = [2*i+1, 2*neighborCount]; + blocks = !this._lightPasses(cx, cy); visibility = this._checkVisibility(A1 as Arc, A2 as Arc, blocks, SHADOWS); if (visibility) { callback(cx, cy, r, visibility); } @@ -79,15 +79,15 @@ export default class PreciseShadowcasting extends FOV { let visible = true; if (index1 == index2 && (edge1 || edge2)) { /* subset of existing shadow, one of the edges match */ - visible = false; + visible = false; } else if (edge1 && edge2 && index1+1==index2 && (index2 % 2)) { /* completely equivalent with existing shadow */ visible = false; } else if (index1 > index2 && (index1 % 2)) { /* subset of existing shadow, not touching */ visible = false; } - + if (!visible) { return 0; } /* fast case: not visible */ - + let visibleLength; /* compute the length of visible arc, adjust list of shadows (if blocking) */ diff --git a/src/fov/recursive-shadowcasting.ts b/src/fov/recursive-shadowcasting.ts index aa394743..2fcb798b 100644 --- a/src/fov/recursive-shadowcasting.ts +++ b/src/fov/recursive-shadowcasting.ts @@ -1,4 +1,4 @@ -import FOV, { VisibilityCallback } from "./fov.js"; +import { FOV, VisibilityCallback } from "./fov"; /** Octants used for translating recursive shadowcasting offsets */ const OCTANTS = [ @@ -18,7 +18,7 @@ const OCTANTS = [ * Based on Peter Harkins' implementation of Björn Bergström's algorithm described here: http://www.roguebasin.com/index.php?title=FOV_using_recursive_shadowcasting * @augments ROT.FOV */ -export default class RecursiveShadowcasting extends FOV { +export class RecursiveShadowcasting extends FOV { /** * Compute visibility for a 360-degree circle * @param {int} x @@ -91,10 +91,10 @@ export default class RecursiveShadowcasting extends FOV { * @param {float} visSlopeStart The slope to start at * @param {float} visSlopeEnd The slope to end at * @param {int} radius The radius to reach out to - * @param {int} xx - * @param {int} xy - * @param {int} yx - * @param {int} yy + * @param {int} xx + * @param {int} xy + * @param {int} yx + * @param {int} yy * @param {function} callback The callback to use when we hit a block that is visible */ _castVisibility(startX: number, startY: number, row: number, visSlopeStart: number, visSlopeEnd: number, radius: number, xx: number, xy: number, yx: number, yy: number, callback: VisibilityCallback) { @@ -116,18 +116,18 @@ export default class RecursiveShadowcasting extends FOV { //Range of the row let slopeStart = (dx - 0.5) / (dy + 0.5); let slopeEnd = (dx + 0.5) / (dy - 0.5); - + //Ignore if not yet at left edge of Octant if (slopeEnd > visSlopeStart) { continue; } - + //Done if past right edge if (slopeStart < visSlopeEnd) { break; } - + //If it's in range, it's visible if ((dx * dx + dy * dy) < (radius * radius)) { callback(mapX, mapY, i, 1); } - + if (!blocked) { //If tile is a blocking tile, cast around it if (!this._lightPasses(mapX, mapY) && i < radius) { @@ -141,7 +141,7 @@ export default class RecursiveShadowcasting extends FOV { newStart = slopeEnd; continue; } - + //Block has ended blocked = false; visSlopeStart = newStart; diff --git a/src/index.ts b/src/index.ts index caa259c1..7c0f555a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ export {default as Display} from "./display/display.js"; export {default as StringGenerator} from "./stringgenerator.js"; export {default as EventQueue} from "./eventqueue.js"; export {default as Scheduler, SpeedActor} from "./scheduler/index.js"; -export {default as FOV} from "./fov/index.js"; +export { FOV } from "./fov/index"; export {default as Map} from "./map/index.js"; export {default as Noise} from "./noise/index.js"; export {default as Path} from "./path/index.js"; diff --git a/src/lighting.ts b/src/lighting.ts index 08af4968..8f35d0f7 100644 --- a/src/lighting.ts +++ b/src/lighting.ts @@ -1,4 +1,4 @@ -import FOV from "./fov/fov.js"; +import { FOV } from "./fov/fov"; import * as Color from "./color.js"; type LightColor = [number, number, number];