Refactor with some OOP

This commit is contained in:
2021-06-01 21:43:25 +02:00
parent 751cc2cc04
commit a3ae7c9f71

View File

@@ -38,45 +38,83 @@ function drawChessBoard(canvas) {
} }
} }
function drawPiece(canvas, piece, field) { function computeScreenCoords(position, canvas) {
let canvasRect = canvas.getBoundingClientRect(); let canvasRect = canvas.getBoundingClientRect();
let rc = boardIndexToRowCol(field); let rc = boardIndexToRowCol(position);
let im = new Image(BOX_SIDE, BOX_SIDE);
im.src = `./icons/${piece}.svg`;
im.onload = () => {
let topPx = canvasRect.top + INDEX_MARGIN + rc[0]*BOX_SIDE + window.pageYOffset; let topPx = canvasRect.top + INDEX_MARGIN + rc[0]*BOX_SIDE + window.pageYOffset;
let leftPx = canvasRect.left + INDEX_MARGIN + rc[1]*BOX_SIDE + window.pageXOffset; let leftPx = canvasRect.left + INDEX_MARGIN + rc[1]*BOX_SIDE + window.pageXOffset;
return [topPx, leftPx]
}
class Piece {
constructor(who, color, position) {
this.who = who;
this.color = color;
this.position = position;
this._im = null;
}
icon() {
return `./icons/${this.who}_${this.color}.svg`
}
draw(canvas) {
let [topPx, leftPx] = computeScreenCoords(this.position, canvas);
if (this._im === null) {
let im = new Image(BOX_SIDE, BOX_SIDE);
im.src = this.icon();
im.onload = () => {
im.style.position = 'absolute'; im.style.position = 'absolute';
im.style.top = `${topPx}px`; im.style.top = `${topPx}px`;
im.style.left = `${leftPx}px`; im.style.left = `${leftPx}px`;
}; };
document.body.appendChild(im); document.body.appendChild(im);
this._im = im;
}
else {
this._im.style.top = `${topPx}px`;
this._im.style.left = `${leftPx}px`;
}
}
} }
function _setupBoard(canvas) { class Configuration {
drawPiece(_canvas, 'rook_black', 'a8'); constructor(pieces) {
drawPiece(_canvas, 'rook_black', 'h8'); this.pieces = pieces
drawPiece(_canvas, 'knight_black', 'b8'); }
drawPiece(_canvas, 'knight_black', 'g8'); draw(canvas) {
drawPiece(_canvas, 'bishop_black', 'c8'); for (let piece of this.pieces) {
drawPiece(_canvas, 'bishop_black', 'f8'); piece.draw(canvas);
drawPiece(_canvas, 'queen_black', 'd8'); }
drawPiece(_canvas, 'king_black', 'e8'); }
for (let colLetter of INDEX_LETTERS) {
drawPiece(canvas, 'pawn_black', `${colLetter}7`);
drawPiece(canvas, 'pawn_white', `${colLetter}2`);
} }
drawPiece(_canvas, 'rook_white', 'a1'); function setupBoard() {
drawPiece(_canvas, 'rook_white', 'h1'); let pawns = [];
drawPiece(_canvas, 'knight_white', 'b1'); for (let x of INDEX_LETTERS) {
drawPiece(_canvas, 'knight_white', 'g1'); pawns.push(new Piece('pawn', 'black', `${x}7`),
drawPiece(_canvas, 'bishop_white', 'c1'); new Piece('pawn', 'white', `${x}2`));
drawPiece(_canvas, 'bishop_white', 'f1'); }
drawPiece(_canvas, 'queen_white', 'd1'); return new Configuration([
drawPiece(_canvas, 'king_white', 'e1'); new Piece('rook', 'black', 'a8'),
new Piece('rook', 'black', 'h8'),
new Piece('knight', 'black', 'b8'),
new Piece('knight', 'black', 'g8'),
new Piece('bishop', 'black', 'c8'),
new Piece('bishop', 'black', 'f8'),
new Piece('queen', 'black', 'd8'),
new Piece('king', 'black', 'e8'),
...pawns,
new Piece('rook', 'white', 'a1'),
new Piece('rook', 'white', 'h1'),
new Piece('knight', 'white', 'b1'),
new Piece('knight', 'white', 'g1'),
new Piece('bishop', 'white', 'c1'),
new Piece('bishop', 'white', 'f1'),
new Piece('queen', 'white', 'd1'),
new Piece('king', 'white', 'e1'),
])
} }
function boardIndexToRowCol(boardIndex) { function boardIndexToRowCol(boardIndex) {
@@ -87,6 +125,11 @@ function boardIndexToRowCol(boardIndex) {
return [row, col]; return [row, col];
} }
var _canvas = createCanvas(); function demo() {
drawChessBoard(_canvas); let canvas = createCanvas();
_setupBoard(_canvas); drawChessBoard(canvas);
let config = setupBoard();
config.draw(canvas);
}
demo();