Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | // zakładamy takie utilsy
import type { Tile } from '@/components/GameCanvas/GameCanvas.types';
import { generateShuffledTiles } from '@/shared/utils/generateTiles';
import { onMounted, ref } from 'vue';
export function useDemoGame(onChange?: () => void) {
const tiles = ref<Tile[]>([]);
let difficulty = 0;
const flipTwoTiles = async () => {
const hidden = tiles.value.filter((t) => !t.flipped && !t.matched);
if (hidden.length < 2) return;
const [first, second] = hidden.sort(() => 0.5 - Math.random()).slice(0, 2);
first.flipped = true;
second.flipped = true;
await new Promise((r) => setTimeout(r, 1000));
if (first.name === second.name) {
first.matched = true;
second.matched = true;
} else {
first.flipped = false;
second.flipped = false;
}
};
const autoplay = async () => {
difficulty = difficulty + 1;
if (difficulty > 3) difficulty = 1;
onChange?.();
while (tiles.value.some((t) => !t.matched)) {
await flipTwoTiles();
await new Promise((r) => setTimeout(r, 600));
}
await new Promise((r) => setTimeout(r, 1000));
tiles.value = generateShuffledTiles('demo', difficulty);
onChange?.();
autoplay();
};
onMounted(() => {
tiles.value = generateShuffledTiles('demo', 1);
autoplay();
});
return { tiles };
}
|