-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectionScreen.js
75 lines (59 loc) · 2.05 KB
/
selectionScreen.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const $parent = document.querySelector('.parent');
const $player = document.querySelector('.player');
const createElement = (tag, className) => {
const $tag = document.createElement(tag);
if (className) {
if (Array.isArray(className)) {
className.forEach(item => {
$tag.classList.add(item);
})
} else {
$tag.classList.add(className);
}
}
return $tag;
}
function createEmptyPlayerBlock() {
const el = createElement('div', ['character', 'div11', 'disabled']);
const img = createElement('img');
img.src = 'https://reactmarathon-api.herokuapp.com/assets/mk/avatar/11.png';
el.appendChild(img);
$parent.appendChild(el);
}
async function init() {
localStorage.removeItem('player1');
const players = await fetch('https://reactmarathon-api.herokuapp.com/api/mk/players').then(res => res.json());
players[10].img = 'https://reactmarathon-api.herokuapp.com/assets/mk/fightingStance/subzero.gif';
let imgSrc = null;
createEmptyPlayerBlock();
players.forEach(item => {
const el = createElement('div', ['character', `div${item.id}`]);
const img = createElement('img');
el.addEventListener('mousemove', () => {
if (imgSrc === null) {
imgSrc = item.img;
const $img = createElement('img');
$img.src = imgSrc;
$player.appendChild($img);
}
});
el.addEventListener('mouseout', () => {
if (imgSrc) {
imgSrc = null;
$player.innerHTML = '';
}
});
el.addEventListener('click', () => {
localStorage.setItem('player1', JSON.stringify(item));
el.classList.add('active');
setTimeout(() => {
window.location.pathname = 'fightScreen/gameplay/index.html';
}, 1000);
});
img.src = item.avatar;
img.alt = item.name;
el.appendChild(img);
$parent.appendChild(el);
});
}
init();