@@ -21,66 +21,69 @@ export class Game {
21
21
this . #board = new Board (
22
22
HTMLTable ,
23
23
"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 ) {
42
31
this . #deselectAndUnhighlightCells( ) ;
43
- this . #endTurn( ) ;
44
32
return ;
45
33
}
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 ( ) ;
56
36
}
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
+ }
61
42
}
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 )
84
87
85
88
if ( this . #isTutorial) {
86
89
this . #tutorial. activate ( this . #players[ 0 ] ) ;
0 commit comments