@mborecki/memory-game 0.3.1 → 0.3.2

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.
@@ -6,7 +6,7 @@ var appGlobals = require('./app-globals-V2Kpy_OQ.js');
6
6
  const defineCustomElements = async (win, options) => {
7
7
  if (typeof window === 'undefined') return undefined;
8
8
  await appGlobals.globalScripts();
9
- return index.bootstrapLazy([["mb-memory-game.cjs",[[513,"mb-memory-game",{"tileGroups":[16],"reverseTile":[16],"init":[4],"cols":[2],"tiles":[32],"selected":[32],"matchedTiles":[32],"uncoverSelected":[32],"initGame":[64]},null,{"board":[{"watchBoard":0}],"matchedTiles":[{"watchMatchedTiles":0}]}]]]], options);
9
+ return index.bootstrapLazy([["mb-memory-game.cjs",[[513,"mb-memory-game",{"tileGroups":[16],"reverseTile":[16],"init":[4],"cols":[2],"tiles":[32],"selected":[32],"matchedTiles":[32],"uncoverSelected":[32],"initGame":[64]},null,{"tileGroups":[{"watchTileGroups":0}],"matchedTiles":[{"watchMatchedTiles":0}]}]]]], options);
10
10
  };
11
11
 
12
12
  exports.setNonce = index.setNonce;
@@ -51,13 +51,13 @@ const MbMemoryGame = class {
51
51
  this.matched = index.createEvent(this, "matched");
52
52
  }
53
53
  tileGroups = [];
