teacherGames/mj/main.js

113 lines
2.5 KiB
JavaScript

import Card from './card.js';
import Panel from './panel.js';
document.body.innerText = "";
const vocabulary = [
["drive", "drove"],
["send", "sent"],
["understand", "understood"],
["find", "found"],
["write", "wrote"],
["feel", "felt"],
["fall", "fell"],
["run", "ran"],
["do", "did"],
["speak", "spoke"],
["awake", "awoke"],
["bear", "bore"],
["bend", "bent"],
["bite", "bit"],
["cling", "clung"],
["breed", "bred"],
["feed", "fed"]
];
let deck = [];
let selected = null;
let panel = new Panel();
let field = document.createElement("div");
field.classList.add("fs");
field.style.position = "relative";
panel.append(document.body);
panel.installShuffle(reshuffle);
document.body.appendChild(field);
window.onresize = function() {
Card.setBounds(0, 0, field.offsetWidth, field.offsetHeight);
reshuffle();
}
Card.setBounds(0, 0, field.offsetWidth, field.offsetHeight);
for (let i = 0; i < 30; ++i) {
let id = Math.floor(Math.random() * vocabulary.length);
let set = vocabulary[id];
for (let s = 0; s < set.length; ++s) {
let card = new Card(set[s], onCardClick, id, s);
card.disrotate();
card.displace();
deck.push(card);
}
}
deck = shuffle(deck);
for (let i = 0; i < deck.length; ++i) {
field.appendChild(deck[i].element);
}
panel.startTimer();
function onCardClick(card) {
if (selected != null) {
if (card === selected) {
return;
}
if (selected.setId === card.setId && selected.cardId !== card.cardId) {
card.select();
score(selected, card);
selected = null;
return;
}
selected.unselect();
}
selected = card;
card.select();
}
function score(c1, c2) {
deck.splice(deck.indexOf(c1), 1)
deck.splice(deck.indexOf(c2), 1);
let x = window.innerWidth / 2
let y = -500;
c1.position(x, y);
c2.position(x, y);
c1.rotate(0);
c2.rotate(0);
setTimeout(fade.bind(null, c1, c2), 200);
}
function reshuffle() {
for (let i = 0; i < deck.length; ++i) {
deck[i].disrotate();
deck[i].displace();
}
}
function fade(c1, c2) {
c1.destructor();
c2.destructor();
}
function shuffle(deck) {
let spare = deck.slice();
let result = [];
while (spare.length > 0) {
let index = Math.floor(Math.random() * spare.length);
result.push(spare.splice(index, 1)[0]);
}
return result;
}