jigsawpuzzlegame 1.0.11 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -61,6 +61,9 @@ Make sure your container has a defined size:
61
61
  | `onWin` | function | `null` | Callback function called when the puzzle is completed |
62
62
  | `onStart` | function | `null` | Callback function called when a game starts |
63
63
  | `onStop` | function | `null` | Callback function called when a game is stopped |
64
+ | `onMerged` | function | `null` | Callback function called when puzzle pieces are merged together (receives the merged piece as parameter) |
65
+ | `onChanged` | function | `null` | Callback function called when a piece is moved or changed (receives the piece as parameter) |
66
+ | `onDeleted` | function | `null` | Callback function called when a piece is deleted/merged into another piece (receives the deleted piece as parameter) |
64
67
 
65
68
  ## API Methods
66
69
 
@@ -196,6 +199,15 @@ const puzzle = new JigsawPuzzle('puzzle-container', {
196
199
  },
197
200
  onStop: () => {
198
201
  console.log('Game stopped');
202
+ },
203
+ onMerged: (piece) => {
204
+ console.log('Pieces merged!', piece);
205
+ },
206
+ onChanged: (piece) => {
207
+ console.log('Piece moved', piece);
208
+ },
209
+ onDeleted: (piece) => {
210
+ console.log('Piece deleted', piece);
199
211
  }
200
212
  });
201
213
 
package/index.html CHANGED
@@ -266,6 +266,24 @@
266
266
  <td><code>null</code></td>
267
267
  <td>Callback function called when a game is stopped</td>
268
268
  </tr>
269
+ <tr>
270
+ <td><code>onMerged</code></td>
271
+ <td>function</td>
272
+ <td><code>null</code></td>
273
+ <td>Callback function called when puzzle pieces are merged together (receives the merged piece as parameter)</td>
274
+ </tr>
275
+ <tr>
276
+ <td><code>onChanged</code></td>
277
+ <td>function</td>
278
+ <td><code>null</code></td>
279
+ <td>Callback function called when a piece is moved or changed (receives the piece as parameter)</td>
280
+ </tr>
281
+ <tr>
282
+ <td><code>onDeleted</code></td>
283
+ <td>function</td>
284
+ <td><code>null</code></td>
285
+ <td>Callback function called when a piece is deleted/merged into another piece (receives the deleted piece as parameter)</td>
286
+ </tr>
269
287
  </tbody>
270
288
  </table>
271
289
 
@@ -307,6 +325,42 @@
307
325
  <p>Called when a game is stopped (when <code>stop()</code> is called).</p>
308
326
  </div>
309
327
 
328
+ <div class="method-section">
329
+ <h3><code>onMerged(piece)</code></h3>
330
+ <p>Called when puzzle pieces are merged together. Receives the merged piece as a parameter.</p>
331
+ <div class="example-box">
332
+ <strong>Example:</strong>
333
+ <pre><code>onMerged: (piece) => {
334
+ console.log('Pieces merged!', piece);
335
+ // Could update UI, play sound, etc.
336
+ }</code></pre>
337
+ </div>
338
+ </div>
339
+
340
+ <div class="method-section">
341
+ <h3><code>onChanged(piece)</code></h3>
342
+ <p>Called when a piece is moved or changed. Receives the piece as a parameter.</p>
343
+ <div class="example-box">
344
+ <strong>Example:</strong>
345
+ <pre><code>onChanged: (piece) => {
346
+ console.log('Piece moved', piece);
347
+ // Could track moves, update statistics, etc.
348
+ }</code></pre>
349
+ </div>
350
+ </div>
351
+
352
+ <div class="method-section">
353
+ <h3><code>onDeleted(piece)</code></h3>
354
+ <p>Called when a piece is deleted/merged into another piece. Receives the deleted piece as a parameter.</p>
355
+ <div class="example-box">
356
+ <strong>Example:</strong>
357
+ <pre><code>onDeleted: (piece) => {
358
+ console.log('Piece deleted', piece);
359
+ // Could track merges, update UI, etc.
360
+ }</code></pre>
361
+ </div>
362
+ </div>
363
+
310
364
  <h2>5. Public Methods</h2>
311
365
 
312
366
  <div class="method-section">
@@ -455,6 +509,18 @@ const puzzle = new JigsawPuzzle('puzzle-container', {
455
509
  },
456
510
  onStart: () => {
457
511
  console.log('Game started');
512
+ },
513
+ onStop: () => {
514
+ console.log('Game stopped');
515
+ },
516
+ onMerged: (piece) => {
517
+ console.log('Pieces merged!', piece);
518
+ },
519
+ onChanged: (piece) => {
520
+ console.log('Piece moved', piece);
521
+ },
522
+ onDeleted: (piece) => {
523
+ console.log('Piece deleted', piece);
458
524
  }
459
525
  });</code></pre>
460
526
  </div>
