-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
119 lines (103 loc) · 3.35 KB
/
script.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
// Selecting elements
const gameboard = document.querySelector('main');
const player0El = document.querySelector('.player--0');
const player1El = document.querySelector('.player--1');
const score0El = document.querySelector('#score--0');
const score1El = document.querySelector('#score--1');
const current0EL = document.querySelector('#current--0');
const current1EL = document.querySelector('#current--1');
const cubeEl = document.querySelector('.cube');
const faces = document.querySelectorAll('.face > img');
const btnNew = document.querySelector('.btn--new');
const btnRoll = document.querySelector('.btn--roll');
const btnHold = document.querySelector('.btn--hold');
const gameGoal = 100;
// Starting conditions
let scores = [],
currentScore,
activePlayer,
playing;
function init() {
scores = [0, 0];
currentScore = 0;
activePlayer = 0;
playing = true;
score0El.textContent = 0;
score1El.textContent = 0;
current0EL.textContent = 0;
current1EL.textContent = 0;
cubeEl.classList.remove('rolled');
player0El.classList.remove('player--winner');
player1El.classList.remove('player--winner');
player0El.classList.add('player--active');
player1El.classList.remove('player--active');
}
init();
function switchPlayer() {
document.querySelector(`#current--${activePlayer}`).textContent = 0;
activePlayer = activePlayer === 0 ? 1 : 0;
currentScore = 0;
player0El.classList.toggle('player--active');
player1El.classList.toggle('player--active');
}
btnRoll.addEventListener('click', () => {
if (playing && !cubeEl.classList.contains('rolled')) {
// Generate dice roll
const dice = Math.trunc(Math.random() * 6) + 1;
console.log(dice);
// Rolled dice animation
cubeEl.classList.add('rolled');
// Changing players
if (dice !== 1) {
// Add dice to current score
currentScore += dice;
document.querySelector(`#current--${activePlayer}`).textContent =
currentScore;
} else {
// Switch to next player
switchPlayer();
}
// Animation handlers
cubeEl.addEventListener('animationstart', event => {
if (event.animationName === 'landingDice') {
for (let i = 0; i < faces.length; ++i) {
faces[i].setAttribute('src', `img/dice-${dice}.png`);
}
gameboard.classList.add('shocked');
}
});
cubeEl.addEventListener('animationend', event => {
if (event.animationName === 'landingDice') {
cubeEl.classList.remove('rolled');
for (let i = 0; i < faces.length; ++i) {
faces[i].setAttribute('src', `img/dice-${i + 1}.png`);
}
gameboard.classList.remove('shocked');
}
});
}
});
btnHold.addEventListener('click', () => {
if (playing) {
// Add current score to active player's score
scores[activePlayer] += currentScore;
document.querySelector(`#score--${activePlayer}`).textContent =
scores[activePlayer];
// Check if player's score is at least 100
if (scores[activePlayer] >= gameGoal) {
// Finish game
playing = false;
document
.querySelector(`.player--${activePlayer}`)
.classList.add('player--winner');
document
.querySelector(`.player--${activePlayer}`)
.classList.remove('player--active');
} else {
// Change player
switchPlayer();
}
}
});
btnNew.addEventListener('click', init);