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