-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
290 lines (253 loc) · 8.04 KB
/
app.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
window.onload = beginningAnimation();
let computerSelection;
let playerSelection;
let computerScore = 0;
let playerScore = 0;
let buttons = document.querySelectorAll(".button");
const body = document.querySelector("body");
const main = document.querySelector("main");
const endAlrt = document.querySelector("#end-alert");
const endDesc = document.querySelector("#end-desc");
const returnMainBtn = document.querySelector("#retry-btn");
const desc = document.querySelector("#desc3");
const container = document.querySelector("#results-container");
body.addEventListener("click", skipAnime());
body.addEventListener("keydown", skipAnime());
function skipAnime() {
const span = document.querySelectorAll("span");
span.forEach((span) => span.classList.add("skip"));
}
function beginningAnimation() {
fadeIn();
//need to turn nodelist of spans into an array so we can access last value for ontransitionend
const desc1 = document.querySelector("#desc1");
let desc1Span = desc1.querySelectorAll("span");
desc1Span = Array.from(desc1Span);
const desc2 = document.querySelector("#desc2");
const desc3 = document.querySelector("#desc3");
desc1Span[desc1Span.length - 1].ontransitionend = () => {
desc1.classList.add("fade-out");
desc1.addEventListener("animationend", () => {
desc1.classList.add("disappear");
desc1.classList.remove("animate");
desc2.classList.remove("disappear");
desc2.classList.add("animate");
fadeIn();
/* need to collect nodelist of span
in the same function we activate fadein()
or else nodelist will be empty */
let desc2Span = desc2.querySelectorAll("span");
desc2Span = Array.from(desc2Span);
desc2Span[desc2Span.length - 1].ontransitionend = () => {
desc2.classList.add("fade-out");
desc2.addEventListener("animationend", () => {
desc2.classList.add("disappear");
desc2.classList.remove("animate");
desc3.classList.remove("disappear");
desc3.classList.add("animate");
fadeIn();
let desc3Span = desc3.querySelectorAll("span");
desc3Span = Array.from(desc3Span);
desc3Span[desc3Span.length - 1].ontransitionend = () => {
const cta = document.querySelector("#cta");
cta.classList.add("drop-down");
cta.addEventListener("animationend", () => {
const gameCtn = document.querySelector("#game-container");
setTimeout(function () {
gameCtn.classList.add("fade-in");
}, 300);
});
};
});
};
});
};
}
function fadeIn() {
let text = document.querySelector(".animate");
let strText = text.textContent;
let splitText = strText.split("");
text.textContent = "";
//append span tags to each character in the string
for (i = 0; i < splitText.length; i++) {
text.innerHTML += `<span>${splitText[i]}</span>`;
}
let char = 0;
let timer = setInterval(onTick, 50);
function onTick() {
const span = text.querySelectorAll("span")[char];
span.classList.add("fade");
char++;
//stops the function from running once the end of the string has been reached
if (char === splitText.length) {
complete();
return;
}
}
function complete() {
clearInterval(timer);
timer = null;
}
}
buttons.forEach((button) => {
button.addEventListener("click", () => {
const img = button.querySelector("img");
playerSelection = img.alt.toLowerCase();
playRound(playerSelection, computerSelection);
if (playerScore === 5 || computerScore === 5) {
declareWinner();
}
});
});
const myArray = ["Rock", "Paper", "Scissors"];
function computerPlay() {
return myArray[~~(Math.random() * myArray.length)];
}
function playRound(playerSelection, computerSelection) {
computerSelection = computerPlay().toLowerCase();
playerSelection = playerSelection.toLowerCase();
if (computerSelection == playerSelection) {
displayResults("Tie game!");
} else if (
(computerSelection == "rock" && playerSelection == "scissors") ||
(computerSelection == "scissors" && playerSelection == "paper") ||
(computerSelection == "paper" && playerSelection == "rock")
) {
computerScore = ++computerScore;
keepCpuScore();
if (computerScore === 1) {
displayResults(
`Oh no! You lost.
${capitalize(computerSelection)} beats ${playerSelection}.`
);
} else if (computerScore === 2) {
displayResults(
`Arghh. ${capitalize(
computerSelection
)} beats ${playerSelection}. Give it another shot!`
);
} else if (computerScore === 3) {
displayResults(
`${capitalize(
computerSelection
)} beats ${playerSelection}. It's ok. You got this!!`
);
} else if (computerScore === 4) {
displayResults(
`Oh no. It's match point!! ${capitalize(
computerSelection
)} beats ${playerSelection}. Don't let us down!`
);
} else {
displayResults(`${computerSelection} beats ${playerSelection}`);
}
} else {
playerScore = ++playerScore;
keepPlayerScore();
if (playerScore === 1) {
displayResults(
`Lets go!!! You won.
${capitalize(playerSelection)} beats ${computerSelection}.`
);
} else if (playerScore === 2) {
displayResults(
`You're pretty good at this. ${capitalize(
playerSelection
)} beats ${computerSelection}.`
);
} else if (playerScore === 3) {
displayResults(
`${capitalize(
playerSelection
)} beats ${computerSelection}! Has mankind found its savior??`
);
} else if (playerScore === 4) {
displayResults(
`${capitalize(
playerSelection
)} beats ${computerSelection}. One more and you're a hero!`
);
} else {
displayResults(`${playerSelection} beats ${computerSelection}`);
}
}
}
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function displayResults(str) {
container.animate([{ opacity: 0 }, { opacity: 1 }], {
duration: 300,
fill: "forwards",
iterations: 1,
delay: 0,
easing: "ease-out",
});
container.textContent = str;
}
function declareWinner() {
rplContent();
if (playerScore > computerScore) {
endDesc.textContent = "You win! Mankind lives another day!!";
returnMainBtn.innerText = "Play Again";
} else {
endDesc.textContent = "You lost...who will save mankind now?";
returnMainBtn.innerText = "Try Again?";
}
fadeIn();
let endDescSpan = endDesc.querySelectorAll("span");
endDescSpan = Array.from(endDescSpan);
endDescSpan[endDescSpan.length - 1].ontransitionend = () => {
returnMainBtn.classList.add("fade-in");
/*returnMainBtn.animate([{ opacity: 0 }, { opacity: 1 }], {
duration: 00,
fill: "forwards",
iterations: 1,
delay: 0,
easing: "ease-in",
});*/
};
}
function rplContent() {
main.classList.add("disappear");
endAlrt.classList.remove("disappear");
desc.classList.remove("animate");
endDesc.classList.add("animate");
returnMainBtn.addEventListener("click", () => {
main.classList.remove("disappear");
endAlrt.classList.add("disappear");
desc.classList.add("animate");
returnMainBtn.classList.remove("fade-in");
resetGame();
});
}
function resetGame() {
fadeIn();
container.textContent = "";
playerScore = 0;
computerScore = 0;
keepPlayerScore();
keepCpuScore();
}
function keepPlayerScore() {
let playerScoreBox = document.querySelector("#player-score");
playerScoreBox.animate([{ opacity: 0 }, { opacity: 1 }], {
duration: 300,
fill: "forwards",
iterations: 1,
delay: 0,
easing: "ease-out",
});
playerScoreBox.textContent = playerScore;
}
function keepCpuScore() {
let computerScoreBox = document.querySelector("#computer-score");
computerScoreBox.animate([{ opacity: 0 }, { opacity: 1 }], {
duration: 300,
fill: "forwards",
iterations: 1,
delay: 0,
easing: "ease-out",
});
computerScoreBox.textContent = computerScore;
}