Skip to content

Commit 338fefc

Browse files
feat: load maps from .txt file
1 parent 8a9a60c commit 338fefc

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/board.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Cell } from "./cell";
22
import { Pawn } from "./pieces/pawn";
3+
import maps from "./maps.txt?raw";
34

45
export class Board {
56
#cells: Cell[][] = [];
@@ -10,16 +11,22 @@ export class Board {
1011

1112
constructor(
1213
HTMLTable: HTMLTableElement,
13-
template: string,
14+
mapName: string,
1415
onCellClick: (clickedCell: Cell) => void,
1516
onCastleClick: (clickedCell: Cell) => void,
1617
) {
18+
const map = maps
19+
.split("\n\n")
20+
.find((s) => s.startsWith(mapName))
21+
?.replace(/^.*\n/, "")
22+
?.split("\n")
23+
24+
if (!map) {
25+
throw new Error("there is no map with this name");
26+
}
27+
1728
const HTMLBody = HTMLTable.createTBody();
18-
template
19-
.replace(/ /g, "")
20-
.split("\n")
21-
.filter((row) => row.length > 0)
22-
.forEach((row, y) => {
29+
map.forEach((row, y) => {
2330
const HTMLRow = HTMLBody.insertRow();
2431
this.#cells[y] = [];
2532
for (const symbol of row.split("")) {
@@ -32,7 +39,7 @@ export class Board {
3239
};
3340
this.#cells[y].push(cell);
3441

35-
if (symbol === "-") {
42+
if (symbol === ".") {
3643
} else if (symbol === "1") {
3744
cell.placePiece(new Pawn("white"), true);
3845
cell.setBuilding("castle");

src/game.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,7 @@ export class Game {
1313
constructor(HTMLTable: HTMLTableElement) {
1414
this.#board = new Board(
1515
HTMLTable,
16-
`
17-
---------
18-
-1--c--c-
19-
----w----
20-
---w-----
21-
-cw-c-wc-
22-
-----w---
23-
----w----
24-
-c--c--2-
25-
---------
26-
`,
16+
"canyon",
2717
(clickedCell) => {
2818
// place piece
2919
if (this.#selectedCell?.piece) {

src/maps.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
canyon
2+
.........
3+
.1..c..c.
4+
....w....
5+
...w.....
6+
.cw.c.wc.
7+
.....w...
8+
....w....
9+
.c..c..2.
10+
.........
11+
12+
trenches
13+
.........
14+
.1..1..1.
15+
.w..w..w.
16+
.........
17+
.........
18+
.........
19+
.........
20+
.w..w..w.
21+
.2..2..2.
22+
.........

0 commit comments

Comments
 (0)