@@ -381,8 +381,10 @@ class Piece {
381
381
  // ============================================================================
382
382
 
383
383
  class PolyPiece {
384
- constructor(initialPiece, puzzleInstance) {
384
+ constructor(initialPiece, puzzleInstance, yIndex, xIndex) {
385
385
  this.puzzle = puzzleInstance;
386
+ this.yIndex = yIndex;
387
+ this.xIndex = xIndex;
386
388
  this.pckxmin = initialPiece.kx;
387
389
  this.pckxmax = initialPiece.kx + 1;
388
390
  this.pckymin = initialPiece.ky;
@@ -414,7 +416,7 @@ class PolyPiece {
414
416
  const kOther = puzzle.polyPieces.indexOf(otherPoly);
415
417
  puzzle.polyPieces.splice(kOther, 1);
416
418
  puzzle.container.removeChild(otherPoly.canvas);
417
-
419
+
418
420
  for (let k = 0; k < otherPoly.pieces.length; ++k) {
419
421
  this.pieces.push(otherPoly.pieces[k]);
420
422
  if (otherPoly.pieces[k].kx < this.pckxmin)
@@ -663,8 +665,8 @@ class PolyPiece {
663
665
  : "gold"
664
666
  : "rgba(0, 0, 0, 0.5)";
665
667
  this.ctx.shadowBlur = this.selected ? mmin(8, puzzle.scalex / 10) : 4;
666
- this.ctx.shadowOffsetX = this.selected ? 0 : -4;
667
- this.ctx.shadowOffsetY = this.selected ? 0 : 4;
668
+ this.ctx.shadowOffsetX = this.selected ? 0 : ([-4, 4, 4, -4][this.rot]);
669
+ this.ctx.shadowOffsetY = this.selected ? 0 : ([4, 4, -4, -4][this.rot]);
668
670
  this.ctx.fill(this.path);
669
671
  if (this.selected) {
670
672
  for (let i = 0; i < 6; i++) this.ctx.fill(this.path);
@@ -703,25 +705,19 @@ class PolyPiece {
703
705
 
704
706
  this.ctx.drawImage(
705
707
  puzzle.gameCanvas,
706
- srcx,
707
- srcy,
708
- w,
709
- h,
710
- destx,
711
- desty,
712
- w,
713
- h
708
+ srcx, srcy, w, h,
709
+ destx, desty, w, h
714
710
  );
715
711
  this.ctx.lineWidth = puzzle.embossThickness * 1.5;
716
712
 
717
- this.ctx.translate(
718
- puzzle.embossThickness / 2,
719
- -puzzle.embossThickness / 2
720
- );
713
+ const dxemboss = puzzle.embossThickness / 2 * [1, -1, -1, 1][this.rot];
714
+ const dyemboss = puzzle.embossThickness / 2 * [-1, -1, 1, 1][this.rot];
715
+
716
+ this.ctx.translate(dxemboss, dyemboss);
721
717
  this.ctx.strokeStyle = "rgba(0, 0, 0, 0.35)";
722
718
  this.ctx.stroke(path);
723
719
 
724
- this.ctx.translate(-puzzle.embossThickness, puzzle.embossThickness);
720
+ this.ctx.translate(-2 * dxemboss, -2 * dyemboss);
725
721
  this.ctx.strokeStyle = "rgba(255, 255, 255, 0.35)";
726
722
  this.ctx.stroke(path);
727
723
 
@@ -911,9 +907,10 @@ class InternalPuzzle {
911
907
 
912
908
  this.polyPieces = [];
913
909
  if (!baseData) {
914
- this.pieces.forEach((row) =>
915
- row.forEach((piece) => {
916
- this.polyPieces.push(new PolyPiece(piece, this));
910
+ this.pieces.forEach((row,yIndex) =>
911
+ row.forEach((piece,xIndex) => {
912
+ console.log('Piece:', yIndex, xIndex);
913
+ this.polyPieces.push(new PolyPiece(piece, this, yIndex, xIndex));
917
914
  })
918
915
  );
919
916
  arrayShuffle(this.polyPieces);
@@ -926,6 +923,7 @@ class InternalPuzzle {
926
923
  const pps = baseData[8];
927
924
  const offs = this.rotationAllowed ? 3 : 2;
928
925
  pps.forEach((ppData) => {
926
+ console.log('ppData:', ppData);
929
927
  let polyp = new PolyPiece(this.pieces[ppData[offs + 1]][ppData[offs]], this);
930
928
  polyp.x = ppData[0];
931
929
  polyp.y = ppData[1];
@@ -1364,7 +1362,8 @@ export class JigsawPuzzle {
1364
1362
  onStart: options.onStart || null,
1365
1363
  onStop: options.onStop || null,
1366
1364
  onMerged: options.onMerged || null,
1367
- onMoved: options.onMoved || null,
1365
+ onChanged: options.onChanged || null,
1366
+ onDeleted: options.onDeleted || null,
1368
1367
  };
1369
1368
  console.log('Options2:', this.options);
1370
1369
 
@@ -1774,12 +1773,14 @@ export class JigsawPuzzle {
1774
1773
  if (this.moving.pp.ifNear(pp)) {
1775
1774
  merged = true;
1776
1775
  if (pp.pieces.length > this.moving.pp.pieces.length) {
1776
+ if (this.options.onDeleted) this.options.onDeleted(this.moving.pp);
1777
1777
  pp.merge(this.moving.pp);
1778
- if (this.options.onMerged) this.options.onMerged(pp, this.moving.pp);
1778
+ if (this.options.onMerged) this.options.onMerged(pp);
1779
1779
  this.moving.pp = pp;
1780
1780
  } else {
1781
+ if (this.options.onDeleted) this.options.onDeleted(pp);
1781
1782
  this.moving.pp.merge(pp);
1782
- if (this.options.onMerged) this.options.onMerged(this.moving.pp, pp);
1783
+ if (this.options.onMerged) this.options.onMerged(this.moving.pp);
1783
1784
  }
1784
1785
  doneSomething = true;
1785
1786
  break;
@@ -1798,7 +1799,7 @@ export class JigsawPuzzle {
1798
1799
  if (this.puzzle.polyPieces.length === 1 && this.puzzle.polyPieces[0].rot === 0) {
1799
1800
  this.state = 60;
1800
1801
  }
1801
- if (this.options.onMoved) this.options.onMoved(this.moving.pp);
1802
+ if (this.options.onChanged) this.options.onChanged(this.moving.pp);
1802
1803
  }
1803
1804
  break;
1804
1805
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jigsawpuzzlegame",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "A class for creating a jigsaw puzzle",
5
5
  "keywords": [
6
6
  "jigsaw",