@@ -31,6 +31,7 @@ class Chess {
|
||||
this.canvas = new visuals.Canvas();
|
||||
this.configVis = new visuals.ConfigVis(this.backend.getConfig());
|
||||
this.configVis.draw(this.canvas);
|
||||
this.activeSquares = new visuals.ActiveSquares();
|
||||
this.moveSource = null;
|
||||
this.canMoveTo = [];
|
||||
document.onclick = (ev) => this.click(ev);
|
||||
@@ -44,12 +45,14 @@ class Chess {
|
||||
if (source === null) return;
|
||||
let validMoves = this.backend.getAvailableMoves(source);
|
||||
if (validMoves === null) return;
|
||||
this.activeSquares.selectSquare(this.canvas, source);
|
||||
this.moveSource = source;
|
||||
}
|
||||
finalizeMove(target) {
|
||||
if (target !== null) {
|
||||
this.backend.makeMove(this.moveSource, target);
|
||||
}
|
||||
this.activeSquares.unselectSquare(this.canvas);
|
||||
this.syncBackend();
|
||||
this.moveSource = null;
|
||||
}
|
||||
|
||||
@@ -86,6 +86,23 @@ export function rowColToBoardIndex(row, col) {
|
||||
return `${colLetter}${rowDigit}`;
|
||||
}
|
||||
|
||||
export class ActiveSquares {
|
||||
constructor() {
|
||||
this.selectedSquare = null;
|
||||
}
|
||||
selectSquare(canvas, position) {
|
||||
let [r, c] = boardIndexToRowCol(position);
|
||||
canvas.drawSquareColor("#c0c0ff", r, c);
|
||||
this.selectedSquare = position;
|
||||
}
|
||||
unselectSquare(canvas) {
|
||||
if (this.selectedSquare === null) return;
|
||||
let [r, c] = boardIndexToRowCol(this.selectedSquare);
|
||||
canvas.drawSquareColor(canvas.decideColor(r, c), r, c);
|
||||
this.selectedSquare = null;
|
||||
}
|
||||
}
|
||||
|
||||
class PieceVis {
|
||||
constructor(piece) {
|
||||
this.piece = piece;
|
||||
|
||||
Reference in New Issue
Block a user