Skip to content

Commit 45b1516

Browse files
refactor: use native event for events
1 parent 8b2be69 commit 45b1516

File tree

3 files changed

+74
-71
lines changed

3 files changed

+74
-71
lines changed

src/board.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export class Board {
1919
constructor(
2020
HTMLTable: HTMLTableElement,
2121
mapName: string,
22-
onCellClick: (clickedCell: Cell, x: number, y: number) => void,
23-
onCastleClick: (clickedCell: Cell) => void,
2422
) {
2523
const map = maps
2624
.split("\n\n")
@@ -37,13 +35,7 @@ export class Board {
3735
const HTMLRow = HTMLBody.insertRow();
3836
this.#cells[y] = [];
3937
row.split("").forEach((symbol, x) => {
40-
const cell = new Cell(HTMLRow.insertCell());
41-
cell.onClick = () => {
42-
onCellClick(cell, x, y);
43-
};
44-
cell.onMenu = () => {
45-
onCastleClick(cell);
46-
};
38+
const cell = new Cell(HTMLRow.insertCell(), x, y);
4739
this.#cells[y].push(cell);
4840

4941
if (symbol === ".") {

src/cell.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ export class Cell {
66
#piece: Piece | null = null;
77
#HTMLCell: HTMLTableCellElement;
88
#isHighlighted = false;
9-
10-
onClick: () => void = () => {};
11-
12-
onMenu: () => void = () => {};
9+
#x: number;
10+
#y: number;
1311

1412
get piece() {
1513
return this.#piece;
@@ -27,15 +25,25 @@ export class Cell {
2725
return this.#isHighlighted;
2826
}
2927

30-
constructor(HTMLCell: HTMLTableCellElement) {
28+
get x() {
29+
return this.#x;
30+
}
31+
32+
get y() {
33+
return this.#y;
34+
}
35+
36+
constructor(HTMLCell: HTMLTableCellElement, x: number, y: number) {
3137
this.#HTMLCell = HTMLCell;
38+
this.#x = x;
39+
this.#y = y;
3240
this.#HTMLCell.classList.add("cell");
3341
this.#HTMLCell.addEventListener("click", () => {
34-
this.onClick();
42+
window.dispatchEvent(new CustomEvent("cellclick", { detail: { cell: this } }))
3543
});
3644
this.#HTMLCell.addEventListener("contextmenu", (event) => {
3745
event.preventDefault();
38-
this.onMenu();
46+
window.dispatchEvent(new CustomEvent("cellmenu", { detail: { cell: this } }))
3947
});
4048
}
4149

src/game.ts

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,66 +21,69 @@ export class Game {
2121
this.#board = new Board(
2222
HTMLTable,
2323
"canyon",
24-
(clickedCell, x, y) => {
25-
// place piece
26-
if (this.#selectedCell?.piece) {
27-
if (!clickedCell.isHighlighted) {
28-
this.#deselectAndUnhighlightCells();
29-
return;
30-
}
31-
if (clickedCell.piece) {
32-
this.#nextPlayer.handlePieceLoss();
33-
}
34-
if (clickedCell.building) {
35-
this.#currentPlayer.handleBuildingCapture(clickedCell.building);
36-
if (clickedCell.owner === this.#nextPlayer.color) {
37-
this.#nextPlayer.handleBuildingLoss(clickedCell.building);
38-
}
39-
}
40-
clickedCell.placePiece(this.#selectedCell.piece);
41-
this.#selectedCell.removePiece();
24+
); // TODO: static func
25+
26+
window.addEventListener("cellclick", ((e: CustomEvent) => {
27+
const clickedCell = e.detail.cell
28+
// place piece
29+
if (this.#selectedCell?.piece) {
30+
if (!clickedCell.isHighlighted) {
4231
this.#deselectAndUnhighlightCells();
43-
this.#endTurn();
4432
return;
4533
}
46-
47-
// grab piece
48-
if (
49-
clickedCell.piece &&
50-
clickedCell.piece.color === this.#currentPlayer.color
51-
) {
52-
this.#selectedCell = clickedCell;
53-
clickedCell.toggleSelected();
54-
clickedCell.piece.highlightMoves(this.#board.cells, x, y);
55-
return;
34+
if (clickedCell.piece) {
35+
this.#nextPlayer.handlePieceLoss();
5636
}
57-
},
58-
(clickedCell) => {
59-
if (clickedCell.owner !== this.#currentPlayer.color) {
60-
return;
37+
if (clickedCell.building) {
38+
this.#currentPlayer.handleBuildingCapture(clickedCell.building);
39+
if (clickedCell.owner === this.#nextPlayer.color) {
40+
this.#nextPlayer.handleBuildingLoss(clickedCell.building);
41+
}
6142
}
62-
this.#castleMenu.open(
63-
(piece) => {
64-
if (!this.#currentPlayer.canBuyPiece()) {
65-
alert("You can't buy any more pieces.");
66-
return;
67-
}
68-
this.#currentPlayer.handlePieceBuy(piece);
69-
clickedCell.placePiece(piece);
70-
this.#endTurn();
71-
},
72-
(building) => {
73-
clickedCell.setBuilding(building);
74-
this.#currentPlayer.handleBuildingUpgrade(building);
75-
this.#endTurn();
76-
},
77-
this.#currentPlayer,
78-
this.#currentPlayer.gold,
79-
clickedCell.building === "factory",
80-
clickedCell.piece !== null,
81-
);
82-
},
83-
);
43+
clickedCell.placePiece(this.#selectedCell.piece);
44+
this.#selectedCell.removePiece();
45+
this.#deselectAndUnhighlightCells();
46+
this.#endTurn();
47+
return;
48+
}
49+
// grab piece
50+
if (
51+
clickedCell.piece &&
52+
clickedCell.piece.color === this.#currentPlayer.color
53+
) {
54+
this.#selectedCell = clickedCell;
55+
clickedCell.toggleSelected();
56+
clickedCell.piece.highlightMoves(this.#board.cells, clickedCell.x, clickedCell.y);
57+
return;
58+
}
59+
}) as EventListener)
60+
61+
window.addEventListener("cellmenu", ((e: CustomEvent) => {
62+
const clickedCell = e.detail.cell
63+
if (clickedCell.owner !== this.#currentPlayer.color) {
64+
return;
65+
}
66+
this.#castleMenu.open(
67+
(piece) => {
68+
if (!this.#currentPlayer.canBuyPiece()) {
69+
alert("You can't buy any more pieces.");
70+
return;
71+
}
72+
this.#currentPlayer.handlePieceBuy(piece);
73+
clickedCell.placePiece(piece);
74+
this.#endTurn();
75+
},
76+
(building) => {
77+
clickedCell.setBuilding(building);
78+
this.#currentPlayer.handleBuildingUpgrade(building);
79+
this.#endTurn();
80+
},
81+
this.#currentPlayer,
82+
this.#currentPlayer.gold,
83+
clickedCell.building === "factory",
84+
clickedCell.piece !== null,
85+
);
86+
}) as EventListener)
8487

8588
if (this.#isTutorial) {
8689
this.#tutorial.activate(this.#players[0]);

0 commit comments

Comments
 (0)