Skip to content

Commit

Permalink
feat: load maps from .txt file
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperwyczawski committed Jul 21, 2024
1 parent 8a9a60c commit 338fefc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
21 changes: 14 additions & 7 deletions src/board.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Cell } from "./cell";
import { Pawn } from "./pieces/pawn";
import maps from "./maps.txt?raw";

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

constructor(
HTMLTable: HTMLTableElement,
template: string,
mapName: string,
onCellClick: (clickedCell: Cell) => void,
onCastleClick: (clickedCell: Cell) => void,
) {
const map = maps
.split("\n\n")
.find((s) => s.startsWith(mapName))
?.replace(/^.*\n/, "")
?.split("\n")

if (!map) {
throw new Error("there is no map with this name");
}

const HTMLBody = HTMLTable.createTBody();
template
.replace(/ /g, "")
.split("\n")
.filter((row) => row.length > 0)
.forEach((row, y) => {
map.forEach((row, y) => {
const HTMLRow = HTMLBody.insertRow();
this.#cells[y] = [];
for (const symbol of row.split("")) {
Expand All @@ -32,7 +39,7 @@ export class Board {
};
this.#cells[y].push(cell);

if (symbol === "-") {
if (symbol === ".") {
} else if (symbol === "1") {
cell.placePiece(new Pawn("white"), true);
cell.setBuilding("castle");
Expand Down
12 changes: 1 addition & 11 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ export class Game {
constructor(HTMLTable: HTMLTableElement) {
this.#board = new Board(
HTMLTable,
`
---------
-1--c--c-
----w----
---w-----
-cw-c-wc-
-----w---
----w----
-c--c--2-
---------
`,
"canyon",
(clickedCell) => {
// place piece
if (this.#selectedCell?.piece) {
Expand Down
22 changes: 22 additions & 0 deletions src/maps.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
canyon
.........
.1..c..c.
....w....
...w.....
.cw.c.wc.
.....w...
....w....
.c..c..2.
.........

trenches
.........
.1..1..1.
.w..w..w.
.........
.........
.........
.........
.w..w..w.
.2..2..2.
.........

0 comments on commit 338fefc

Please sign in to comment.