Skip to content

Commit bfdb511

Browse files
Add padTilemap method
1 parent 5fcf814 commit bfdb511

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/draw/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export function draw(rootNode: Node<Room>) {
3737
tiles = carveRooms(tiles, rootNode);
3838
tiles = carveCorridors(tiles, rootNode);
3939

40+
// Add some padding to the tilemap for a more visually appealing result (optional)
41+
// tiles = padTilemap(tiles, 3, TilesTypes.WALL);
42+
4043
// Draw tiles
4144
drawTilesMask(context, tileSize, tiles);
4245
drawTiles(context, tileSize, tiles);
@@ -237,8 +240,8 @@ function drawTilesMask(
237240
// Compute the bitmask tilesmap
238241
const mask = computeTilesMask(normalized);
239242

240-
for (let y = 0; y < tiles.length; y++) {
241-
for (let x = 0; x < tiles[y].length; x++) {
243+
for (let y = 0; y < mask.length; y++) {
244+
for (let x = 0; x < mask[y].length; x++) {
242245
const tileId = mask[y][x];
243246
const texture = tilesTextures[tileId];
244247

src/draw/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,32 @@ export function duplicateTilemap(tiles: Tiles): Tiles {
3030
});
3131
}
3232

33+
/**
34+
* Pad a tilemap on all sides.
35+
*/
36+
export function padTilemap(
37+
tiles: Tiles,
38+
paddingWidth: number,
39+
fill: number
40+
): Tiles {
41+
const copy = duplicateTilemap(tiles);
42+
43+
const newLine = new Array(tiles[0].length).fill(fill);
44+
45+
for (let i = 0; i < paddingWidth; i++) {
46+
copy.unshift(newLine);
47+
copy.push(newLine);
48+
}
49+
50+
return copy.map((row) => {
51+
return [
52+
...new Array(paddingWidth).fill(fill),
53+
...row,
54+
...new Array(paddingWidth).fill(fill),
55+
];
56+
});
57+
}
58+
3359
/**
3460
* Get the dungeon width and height in tiles unit.
3561
*/

0 commit comments

Comments
 (0)