@mborecki/memory-game 0.4.0 → 0.4.1
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/dist/cjs/app-globals-V2Kpy_OQ.js +5 -0
- package/dist/cjs/index-D_ddvJ2f.js +3228 -0
- package/dist/cjs/index.cjs.js +2 -0
- package/dist/cjs/loader.cjs.js +13 -0
- package/dist/cjs/mb-memory-game.cjs.entry.js +217 -0
- package/dist/cjs/memory-game.cjs.js +25 -0
- package/dist/collection/collection-manifest.json +12 -0
- package/dist/collection/components/sliding-puzzle/memory-game.css +72 -0
- package/dist/collection/components/sliding-puzzle/memory-game.js +381 -0
- package/dist/collection/components/sliding-puzzle/tile.js +16 -0
- package/dist/collection/components/sliding-puzzle/types.js +1 -0
- package/dist/collection/index.js +1 -0
- package/dist/collection/utils/get-set.js +23 -0
- package/dist/collection/utils/get-set.test.js +31 -0
- package/dist/components/index.js +1 -0
- package/dist/components/mb-memory-game.js +1 -0
- package/dist/esm/app-globals-DQuL1Twl.js +3 -0
- package/dist/esm/index-D2pBOsDL.js +3221 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/loader.js +11 -0
- package/dist/{memory-game → esm}/mb-memory-game.entry.js +5 -8
- package/dist/esm/memory-game.js +21 -0
- package/dist/index.cjs.js +1 -0
- package/dist/index.js +1 -0
- package/dist/memory-game/index.esm.js +0 -4
- package/dist/memory-game/memory-game.esm.js +1 -50
- package/dist/memory-game/p-D2pBOsDL.js +2 -0
- package/dist/memory-game/p-DQuL1Twl.js +1 -0
- package/dist/memory-game/p-f3557562.entry.js +1 -0
- package/dist/vitest.config.js +4 -0
- package/package.json +1 -1
- package/dist/memory-game/index-Br8RptzJ.js +0 -6135
- package/dist/memory-game/index-Br8RptzJ.js.map +0 -1
- package/dist/memory-game/index.esm.js.map +0 -1
- package/dist/memory-game/mb-memory-game.entry.js.map +0 -1
- package/dist/memory-game/memory-game.esm.js.map +0 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var index = require('./index-D_ddvJ2f.js');
|
|
4
|
+
var appGlobals = require('./app-globals-V2Kpy_OQ.js');
|
|
5
|
+
|
|
6
|
+
const defineCustomElements = async (win, options) => {
|
|
7
|
+
if (typeof window === 'undefined') return undefined;
|
|
8
|
+
await appGlobals.globalScripts();
|
|
9
|
+
return index.bootstrapLazy([["mb-memory-game.cjs",[[513,"mb-memory-game",{"tileGroups":[16],"reverseTile":[16],"init":[4],"cols":[2],"keepMatched":[4,"keep-matched"],"tiles":[32],"selected":[32],"matchedTiles":[32],"lastMatched":[32],"initGame":[64]},null,{"tileGroups":[{"watchTileGroups":0}],"matchedTiles":[{"watchMatchedTiles":0}]}]]]], options);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
exports.setNonce = index.setNonce;
|
|
13
|
+
exports.defineCustomElements = defineCustomElements;
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var index = require('./index-D_ddvJ2f.js');
|
|
4
|
+
|
|
5
|
+
function getSet(items, count) {
|
|
6
|
+
console.log('getSet', items, count);
|
|
7
|
+
if (items.length <= 0)
|
|
8
|
+
throw new Error("getSet() - no items");
|
|
9
|
+
if (items.length === count) {
|
|
10
|
+
return [...items];
|
|
11
|
+
}
|
|
12
|
+
let result = [];
|
|
13
|
+
while (count - result.length > items.length) {
|
|
14
|
+
result = [
|
|
15
|
+
...result,
|
|
16
|
+
...items
|
|
17
|
+
];
|
|
18
|
+
}
|
|
19
|
+
const rest = new Set();
|
|
20
|
+
while (rest.size < count - result.length) {
|
|
21
|
+
rest.add(items[Math.floor(Math.random() * items.length)]);
|
|
22
|
+
}
|
|
23
|
+
return [
|
|
24
|
+
...result,
|
|
25
|
+
...rest
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function Tile({ tile, selected, matched, uncovered, reversTile }) {
|
|
30
|
+
return index.h("div", { part: "tile", class: {
|
|
31
|
+
'tile-container': true,
|
|
32
|
+
selected,
|
|
33
|
+
matched
|
|
34
|
+
} }, index.h(TileRender, { tile: uncovered ? tile.data : reversTile }));
|
|
35
|
+
}
|
|
36
|
+
function TileRender({ tile }) {
|
|
37
|
+
if (tile.type === 'text') {
|
|
38
|
+
return index.h("button", { class: "tile tile-text" }, tile.text);
|
|
39
|
+
}
|
|
40
|
+
if (tile.type === 'image') {
|
|
41
|
+
return index.h("button", { class: "tile tile-image" }, index.h("img", { src: tile.imageSrc }));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const memoryGameCss = () => `: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}[part=list]{width:100cqw;height:100cqh;display:grid;grid-template-columns:repeat(var(--cols, 4), 1fr);grid-template-rows: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);overflow:hidden;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}`;
|
|
46
|
+
|
|
47
|
+
const MbMemoryGame = class {
|
|
48
|
+
constructor(hostRef) {
|
|
49
|
+
index.registerInstance(this, hostRef);
|
|
50
|
+
this.completed = index.createEvent(this, "completed");
|
|
51
|
+
this.matched = index.createEvent(this, "matched");
|
|
52
|
+
}
|
|
53
|
+
tileGroups = [];
|
|
54
|
+
watchTileGroups() {
|
|
55
|
+
console.log('watchTileGroups');
|
|
56
|
+
if (this.init) {
|
|
57
|
+
this.initGame();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
reverseTile = { type: 'text', text: '?' };
|
|
61
|
+
init = true;
|
|
62
|
+
tiles = [];
|
|
63
|
+
selected = [];
|
|
64
|
+
matchedTiles = [];
|
|
65
|
+
watchMatchedTiles() {
|
|
66
|
+
if (this.matchedTiles.length === this.tiles.length) {
|
|
67
|
+
this.completed.emit();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
cols;
|
|
71
|
+
blockUI = false;
|
|
72
|
+
keepMatched = false;
|
|
73
|
+
completed;
|
|
74
|
+
matched;
|
|
75
|
+
componentWillLoad() {
|
|
76
|
+
console.log('componentDidLoad', this.init, this.tileGroups, this.reverseTile);
|
|
77
|
+
if (this.init && this.tileGroups.length) {
|
|
78
|
+
this.initGame();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
slots = new Map();
|
|
82
|
+
lastMatched = null;
|
|
83
|
+
get gridCols() {
|
|
84
|
+
if (this.cols) {
|
|
85
|
+
return this.cols;
|
|
86
|
+
}
|
|
87
|
+
return Math.floor(Math.sqrt(this.tiles.length));
|
|
88
|
+
}
|
|
89
|
+
get gridRows() {
|
|
90
|
+
return Math.ceil(this.tiles.length / this.gridCols);
|
|
91
|
+
}
|
|
92
|
+
get gridAspectRatio() {
|
|
93
|
+
return this.gridRows / this.gridCols;
|
|
94
|
+
}
|
|
95
|
+
async initGame(tileGroups = this.tileGroups, reverseTile = this.reverseTile) {
|
|
96
|
+
console.log('initGame', tileGroups);
|
|
97
|
+
this.reverseTile = reverseTile;
|
|
98
|
+
this.buildTiles(tileGroups);
|
|
99
|
+
}
|
|
100
|
+
buildTiles(tileGroups) {
|
|
101
|
+
let result = [];
|
|
102
|
+
let index = 0;
|
|
103
|
+
tileGroups.forEach(tg => {
|
|
104
|
+
const tiles = getSet(tg.tiles, tg.count);
|
|
105
|
+
result = [
|
|
106
|
+
...result,
|
|
107
|
+
...tiles.map(t => {
|
|
108
|
+
return {
|
|
109
|
+
id: index++,
|
|
110
|
+
data: { ...t },
|
|
111
|
+
groupId: tg.id,
|
|
112
|
+
uncovered: false,
|
|
113
|
+
matched: false
|
|
114
|
+
};
|
|
115
|
+
})
|
|
116
|
+
];
|
|
117
|
+
});
|
|
118
|
+
this.tiles = result.sort(() => Math.random() - .5);
|
|
119
|
+
}
|
|
120
|
+
async onTileClick(id) {
|
|
121
|
+
if (this.blockUI)
|
|
122
|
+
return;
|
|
123
|
+
this.lastMatched = null;
|
|
124
|
+
if (this.selected.includes(id)) {
|
|
125
|
+
this.selected = this.selected.filter(i => i !== id);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
this.selected = [...this.selected, id];
|
|
129
|
+
if (this.selected.length === 2) {
|
|
130
|
+
const t1 = this.tiles.find(t => t.id === this.selected[0]);
|
|
131
|
+
const t2 = this.tiles.find(t => t.id === this.selected[1]);
|
|
132
|
+
if (!t1 || !t2)
|
|
133
|
+
return;
|
|
134
|
+
this.blockUI = true;
|
|
135
|
+
if (t1.groupId === t2.groupId) {
|
|
136
|
+
await Promise.all([
|
|
137
|
+
this.markAsMatched(t1.id),
|
|
138
|
+
this.markAsMatched(t2.id)
|
|
139
|
+
]);
|
|
140
|
+
this.matched.emit({ t1: t1.data, t2: t2.data, groupId: t1.groupId });
|
|
141
|
+
if (this.keepMatched) {
|
|
142
|
+
this.lastMatched = t1.groupId;
|
|
143
|
+
}
|
|
144
|
+
this.matchedTiles = [
|
|
145
|
+
...this.matchedTiles, t1.id, t2.id
|
|
146
|
+
];
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
await Promise.all([
|
|
150
|
+
this.animateWrongMatch(t1.id),
|
|
151
|
+
this.animateWrongMatch(t2.id)
|
|
152
|
+
]);
|
|
153
|
+
}
|
|
154
|
+
this.blockUI = false;
|
|
155
|
+
this.selected = [];
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
markAsMatched(id) {
|
|
159
|
+
return new Promise(resolve => {
|
|
160
|
+
const slot = this.slots.get(id);
|
|
161
|
+
if (!slot)
|
|
162
|
+
return;
|
|
163
|
+
const tile = slot.children[0];
|
|
164
|
+
const anim = tile.animate([], { duration: 1000 });
|
|
165
|
+
anim.addEventListener('finish', () => {
|
|
166
|
+
resolve();
|
|
167
|
+
});
|
|
168
|
+
anim.addEventListener('cancel', () => {
|
|
169
|
+
resolve();
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
animateWrongMatch(id) {
|
|
174
|
+
return new Promise(resolve => {
|
|
175
|
+
const slot = this.slots.get(id);
|
|
176
|
+
if (!slot)
|
|
177
|
+
return;
|
|
178
|
+
const tile = slot.children[0];
|
|
179
|
+
const anim = tile.animate([
|
|
180
|
+
{ rotate: "0deg" },
|
|
181
|
+
{ rotate: "0deg" },
|
|
182
|
+
{ rotate: "30deg" },
|
|
183
|
+
{ rotate: "0deg" },
|
|
184
|
+
{ rotate: "-30deg" },
|
|
185
|
+
{ rotate: "0deg" },
|
|
186
|
+
{ rotate: "0deg" },
|
|
187
|
+
], { duration: 1500 });
|
|
188
|
+
anim.addEventListener('finish', () => {
|
|
189
|
+
resolve();
|
|
190
|
+
});
|
|
191
|
+
anim.addEventListener('cancel', () => {
|
|
192
|
+
resolve();
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
render() {
|
|
197
|
+
return index.h("div", { key: '0919333971e5a2a625da795a16243209a4d4f861', part: "container", style: {
|
|
198
|
+
'--aspect': `${this.gridAspectRatio}`
|
|
199
|
+
} }, index.h("div", { key: 'fcfba146bb8304d9da1a749509bb84a5650d08e7', part: "list", style: {
|
|
200
|
+
'--cols': `${this.gridCols}`,
|
|
201
|
+
'--rows': `${this.gridRows}`
|
|
202
|
+
} }, this.tiles.map(t => {
|
|
203
|
+
return index.h("div", { part: "tile-slot", onClick: () => this.onTileClick(t.id), ref: el => this.slots.set(t.id, el) }, index.h(Tile, { tile: t, selected: this.selected.includes(t.id) || t.groupId === this.lastMatched, matched: this.matchedTiles.includes(t.id) && t.groupId !== this.lastMatched, reversTile: this.reverseTile, uncovered: this.selected.includes(t.id) || t.groupId === this.lastMatched }));
|
|
204
|
+
})));
|
|
205
|
+
}
|
|
206
|
+
static get watchers() { return {
|
|
207
|
+
"tileGroups": [{
|
|
208
|
+
"watchTileGroups": 0
|
|
209
|
+
}],
|
|
210
|
+
"matchedTiles": [{
|
|
211
|
+
"watchMatchedTiles": 0
|
|
212
|
+
}]
|
|
213
|
+
}; }
|
|
214
|
+
};
|
|
215
|
+
MbMemoryGame.style = memoryGameCss();
|
|
216
|
+
|
|
217
|
+
exports.mb_memory_game = MbMemoryGame;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var index = require('./index-D_ddvJ2f.js');
|
|
4
|
+
var appGlobals = require('./app-globals-V2Kpy_OQ.js');
|
|
5
|
+
|
|
6
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
7
|
+
/*
|
|
8
|
+
Stencil Client Patch Browser v4.41.0 | MIT Licensed | https://stenciljs.com
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
var patchBrowser = () => {
|
|
12
|
+
const importMeta = (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('memory-game.cjs.js', document.baseURI).href));
|
|
13
|
+
const opts = {};
|
|
14
|
+
if (importMeta !== "") {
|
|
15
|
+
opts.resourcesUrl = new URL(".", importMeta).href;
|
|
16
|
+
}
|
|
17
|
+
return index.promiseResolve(opts);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
patchBrowser().then(async (options) => {
|
|
21
|
+
await appGlobals.globalScripts();
|
|
22
|
+
return index.bootstrapLazy([["mb-memory-game.cjs",[[513,"mb-memory-game",{"tileGroups":[16],"reverseTile":[16],"init":[4],"cols":[2],"keepMatched":[4,"keep-matched"],"tiles":[32],"selected":[32],"matchedTiles":[32],"lastMatched":[32],"initGame":[64]},null,{"tileGroups":[{"watchTileGroups":0}],"matchedTiles":[{"watchMatchedTiles":0}]}]]]], options);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
exports.setNonce = index.setNonce;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
:host {
|
|
2
|
+
box-sizing: border-box;
|
|
3
|
+
display: grid;
|
|
4
|
+
place-content: center;
|
|
5
|
+
width: 100%;
|
|
6
|
+
height: 100%;
|
|
7
|
+
container-type: size;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
[part=container] {
|
|
11
|
+
width: min(100cqw, 100cqh / var(--aspect, 1));
|
|
12
|
+
height: min(100cqw * var(--aspect, 1), 100cqh);
|
|
13
|
+
container-type: size;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
[part=list] {
|
|
17
|
+
width: 100cqw;
|
|
18
|
+
height: 100cqh;
|
|
19
|
+
display: grid;
|
|
20
|
+
grid-template-columns: repeat(var(--cols, 4), 1fr);
|
|
21
|
+
grid-template-rows: repeat(var(--rows, 4), 1fr);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
[part=tile-slot] {
|
|
25
|
+
display: block;
|
|
26
|
+
aspect-ratio: 1;
|
|
27
|
+
padding: 5%;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
.tile-container {
|
|
31
|
+
min-width: 0;
|
|
32
|
+
transition: scale 0.3s;
|
|
33
|
+
width: 100%;
|
|
34
|
+
height: 100%;
|
|
35
|
+
}
|
|
36
|
+
.tile-container:hover {
|
|
37
|
+
scale: 1.05;
|
|
38
|
+
}
|
|
39
|
+
.tile-container.selected {
|
|
40
|
+
scale: 1.1;
|
|
41
|
+
}
|
|
42
|
+
.tile-container.matched {
|
|
43
|
+
scale: 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.tile {
|
|
47
|
+
border-radius: var(--tile-border-radius, 5px);
|
|
48
|
+
border: var(--tile-border-width, 2px) solid var(--tile-border-color, black);
|
|
49
|
+
overflow: hidden;
|
|
50
|
+
padding: 0;
|
|
51
|
+
margin: 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
.tile-text {
|
|
55
|
+
width: 100%;
|
|
56
|
+
height: 100%;
|
|
57
|
+
aspect-ratio: 1;
|
|
58
|
+
background: #d9d9d9;
|
|
59
|
+
display: grid;
|
|
60
|
+
place-content: center;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.tile-image {
|
|
64
|
+
width: 100%;
|
|
65
|
+
height: 100%;
|
|
66
|
+
}
|
|
67
|
+
.tile-image img {
|
|
68
|
+
display: block;
|
|
69
|
+
width: 100%;
|
|
70
|
+
height: 100%;
|
|
71
|
+
object-fit: cover;
|
|
72
|
+
}
|