iPuzzle/js/puzzle/PieceObject.js

47 lines
1.3 KiB
JavaScript

/* global Phaser */
class PieceObject {
/**
* @param {Phaser.Scene} scene
* @param {object} pieceData - PieceData from PuzzleGenerator
* @param {number} pieceW - width of grid cell
* @param {number} pieceH - height of grid cell
* @param {number} tabSize - canvas padding used by PieceRenderer
* @param {number} canvasW - full canvas width (pieceW + 2*tabSize)
* @param {number} canvasH - full canvas height (pieceH + 2*tabSize)
*/
constructor(scene, pieceData, pieceW, pieceH, tabSize, canvasW, canvasH) {
this.data = pieceData;
this.pieceW = pieceW;
this.pieceH = pieceH;
this.tabSize = tabSize;
this.image = scene.add.image(pieceData.x, pieceData.y, `piece_${pieceData.id}`);
// Anchor at the cell centre (not the canvas centre).
// The cell starts at (tabSize, tabSize) in the canvas, so cell-centre is at
// (tabSize + pieceW/2, tabSize + pieceH/2) within the canvas.
this.image.setOrigin(
(tabSize + pieceW / 2) / canvasW,
(tabSize + pieceH / 2) / canvasH
);
this.image.setInteractive();
this.image.setDepth(0);
}
setPosition(x, y) {
this.data.x = x;
this.data.y = y;
this.image.setPosition(x, y);
}
setDepth(d) {
this.image.setDepth(d);
}
destroy() {
this.image.destroy();
}
}