@@ -9,21 +9,35 @@ import {
99 duplicateTilemap ,
1010 getDungeonDimensions ,
1111 getTileSize ,
12+ padNodes ,
1213} from "./utils" ;
1314
15+ interface DrawOptions {
16+ /** The amount of padding to apply to the dungeon's tilemap. */
17+ padding ?: number ;
18+ /** Wether or not to draw debugging widgets (ex: grid lines, connections, ids, etc.). */
19+ debugWidgets ?: boolean ;
20+ }
21+
1422/**
1523 * Entrypoint method to:
1624 * - Get a reference to the Canvas' context
1725 * - Transform rooms and corridors to tiles
18- * - Draw widgets such as room names and connections
26+ * - Draw widgets such as room names and connections (optional)
1927 *
2028 * Note: all transformations to data are done by reference.
2129 */
22- export function draw ( rootNode : Node < Room > ) {
30+ export function draw ( rootNode : Node < Room > , options : DrawOptions = { } ) {
2331 const { context, canvasDimensions } = initializeContext ( ) ;
2432
33+ const padding =
34+ ! ! options . padding && ! isNaN ( options . padding ) && options . padding > 0
35+ ? options . padding
36+ : 0 ;
37+ const debugWidgets = ! ! options . debugWidgets ;
38+
2539 // Find dungeon's width and height
26- const dungeonDimensions = getDungeonDimensions ( rootNode ) ;
40+ const dungeonDimensions = getDungeonDimensions ( rootNode , padding ) ;
2741 const tileSize = getTileSize ( canvasDimensions , dungeonDimensions ) ;
2842
2943 // Create empty tilesmap
@@ -33,21 +47,23 @@ export function draw(rootNode: Node<Room>) {
3347 TilesTypes . WALL
3448 ) ;
3549
50+ // Add some padding to the tilemap for a more visually appealing result
51+ padNodes ( rootNode , padding ) ;
52+
3653 // Carve rooms and corridors into the tilesmap
3754 tiles = carveRooms ( tiles , rootNode ) ;
3855 tiles = carveCorridors ( tiles , rootNode ) ;
3956
40- // Add some padding to the tilemap for a more visually appealing result (optional)
41- // tiles = padTilemap(tiles, 3, TilesTypes.WALL);
42-
4357 // Draw tiles
4458 drawTilesMask ( context , tileSize , tiles ) ;
4559 drawTiles ( context , tileSize , tiles ) ;
4660
4761 // Draw widgets
48- drawGrid ( context , tileSize , canvasDimensions ) ;
49- drawConnections ( context , tileSize , rootNode ) ;
50- drawRoomIds ( context , tileSize , rootNode ) ;
62+ if ( debugWidgets ) {
63+ drawGrid ( context , tileSize , canvasDimensions ) ;
64+ drawConnections ( context , tileSize , rootNode ) ;
65+ drawRoomIds ( context , tileSize , rootNode ) ;
66+ }
5167}
5268
5369//
@@ -258,7 +274,7 @@ function drawTilesMask(
258274 }
259275}
260276
261- export function normalizeTilemap ( tiles : Tiles ) : Tiles {
277+ function normalizeTilemap ( tiles : Tiles ) : Tiles {
262278 const copy = duplicateTilemap ( tiles ) ;
263279
264280 for ( let y = 0 ; y < copy . length ; y ++ ) {
0 commit comments