Skip to content

Commit c091bcb

Browse files
Add proper padding and debug options to the draw() method.
1 parent 8342c36 commit c091bcb

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

src/draw/canvas.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Dimensions } from "../types";
22

3+
/**
4+
* Initialize a canvas and a rendering context.
5+
*/
36
export function initializeContext(): {
47
context: CanvasRenderingContext2D;
58
canvasDimensions: Dimensions;

src/draw/index.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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++) {

src/draw/utils.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function duplicateTilemap(tiles: Tiles): Tiles {
3232

3333
/**
3434
* Pad a tilemap on all sides.
35+
* ⚠️ Note: currently not used, but could serve as a convenient method.
3536
*/
3637
export function padTilemap(
3738
tiles: Tiles,
@@ -56,10 +57,32 @@ export function padTilemap(
5657
});
5758
}
5859

60+
/**
61+
* Pad a tree of nodes.
62+
*/
63+
export function padNodes(rootNode: Node<Room>, padding: number) {
64+
traverseTree((node) => {
65+
if (!node.value.position || !node.value.dimensions) {
66+
return;
67+
}
68+
69+
node.value.position.y += padding;
70+
node.value.position.x += padding;
71+
72+
if (node.value.corridor) {
73+
node.value.corridor.position.y += padding;
74+
node.value.corridor.position.x += padding;
75+
}
76+
}, rootNode);
77+
}
78+
5979
/**
6080
* Get the dungeon width and height in tiles unit.
6181
*/
62-
export function getDungeonDimensions(rootNode: Node<Room>): Dimensions {
82+
export function getDungeonDimensions(
83+
rootNode: Node<Room>,
84+
padding: number
85+
): Dimensions {
6386
let dimensions: Dimensions = {
6487
width: 0,
6588
height: 0,
@@ -81,6 +104,10 @@ export function getDungeonDimensions(rootNode: Node<Room>): Dimensions {
81104
}
82105
}, rootNode);
83106

107+
// We add padding to the overall dimension
108+
dimensions.width += padding * 2;
109+
dimensions.height += padding * 2;
110+
84111
return dimensions;
85112
}
86113

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ let inputGraph: InputDungeon = LARGE;
1111
*/
1212
function generateAndDraw() {
1313
const rootNode = logStep(`Generate ✅`, () => generate(inputGraph));
14-
logStep(`Draw ✅`, () => draw(rootNode));
14+
logStep(`Draw ✅`, () =>
15+
draw(rootNode, {
16+
padding: 4,
17+
debugWidgets: true,
18+
})
19+
);
1520
}
1621

1722
/**

0 commit comments

Comments
 (0)