iPuzzle/js/state/PuzzleState.js

43 lines
1.2 KiB
JavaScript

class PuzzleState {
constructor({ imageKey, imagePath, pieceCount, roomCode, cols, rows, pieces, groups, completed, bgKey, bgPath }) {
this.imageKey = imageKey;
this.imagePath = imagePath;
this.pieceCount = pieceCount;
this.roomCode = roomCode;
this.cols = cols;
this.rows = rows;
this.pieces = pieces; // PieceData[]
this.groups = groups; // number[][] — each inner array is one group's piece IDs
this.completed = completed || false;
this.bgKey = bgKey || 'bg_dark_wood';
this.bgPath = bgPath || 'assets/images/ui/dark_wood.jpg';
}
serialize() {
return {
imageKey: this.imageKey,
imagePath: this.imagePath,
pieceCount: this.pieceCount,
roomCode: this.roomCode,
cols: this.cols,
rows: this.rows,
pieces: this.pieces.map(p => ({
id: p.id,
gridRow: p.gridRow,
gridCol: p.gridCol,
edges: p.edges,
x: p.x,
y: p.y
})),
groups: this.groups,
completed: this.completed,
bgKey: this.bgKey,
bgPath: this.bgPath,
};
}
static deserialize(obj) {
return new PuzzleState(obj);
}
}