54
- reverseTile = { type: 'text', text: '?' };
55
- init = true;
56
- watchBoard() {
54
+ watchTileGroups() {
57
55
  if (this.init) {
58
56
  this.initGame();
59
57
  }
60
58
  }
59
+ reverseTile = { type: 'text', text: '?' };
60
+ init = true;
61
61
  tiles = [];
62
62
  selected = [];
63
63
  matchedTiles = [];
@@ -72,8 +72,8 @@ const MbMemoryGame = class {
72
72
  completed;
73
73
  matched;
74
74
  componentWillLoad() {
75
- console.log('componentDidLoad', this.init);
76
- if (this.init && this.tileGroups) {
75
+ console.log('componentDidLoad', this.init, this.tileGroups, this.reverseTile);
76
+ if (this.init && this.tileGroups.length) {
77
77
  this.initGame();
78
78
  }
79
79
  }
@@ -190,9 +190,9 @@ const MbMemoryGame = class {
190
190
  });
191
191
  }
192
192
  render() {
193
- return index.h("div", { key: 'bb7b898014606de493fd580e50ef685f529ac2bb', part: "container", style: {
193
+ return index.h("div", { key: '56287b90e45786be67ed85b8964bb99a6589f2b5', part: "container", style: {
194
194
  '--aspect': `${this.gridAspectRatio}`
195
- } }, index.h("div", { key: '0c677fb539e7af023c21411e70adf64050c97077', part: "list", style: {
195
+ } }, index.h("div", { key: 'cc965692a5523254c6930a26cb244a24fa7c05f7', part: "list", style: {
196
196
  '--cols': `${this.gridCols}`,
197
197
  '--rows': `${this.gridRows}`
198
198
  } }, this.tiles.map(t => {
@@ -200,8 +200,8 @@ const MbMemoryGame = class {
200
200
  })));
201
201
  }
202
202
  static get watchers() { return {
203
- "board": [{
204
- "watchBoard": 0
203
+ "tileGroups": [{
204
+ "watchTileGroups": 0
205
205
  }],
206
206
  "matchedTiles": [{
207
207
  "watchMatchedTiles": 0
@@ -19,7 +19,7 @@ var patchBrowser = () => {
19
19
 
20
20
  patchBrowser().then(async (options) => {
21
21
  await appGlobals.globalScripts();
22
- return index.bootstrapLazy([["mb-memory-game.cjs",[[513,"mb-memory-game",{"tileGroups":[16],"reverseTile":[16],"init":[4],"cols":[2],"tiles":[32],"selected":[32],"matchedTiles":[32],"uncoverSelected":[32],"initGame":[64]},null,{"board":[{"watchBoard":0}],"matchedTiles":[{"watchMatchedTiles":0}]}]]]], options);
22
+ return index.bootstrapLazy([["mb-memory-game.cjs",[[513,"mb-memory-game",{"tileGroups":[16],"reverseTile":[16],"init":[4],"cols":[2],"tiles":[32],"selected":[32],"matchedTiles":[32],"uncoverSelected":[32],"initGame":[64]},null,{"tileGroups":[{"watchTileGroups":0}],"matchedTiles":[{"watchMatchedTiles":0}]}]]]], options);
23
23
  });
24
24
 
25
25
  exports.setNonce = index.setNonce;
@@ -3,13 +3,13 @@ import { getSet } from "../../utils/get-set";
3
3
  import { Tile } from "./tile";
4
4
  export class MbMemoryGame {
5
5
  tileGroups = [];
6
- reverseTile = { type: 'text', text: '?' };
7
- init = true;
8
- watchBoard() {
6
+ watchTileGroups() {
9
7
  if (this.init) {
10
8
  this.initGame();
11
9
  }
12
10
  }
11
+ reverseTile = { type: 'text', text: '?' };
12
+ init = true;
13
13
  tiles = [];
14
14
  selected = [];
15
15
  matchedTiles = [];
@@ -24,8 +24,8 @@ export class MbMemoryGame {
24
24
  completed;
25
25
  matched;
26
26
  componentWillLoad() {
27
- console.log('componentDidLoad', this.init);
28
- if (this.init && this.tileGroups) {
27
+ console.log('componentDidLoad', this.init, this.tileGroups, this.reverseTile);
28
+ if (this.init && this.tileGroups.length) {
29
29
  this.initGame();
30
30
  }
31
31
  }
@@ -142,9 +142,9 @@ export class MbMemoryGame {
142
142
  });
143
143
  }
144
144
  render() {
145
- return h("div", { key: 'bb7b898014606de493fd580e50ef685f529ac2bb', part: "container", style: {
145
+ return h("div", { key: '56287b90e45786be67ed85b8964bb99a6589f2b5', part: "container", style: {
146
146
  '--aspect': `${this.gridAspectRatio}`
147
- } }, h("div", { key: '0c677fb539e7af023c21411e70adf64050c97077', part: "list", style: {
147
+ } }, h("div", { key: 'cc965692a5523254c6930a26cb244a24fa7c05f7', part: "list", style: {
148
148
  '--cols': `${this.gridCols}`,
149
149
  '--rows': `${this.gridRows}`
150
150
  } }, this.tiles.map(t => {
@@ -347,8 +347,8 @@ export class MbMemoryGame {
347
347
  }
348
348
  static get watchers() {
349
349
  return [{
350
- "propName": "board",
351
- "methodName": "watchBoard"
350
+ "propName": "tileGroups",
351
+ "methodName": "watchTileGroups"
352
352
  }, {
353
353
  "propName": "matchedTiles",
354
354
  "methodName": "watchMatchedTiles"
@@ -1 +1 @@
1
- import{h as t,t as e,p as i,H as s,c as r}from"./index.js";function a({tile:e,selected:i,matched:s,uncovered:r,reversTile:a}){return t("div",{class:{"tile-container":!0,selected:i,matched:s}},t(c,{tile:r?e.data:a}))}function c({tile:e}){return"text"===e.type?t("button",{class:"tile tile-text"},e.text):"image"===e.type?t("button",{class:"tile tile-image"},t("img",{src:e.imageSrc})):void 0}const o=i(class extends s{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.completed=r(this,"completed"),this.matched=r(this,"matched")}tileGroups=[];reverseTile={type:"text",text:"?"};init=!0;watchBoard(){this.init&&this.initGame()}tiles=[];selected=[];matchedTiles=[];watchMatchedTiles(){this.matchedTiles.length===this.tiles.length&&this.completed.emit()}cols;blockUI=!1;uncoverSelected=!1;completed;matched;componentWillLoad(){console.log("componentDidLoad",this.init),this.init&&this.tileGroups&&this.initGame()}slots=new Map;get gridCols(){return this.cols?this.cols:Math.floor(Math.sqrt(this.tiles.length))}get gridRows(){return Math.ceil(this.tiles.length/this.gridCols)}get gridAspectRatio(){return this.gridCols/this.gridRows}async initGame(t=this.tileGroups,e=this.reverseTile){console.log("initGame",t),this.reverseTile=e,this.buildTiles(t)}buildTiles(t){let e=[],i=0;t.forEach((t=>{const s=function(t,e){if(console.log("getSet",t,e),t.length<=0)throw Error("getSet() - no items");if(t.length===e)return[...t];let i=[];for(;e-i.length>t.length;)i=[...i,...t];const s=new Set;for(;s.size<e-i.length;)s.add(t[Math.floor(Math.random()*t.length)]);return[...i,...s]}(t.tiles,t.count);e=[...e,...s.map((e=>({id:i++,data:{...e},groupId:t.id,uncovered:!1,matched:!1})))]})),this.tiles=e.sort((()=>Math.random()-.5))}async onTileClick(t){if(!this.blockUI)if(this.selected.includes(t))this.selected=this.selected.filter((e=>e!==t));else if(this.selected=[...this.selected,t],2===this.selected.length){const t=this.tiles.find((t=>t.id===this.selected[0])),e=this.tiles.find((t=>t.id===this.selected[1]));if(!t||!e)return;this.blockUI=!0,this.uncoverSelected=!0,t.groupId===e.groupId?(await Promise.all([this.markAsMatched(t.id),this.markAsMatched(e.id)]),this.matched.emit({t1:t.data,t2:e.data,groupId:t.groupId}),this.matchedTiles=[...this.matchedTiles,t.id,e.id]):await Promise.all([this.animateWrongMatch(t.id),this.animateWrongMatch(e.id)]),this.uncoverSelected=!1,this.blockUI=!1,this.selected=[]}}markAsMatched(t){return new Promise((e=>{const i=this.slots.get(t);if(!i)return;const s=i.children[0].animate([],{duration:1e3});s.addEventListener("finish",(()=>{e()})),s.addEventListener("cancel",(()=>{e()}))}))}animateWrongMatch(t){return new Promise((e=>{const i=this.slots.get(t);if(!i)return;const s=i.children[0].animate([{rotate:"0deg"},{rotate:"0deg"},{rotate:"30deg"},{rotate:"0deg"},{rotate:"-30deg"},{rotate:"0deg"},{rotate:"0deg"}],{duration:1500});s.addEventListener("finish",(()=>{e()})),s.addEventListener("cancel",(()=>{e()}))}))}render(){return t("div",{key:"bb7b898014606de493fd580e50ef685f529ac2bb",part:"container",style:{"--aspect":""+this.gridAspectRatio}},t("div",{key:"0c677fb539e7af023c21411e70adf64050c97077",part:"list",style:{"--cols":""+this.gridCols,"--rows":""+this.gridRows}},this.tiles.map((e=>t("div",{part:"tile-slot",onClick:()=>this.onTileClick(e.id),ref:t=>this.slots.set(e.id,t)},t(a,{tile:e,selected:this.selected.includes(e.id),matched:this.matchedTiles.includes(e.id),reversTile:this.reverseTile,uncovered:this.uncoverSelected&&this.selected.includes(e.id)}))))))}static get watchers(){return{board:[{watchBoard:0}],matchedTiles:[{watchMatchedTiles:0}]}}static get style(){return":host{box-sizing:border-box;display:grid;place-content:center;width:100%;height:100%;container-type:size}[part=container]{width:min(100cqw, 100cqh / var(--aspect, 1));height:min(100cqw * var(--aspect, 1), 100cqh);container-type:size;background:pink}[part=list]{width:100cqw;height:100cqh;display:grid;grid-template-columns:repeat(var(--cols, 4), 1fr);grid-template-columns:repeat(var(--rows, 4), 1fr)}[part=tile-slot]{display:block;aspect-ratio:1;padding:5%}.tile-container{min-width:0;transition:scale 0.3s;width:100%;height:100%}.tile-container:hover{scale:1.05}.tile-container.selected{scale:1.1}.tile-container.matched{scale:0}.tile{border-radius:var(--tile-border-radius, 5px);border:var(--tile-border-width, 2px) solid var(--tile-border-color, black);padding:0;margin:0}.tile-text{width:100%;height:100%;aspect-ratio:1;background:#d9d9d9;display:grid;place-content:center}.tile-image{width:100%;height:100%}.tile-image img{display:block;width:100%;height:100%;object-fit:cover}"}},[513,"mb-memory-game",{tileGroups:[16],reverseTile:[16],init:[4],cols:[2],tiles:[32],selected:[32],matchedTiles:[32],uncoverSelected:[32],initGame:[64]},void 0,{board:[{watchBoard:0}],matchedTiles:[{watchMatchedTiles:0}]}]);function h(){"undefined"!=typeof customElements&&["mb-memory-game"].forEach((t=>{"mb-memory-game"===t&&(customElements.get(e(t))||customElements.define(e(t),o))}))}h();const n=o,d=h;export{n as MbMemoryGame,d as defineCustomElement}
1
+ import{h as t,t as e,p as i,H as s,c as r}from"./index.js";function a({tile:e,selected:i,matched:s,uncovered:r,reversTile:a}){return t("div",{class:{"tile-container":!0,selected:i,matched:s}},t(c,{tile:r?e.data:a}))}function c({tile:e}){return"text"===e.type?t("button",{class:"tile tile-text"},e.text):"image"===e.type?t("button",{class:"tile tile-image"},t("img",{src:e.imageSrc})):void 0}const o=i(class extends s{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.completed=r(this,"completed"),this.matched=r(this,"matched")}tileGroups=[];watchTileGroups(){this.init&&this.initGame()}reverseTile={type:"text",text:"?"};init=!0;tiles=[];selected=[];matchedTiles=[];watchMatchedTiles(){this.matchedTiles.length===this.tiles.length&&this.completed.emit()}cols;blockUI=!1;uncoverSelected=!1;completed;matched;componentWillLoad(){console.log("componentDidLoad",this.init,this.tileGroups,this.reverseTile),this.init&&this.tileGroups.length&&this.initGame()}slots=new Map;get gridCols(){return this.cols?this.cols:Math.floor(Math.sqrt(this.tiles.length))}get gridRows(){return Math.ceil(this.tiles.length/this.gridCols)}get gridAspectRatio(){return this.gridCols/this.gridRows}async initGame(t=this.tileGroups,e=this.reverseTile){console.log("initGame",t),this.reverseTile=e,this.buildTiles(t)}buildTiles(t){let e=[],i=0;t.forEach((t=>{const s=function(t,e){if(console.log("getSet",t,e),t.length<=0)throw Error("getSet() - no items");if(t.length===e)return[...t];let i=[];for(;e-i.length>t.length;)i=[...i,...t];const s=new Set;for(;s.size<e-i.length;)s.add(t[Math.floor(Math.random()*t.length)]);return[...i,...s]}(t.tiles,t.count);e=[...e,...s.map((e=>({id:i++,data:{...e},groupId:t.id,uncovered:!1,matched:!1})))]})),this.tiles=e.sort((()=>Math.random()-.5))}async onTileClick(t){if(!this.blockUI)if(this.selected.includes(t))this.selected=this.selected.filter((e=>e!==t));else if(this.selected=[...this.selected,t],2===this.selected.length){const t=this.tiles.find((t=>t.id===this.selected[0])),e=this.tiles.find((t=>t.id===this.selected[1]));if(!t||!e)return;this.blockUI=!0,this.uncoverSelected=!0,t.groupId===e.groupId?(await Promise.all([this.markAsMatched(t.id),this.markAsMatched(e.id)]),this.matched.emit({t1:t.data,t2:e.data,groupId:t.groupId}),this.matchedTiles=[...this.matchedTiles,t.id,e.id]):await Promise.all([this.animateWrongMatch(t.id),this.animateWrongMatch(e.id)]),this.uncoverSelected=!1,this.blockUI=!1,this.selected=[]}}markAsMatched(t){return new Promise((e=>{const i=this.slots.get(t);if(!i)return;const s=i.children[0].animate([],{duration:1e3});s.addEventListener("finish",(()=>{e()})),s.addEventListener("cancel",(()=>{e()}))}))}animateWrongMatch(t){return new Promise((e=>{const i=this.slots.get(t);if(!i)return;const s=i.children[0].animate([{rotate:"0deg"},{rotate:"0deg"},{rotate:"30deg"},{rotate:"0deg"},{rotate:"-30deg"},{rotate:"0deg"},{rotate:"0deg"}],{duration:1500});s.addEventListener("finish",(()=>{e()})),s.addEventListener("cancel",(()=>{e()}))}))}render(){return t("div",{key:"56287b90e45786be67ed85b8964bb99a6589f2b5",part:"container",style:{"--aspect":""+this.gridAspectRatio}},t("div",{key:"cc965692a5523254c6930a26cb244a24fa7c05f7",part:"list",style:{"--cols":""+this.gridCols,"--rows":""+this.gridRows}},this.tiles.map((e=>t("div",{part:"tile-slot",onClick:()=>this.onTileClick(e.id),ref:t=>this.slots.set(e.id,t)},t(a,{tile:e,selected:this.selected.includes(e.id),matched:this.matchedTiles.includes(e.id),reversTile:this.reverseTile,uncovered:this.uncoverSelected&&this.selected.includes(e.id)}))))))}static get watchers(){return{tileGroups:[{watchTileGroups:0}],matchedTiles:[{watchMatchedTiles:0}]}}static get style(){return":host{box-sizing:border-box;display:grid;place-content:center;width:100%;height:100%;container-type:size}[part=container]{width:min(100cqw, 100cqh / var(--aspect, 1));height:min(100cqw * var(--aspect, 1), 100cqh);container-type:size;background:pink}[part=list]{width:100cqw;height:100cqh;display:grid;grid-template-columns:repeat(var(--cols, 4), 1fr);grid-template-columns:repeat(var(--rows, 4), 1fr)}[part=tile-slot]{display:block;aspect-ratio:1;padding:5%}.tile-container{min-width:0;transition:scale 0.3s;width:100%;height:100%}.tile-container:hover{scale:1.05}.tile-container.selected{scale:1.1}.tile-container.matched{scale:0}.tile{border-radius:var(--tile-border-radius, 5px);border:var(--tile-border-width, 2px) solid var(--tile-border-color, black);padding:0;margin:0}.tile-text{width:100%;height:100%;aspect-ratio:1;background:#d9d9d9;display:grid;place-content:center}.tile-image{width:100%;height:100%}.tile-image img{display:block;width:100%;height:100%;object-fit:cover}"}},[513,"mb-memory-game",{tileGroups:[16],reverseTile:[16],init:[4],cols:[2],tiles:[32],selected:[32],matchedTiles:[32],uncoverSelected:[32],initGame:[64]},void 0,{tileGroups:[{watchTileGroups:0}],matchedTiles:[{watchMatchedTiles:0}]}]);function h(){"undefined"!=typeof customElements&&["mb-memory-game"].forEach((t=>{"mb-memory-game"===t&&(customElements.get(e(t))||customElements.define(e(t),o))}))}h();const n=o,l=h;export{n as MbMemoryGame,l as defineCustomElement}
@@ -5,7 +5,7 @@ import { g as globalScripts } from './app-globals-DQuL1Twl.js';
5
5
  const defineCustomElements = async (win, options) => {
6
6
  if (typeof window === 'undefined') return undefined;
7
7
  await globalScripts();
8
- return bootstrapLazy([["mb-memory-game",[[513,"mb-memory-game",{"tileGroups":[16],"reverseTile":[16],"init":[4],"cols":[2],"tiles":[32],"selected":[32],"matchedTiles":[32],"uncoverSelected":[32],"initGame":[64]},null,{"board":[{"watchBoard":0}],"matchedTiles":[{"watchMatchedTiles":0}]}]]]], options);
8
+ return bootstrapLazy([["mb-memory-game",[[513,"mb-memory-game",{"tileGroups":[16],"reverseTile":[16],"init":[4],"cols":[2],"tiles":[32],"selected":[32],"matchedTiles":[32],"uncoverSelected":[32],"initGame":[64]},null,{"tileGroups":[{"watchTileGroups":0}],"matchedTiles":[{"watchMatchedTiles":0}]}]]]], options);
9
9
  };
10
10
 
11
11
  export { defineCustomElements };
@@ -49,13 +49,13 @@ const MbMemoryGame = class {
49
49
  this.matched = createEvent(this, "matched");
50
50
  }
51
51
  tileGroups = [];
52
- reverseTile = { type: 'text', text: '?' };
53
- init = true;
54
- watchBoard() {
52
+ watchTileGroups() {
55
53
  if (this.init) {
56
54
  this.initGame();
57
55
  }
58
56
  }
57
+ reverseTile = { type: 'text', text: '?' };
58
+ init = true;
59
59
  tiles = [];
60
60
  selected = [];
61
61
  matchedTiles = [];
@@ -70,8 +70,8 @@ const MbMemoryGame = class {
70
70
  completed;
71
71
  matched;
72
72
  componentWillLoad() {
73
- console.log('componentDidLoad', this.init);
74
- if (this.init && this.tileGroups) {
73
+ console.log('componentDidLoad', this.init, this.tileGroups, this.reverseTile);
74
+ if (this.init && this.tileGroups.length) {
75
75
  this.initGame();
76
76
  }
77
77
  }
@@ -188,9 +188,9 @@ const MbMemoryGame = class {
188
188
  });
189
189
  }
190
190
  render() {
191
- return h("div", { key: 'bb7b898014606de493fd580e50ef685f529ac2bb', part: "container", style: {
191
+ return h("div", { key: '56287b90e45786be67ed85b8964bb99a6589f2b5', part: "container", style: {
192
192
  '--aspect': `${this.gridAspectRatio}`
193
- } }, h("div", { key: '0c677fb539e7af023c21411e70adf64050c97077', part: "list", style: {
193
+ } }, h("div", { key: 'cc965692a5523254c6930a26cb244a24fa7c05f7', part: "list", style: {
194
194
  '--cols': `${this.gridCols}`,
195
195
  '--rows': `${this.gridRows}`
196
196
  } }, this.tiles.map(t => {
@@ -198,8 +198,8 @@ const MbMemoryGame = class {
198
198
  })));
199
199
  }
200
200
  static get watchers() { return {
201
- "board": [{
202
- "watchBoard": 0
201
+ "tileGroups": [{
202
+ "watchTileGroups": 0
203
203
  }],
204
204
  "matchedTiles": [{
205
205
  "watchMatchedTiles": 0
@@ -17,5 +17,5 @@ var patchBrowser = () => {
17
17
 
18
18
  patchBrowser().then(async (options) => {
19
19
  await globalScripts();
20
- return bootstrapLazy([["mb-memory-game",[[513,"mb-memory-game",{"tileGroups":[16],"reverseTile":[16],"init":[4],"cols":[2],"tiles":[32],"selected":[32],"matchedTiles":[32],"uncoverSelected":[32],"initGame":[64]},null,{"board":[{"watchBoard":0}],"matchedTiles":[{"watchMatchedTiles":0}]}]]]], options);
20
+ return bootstrapLazy([["mb-memory-game",[[513,"mb-memory-game",{"tileGroups":[16],"reverseTile":[16],"init":[4],"cols":[2],"tiles":[32],"selected":[32],"matchedTiles":[32],"uncoverSelected":[32],"initGame":[64]},null,{"tileGroups":[{"watchTileGroups":0}],"matchedTiles":[{"watchMatchedTiles":0}]}]]]], options);
21
21
  });
@@ -1 +1 @@
1
- import{p as e,b as t}from"./p-D2pBOsDL.js";export{s as setNonce}from"./p-D2pBOsDL.js";import{g as a}from"./p-DQuL1Twl.js";(()=>{const s=import.meta.url,t={};return""!==s&&(t.resourcesUrl=new URL(".",s).href),e(t)})().then((async e=>(await a(),t([["p-35b15a0c",[[513,"mb-memory-game",{tileGroups:[16],reverseTile:[16],init:[4],cols:[2],tiles:[32],selected:[32],matchedTiles:[32],uncoverSelected:[32],initGame:[64]},null,{board:[{watchBoard:0}],matchedTiles:[{watchMatchedTiles:0}]}]]]],e))));
1
+ import{p as e,b as t}from"./p-D2pBOsDL.js";export{s as setNonce}from"./p-D2pBOsDL.js";import{g as i}from"./p-DQuL1Twl.js";(()=>{const s=import.meta.url,t={};return""!==s&&(t.resourcesUrl=new URL(".",s).href),e(t)})().then((async e=>(await i(),t([["p-eb79664b",[[513,"mb-memory-game",{tileGroups:[16],reverseTile:[16],init:[4],cols:[2],tiles:[32],selected:[32],matchedTiles:[32],uncoverSelected:[32],initGame:[64]},null,{tileGroups:[{watchTileGroups:0}],matchedTiles:[{watchMatchedTiles:0}]}]]]],e))));
@@ -0,0 +1 @@
1
+ import{h as t,r as e,c as i}from"./p-D2pBOsDL.js";function s({tile:e,selected:i,matched:s,uncovered:a,reversTile:h}){return t("div",{class:{"tile-container":!0,selected:i,matched:s}},t(r,{tile:a?e.data:h}))}function r({tile:e}){return"text"===e.type?t("button",{class:"tile tile-text"},e.text):"image"===e.type?t("button",{class:"tile tile-image"},t("img",{src:e.imageSrc})):void 0}const a=class{constructor(t){e(this,t),this.completed=i(this,"completed"),this.matched=i(this,"matched")}tileGroups=[];watchTileGroups(){this.init&&this.initGame()}reverseTile={type:"text",text:"?"};init=!0;tiles=[];selected=[];matchedTiles=[];watchMatchedTiles(){this.matchedTiles.length===this.tiles.length&&this.completed.emit()}cols;blockUI=!1;uncoverSelected=!1;completed;matched;componentWillLoad(){console.log("componentDidLoad",this.init,this.tileGroups,this.reverseTile),this.init&&this.tileGroups.length&&this.initGame()}slots=new Map;get gridCols(){return this.cols?this.cols:Math.floor(Math.sqrt(this.tiles.length))}get gridRows(){return Math.ceil(this.tiles.length/this.gridCols)}get gridAspectRatio(){return this.gridCols/this.gridRows}async initGame(t=this.tileGroups,e=this.reverseTile){console.log("initGame",t),this.reverseTile=e,this.buildTiles(t)}buildTiles(t){let e=[],i=0;t.forEach((t=>{const s=function(t,e){if(console.log("getSet",t,e),t.length<=0)throw new Error("getSet() - no items");if(t.length===e)return[...t];let i=[];for(;e-i.length>t.length;)i=[...i,...t];const s=new Set;for(;s.size<e-i.length;)s.add(t[Math.floor(Math.random()*t.length)]);return[...i,...s]}(t.tiles,t.count);e=[...e,...s.map((e=>({id:i++,data:{...e},groupId:t.id,uncovered:!1,matched:!1})))]})),this.tiles=e.sort((()=>Math.random()-.5))}async onTileClick(t){if(!this.blockUI)if(this.selected.includes(t))this.selected=this.selected.filter((e=>e!==t));else if(this.selected=[...this.selected,t],2===this.selected.length){const t=this.tiles.find((t=>t.id===this.selected[0])),e=this.tiles.find((t=>t.id===this.selected[1]));if(!t||!e)return;this.blockUI=!0,this.uncoverSelected=!0,t.groupId===e.groupId?(await Promise.all([this.markAsMatched(t.id),this.markAsMatched(e.id)]),this.matched.emit({t1:t.data,t2:e.data,groupId:t.groupId}),this.matchedTiles=[...this.matchedTiles,t.id,e.id]):await Promise.all([this.animateWrongMatch(t.id),this.animateWrongMatch(e.id)]),this.uncoverSelected=!1,this.blockUI=!1,this.selected=[]}}markAsMatched(t){return new Promise((e=>{const i=this.slots.get(t);if(!i)return;const s=i.children[0].animate([],{duration:1e3});s.addEventListener("finish",(()=>{e()})),s.addEventListener("cancel",(()=>{e()}))}))}animateWrongMatch(t){return new Promise((e=>{const i=this.slots.get(t);if(!i)return;const s=i.children[0].animate([{rotate:"0deg"},{rotate:"0deg"},{rotate:"30deg"},{rotate:"0deg"},{rotate:"-30deg"},{rotate:"0deg"},{rotate:"0deg"}],{duration:1500});s.addEventListener("finish",(()=>{e()})),s.addEventListener("cancel",(()=>{e()}))}))}render(){return t("div",{key:"56287b90e45786be67ed85b8964bb99a6589f2b5",part:"container",style:{"--aspect":`${this.gridAspectRatio}`}},t("div",{key:"cc965692a5523254c6930a26cb244a24fa7c05f7",part:"list",style:{"--cols":`${this.gridCols}`,"--rows":`${this.gridRows}`}},this.tiles.map((e=>t("div",{part:"tile-slot",onClick:()=>this.onTileClick(e.id),ref:t=>this.slots.set(e.id,t)},t(s,{tile:e,selected:this.selected.includes(e.id),matched:this.matchedTiles.includes(e.id),reversTile:this.reverseTile,uncovered:this.uncoverSelected&&this.selected.includes(e.id)}))))))}static get watchers(){return{tileGroups:[{watchTileGroups:0}],matchedTiles:[{watchMatchedTiles:0}]}}};a.style=":host{box-sizing:border-box;display:grid;place-content:center;width:100%;height:100%;container-type:size}[part=container]{width:min(100cqw, 100cqh / var(--aspect, 1));height:min(100cqw * var(--aspect, 1), 100cqh);container-type:size;background:pink}[part=list]{width:100cqw;height:100cqh;display:grid;grid-template-columns:repeat(var(--cols, 4), 1fr);grid-template-columns:repeat(var(--rows, 4), 1fr)}[part=tile-slot]{display:block;aspect-ratio:1;padding:5%}.tile-container{min-width:0;transition:scale 0.3s;width:100%;height:100%}.tile-container:hover{scale:1.05}.tile-container.selected{scale:1.1}.tile-container.matched{scale:0}.tile{border-radius:var(--tile-border-radius, 5px);border:var(--tile-border-width, 2px) solid var(--tile-border-color, black);padding:0;margin:0}.tile-text{width:100%;height:100%;aspect-ratio:1;background:#d9d9d9;display:grid;place-content:center}.tile-image{width:100%;height:100%}.tile-image img{display:block;width:100%;height:100%;object-fit:cover}";export{a as mb_memory_game}
@@ -2,9 +2,9 @@ import { EventEmitter } from "../../stencil-public-runtime";
2
2
  import { ITile, MatchedEvent, TileDefinition, TileGroup, TileID } from "./types";
3
3
  export declare class MbMemoryGame {
4
4
  tileGroups: TileGroup[];
5
+ watchTileGroups(): void;
5
6
  reverseTile: TileDefinition;
6
7
  init: boolean;
7
- watchBoard(): void;
8
8
  tiles: ITile[];
9
9
  selected: TileID[];
10
10
  matchedTiles: (string | number | null)[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mborecki/memory-game",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Sliding Puzzle",
@@ -40,8 +40,8 @@
40
40
  "tslib": "^2.8.1",
41
41
  "typescript": "5.9.3",
42
42
  "vitest": "4.0.16",
43
- "@mb-puzzle/grid": "0.2.2",
44
- "@mb-puzzle/vec2": "0.9.0"
43
+ "@mb-puzzle/vec2": "0.9.0",
44
+ "@mb-puzzle/grid": "0.2.2"
45
45
  },
46
46
  "license": "MIT",
47
47
  "scripts": {
@@ -1 +0,0 @@
1
- import{h as t,r as e,c as i}from"./p-D2pBOsDL.js";function s({tile:e,selected:i,matched:s,uncovered:a,reversTile:h}){return t("div",{class:{"tile-container":!0,selected:i,matched:s}},t(r,{tile:a?e.data:h}))}function r({tile:e}){return"text"===e.type?t("button",{class:"tile tile-text"},e.text):"image"===e.type?t("button",{class:"tile tile-image"},t("img",{src:e.imageSrc})):void 0}const a=class{constructor(t){e(this,t),this.completed=i(this,"completed"),this.matched=i(this,"matched")}tileGroups=[];reverseTile={type:"text",text:"?"};init=!0;watchBoard(){this.init&&this.initGame()}tiles=[];selected=[];matchedTiles=[];watchMatchedTiles(){this.matchedTiles.length===this.tiles.length&&this.completed.emit()}cols;blockUI=!1;uncoverSelected=!1;completed;matched;componentWillLoad(){console.log("componentDidLoad",this.init),this.init&&this.tileGroups&&this.initGame()}slots=new Map;get gridCols(){return this.cols?this.cols:Math.floor(Math.sqrt(this.tiles.length))}get gridRows(){return Math.ceil(this.tiles.length/this.gridCols)}get gridAspectRatio(){return this.gridCols/this.gridRows}async initGame(t=this.tileGroups,e=this.reverseTile){console.log("initGame",t),this.reverseTile=e,this.buildTiles(t)}buildTiles(t){let e=[],i=0;t.forEach((t=>{const s=function(t,e){if(console.log("getSet",t,e),t.length<=0)throw new Error("getSet() - no items");if(t.length===e)return[...t];let i=[];for(;e-i.length>t.length;)i=[...i,...t];const s=new Set;for(;s.size<e-i.length;)s.add(t[Math.floor(Math.random()*t.length)]);return[...i,...s]}(t.tiles,t.count);e=[...e,...s.map((e=>({id:i++,data:{...e},groupId:t.id,uncovered:!1,matched:!1})))]})),this.tiles=e.sort((()=>Math.random()-.5))}async onTileClick(t){if(!this.blockUI)if(this.selected.includes(t))this.selected=this.selected.filter((e=>e!==t));else if(this.selected=[...this.selected,t],2===this.selected.length){const t=this.tiles.find((t=>t.id===this.selected[0])),e=this.tiles.find((t=>t.id===this.selected[1]));if(!t||!e)return;this.blockUI=!0,this.uncoverSelected=!0,t.groupId===e.groupId?(await Promise.all([this.markAsMatched(t.id),this.markAsMatched(e.id)]),this.matched.emit({t1:t.data,t2:e.data,groupId:t.groupId}),this.matchedTiles=[...this.matchedTiles,t.id,e.id]):await Promise.all([this.animateWrongMatch(t.id),this.animateWrongMatch(e.id)]),this.uncoverSelected=!1,this.blockUI=!1,this.selected=[]}}markAsMatched(t){return new Promise((e=>{const i=this.slots.get(t);if(!i)return;const s=i.children[0].animate([],{duration:1e3});s.addEventListener("finish",(()=>{e()})),s.addEventListener("cancel",(()=>{e()}))}))}animateWrongMatch(t){return new Promise((e=>{const i=this.slots.get(t);if(!i)return;const s=i.children[0].animate([{rotate:"0deg"},{rotate:"0deg"},{rotate:"30deg"},{rotate:"0deg"},{rotate:"-30deg"},{rotate:"0deg"},{rotate:"0deg"}],{duration:1500});s.addEventListener("finish",(()=>{e()})),s.addEventListener("cancel",(()=>{e()}))}))}render(){return t("div",{key:"bb7b898014606de493fd580e50ef685f529ac2bb",part:"container",style:{"--aspect":`${this.gridAspectRatio}`}},t("div",{key:"0c677fb539e7af023c21411e70adf64050c97077",part:"list",style:{"--cols":`${this.gridCols}`,"--rows":`${this.gridRows}`}},this.tiles.map((e=>t("div",{part:"tile-slot",onClick:()=>this.onTileClick(e.id),ref:t=>this.slots.set(e.id,t)},t(s,{tile:e,selected:this.selected.includes(e.id),matched:this.matchedTiles.includes(e.id),reversTile:this.reverseTile,uncovered:this.uncoverSelected&&this.selected.includes(e.id)}))))))}static get watchers(){return{board:[{watchBoard:0}],matchedTiles:[{watchMatchedTiles:0}]}}};a.style=":host{box-sizing:border-box;display:grid;place-content:center;width:100%;height:100%;container-type:size}[part=container]{width:min(100cqw, 100cqh / var(--aspect, 1));height:min(100cqw * var(--aspect, 1), 100cqh);container-type:size;background:pink}[part=list]{width:100cqw;height:100cqh;display:grid;grid-template-columns:repeat(var(--cols, 4), 1fr);grid-template-columns:repeat(var(--rows, 4), 1fr)}[part=tile-slot]{display:block;aspect-ratio:1;padding:5%}.tile-container{min-width:0;transition:scale 0.3s;width:100%;height:100%}.tile-container:hover{scale:1.05}.tile-container.selected{scale:1.1}.tile-container.matched{scale:0}.tile{border-radius:var(--tile-border-radius, 5px);border:var(--tile-border-width, 2px) solid var(--tile-border-color, black);padding:0;margin:0}.tile-text{width:100%;height:100%;aspect-ratio:1;background:#d9d9d9;display:grid;place-content:center}.tile-image{width:100%;height:100%}.tile-image img{display:block;width:100%;height:100%;object-fit:cover}";export{a as mb_memory_game}