-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript.js
More file actions
414 lines (388 loc) · 12.5 KB
/
script.js
File metadata and controls
414 lines (388 loc) · 12.5 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
var spans = 0, bossHp, myHp, time, mode, timemode, totaltime, damage, hit, correct, combo, score, myTime, gameend;
var words = document.querySelector(".words");
var health = document.querySelector(".health");
var myhealth = document.querySelector(".myhealth");
var loadingstart = document.getElementById("loadingstart");
var presshere = document.getElementById("presshere");
var contact = document.getElementById("contact");
var load = document.getElementById("load");
var loadsuccess = document.getElementById("loadsuccess");
var mainmenu = document.getElementById("mainmenu");
var preload = document.getElementById("preload");
var level = document.getElementById("level");
var statusmode = document.getElementById("statusmode");
var startdelay = document.getElementById("startdelay");
var game = document.getElementById("game");
var gamewin = document.getElementById("gamewin");
var gameover = document.getElementById("gameover");
var gamebox = document.getElementById("gamebox");
var monster_start = document.getElementById("monster_start");
var monster_die = document.getElementById("monster_die");
var myword = document.getElementById("myword");
var mytranslate = document.getElementById("mytranslate");
var timestatus = document.getElementById("timestatus");
var updatecombo = document.getElementById("updatecombo");
var updatescore = document.getElementById("updatescore");
var scoreboard = document.getElementById("score");
var s_click = new Audio("./sound/1.click.wav");
var s_countdown = new Audio("./sound/2.countdown.wav");
var s_press = new Audio("./sound/3.press.wav");
var s_damage = new Audio("./sound/4.damage.mp3");
var s_hurt = new Audio("./sound/5.hurt.wav");
var s_die = new Audio("./sound/6.die.wav");
var s_win = new Audio("./sound/7.win.mp3");
var s_gameover = new Audio("./sound/8.gameover.wav");
var s_miss = new Audio("./sound/9.missed.wav");
var s_music = new Audio("./sound/10.music.mp3");
var s_coupe = new Audio("./sound/11.coupe.mp3");
//Change mode
function rightChange() {
mode = statusmode.getAttribute('mode');
if(mode == 'easy') {
statusmode.setAttribute('mode','medium');
statusmode.innerHTML = "Medium";
}
if(mode == 'medium') {
statusmode.setAttribute('mode','hard');
statusmode.innerHTML = "Hard";
}
if(mode == 'hard') {
statusmode.setAttribute('mode','expert');
statusmode.innerHTML = "Expert";
}
if(mode == 'expert') {
statusmode.setAttribute('mode','easy');
statusmode.innerHTML = "Easy";
}
soundClick();
}
//Set Game
function setGame() {
mymonster = Math.floor(Math.random()*1000)%6;
bossHp = 100;
health.style.width = 100 + "%";
myHp = 100;
myhealth.style.width = 100 + "%";
hit = 0;
combo = 0;
score = 0;
gameend = false;
updatecombo.style.color = "white";
updatecombo.innerHTML = 0;
updatescore.innerHTML = 0;
monster_start.src = "./img/Mons" + mymonster + ".gif";
monster_die.src = "./img/Mons" + mymonster + "-die.gif";
monster_die.style.display = "none";
monster_start.style.display = "block";
myword.style.display = "block";
timestatus.style.display = "block";
mytranslate.style.display = "block";
}
//Set Mode Game
function setMode(mode) {
mode = (mode == undefined) ? "easy": (mode == "easy") ? "medium": (mode == "medium") ? "hard": (mode == "hard") ? "expert": "easy";
if(mode == "easy") {
timemode = 4;
damage = 10;
}
else if(mode == "medium") {
timemode = 3;
damage = 15;
}
else if(mode == "hard") {
timemode = 2;
damage = 20;
}
else if(mode == "expert") {
timemode = 1;
damage = 25;
}
totaltime = timemode;
soundCountdown();
delayStart();
}
//Random Word
function random() {
words.innerHTML = "";
correct = 0;
var random = Math.floor(Math.random()*100000)%3000;
var wordArray = wordlist[random].split("");
for (var i = 0; i < wordArray.length; i++) {
var span = document.createElement("span");
span.classList.add("span");
span.innerHTML = wordArray[i];
words.appendChild(span);
}
spans = document.querySelectorAll(".span");
mytranslate.innerHTML = thailist[random];
totaltime = timemode*Math.floor(wordArray.length/3);
if(totaltime == 0) {
totaltime = timemode;
}
}
//My typing
function typing(e) {
typed = String.fromCharCode(e.which);
if (gameend) {
return;
}
for (var i = 0; i < spans.length; i++) {
if (spans[i].innerHTML === typed) { // if typed letter is the one from the word
if (spans[i].classList.contains("bg")) { // if it already has class with the bacground color then check the next one
continue;
} else if (spans[i].classList.contains("bg") === false && spans[i-1] === undefined || spans[i-1].classList.contains("bg") !== false ) { // if it dont have class, if it is not first letter or if the letter before it dont have class (this is done to avoid marking the letters who are not in order for being checked, for example if you have two "A"s so to avoid marking both of them if the first one is at the index 0 and second at index 5 for example)
spans[i].classList.add("bg");
soundPress();
game.style.borderColor = "#8fff86";
game.style.boxShadow = "0px 0px 40px #42f403";
combo += 1;
correct += 1;
updatecombo.innerHTML = combo;
setTimeout(function() {
game.style.borderColor = "#86d9ff";
game.style.boxShadow = "0px 0px 20px #03A9F4";
}, 1000);
break;
}
}
else if (spans[i].innerHTML !== typed) {
if (spans[i].classList.contains("bg")) { // if it already has class with the bacground color then check the next one
continue;
} else if (spans[i].classList.contains("bg") === false && spans[i-1] === undefined || spans[i-1].classList.contains("bg") !== false ) { // if it dont have class, if it is not first letter or if the letter before it dont have class (this is done to avoid marking the letters who are not in order for being checked, for example if you have two "A"s so to avoid marking both of them if the first one is at the index 0 and second at index 5 for example)
soundMiss();
game.style.borderColor = "#ff8686";
game.style.boxShadow = "0px 0px 40px #f40303";
combo = 0;
setTimeout(function() {
game.style.borderColor = "#86d9ff";
game.style.boxShadow = "0px 0px 20px #03A9F4";
}, 1000);
break;
}
}
}
var checker = 0;
for (var j = 0; j < spans.length; j++) { //checking if all the letters are typed
if (spans[j].className === "span bg") {
checker++;
}
if (checker === spans.length) {
hit = 1;
score += Math.floor(correct*(Math.floor(combo/10)*0.5 + 1));
updatescore.innerHTML = score;
document.removeEventListener("keydown", typing, false);
setTimeout(function(){
words.className = "words"; // restart the classes
soundDamage();
random(); // give another word
time = totaltime + 1;
hit = 0;
document.addEventListener("keydown", typing, false);
}, 400);
bossHp -= 10;
health.style.width = bossHp + "%";
}
}
hue = Math.floor(combo/10)*30;
if (hue == 0) {
updatecombo.style.color = "white";
}
else {
updatecombo.style.color = "hsl("+hue+", 100%, 50%)";
}
updatecombo.innerHTML = combo;
}
//Check Hp
function check() {
if (bossHp <= 0) {
s_coupe.pause();
soundDie();
monster_start.style.display = "none";
myword.style.display = "none";
timestatus.style.display = "none";
mytranslate.style.display = "none";
monster_die.style.display = "block";
gameend = true;
clearInterval(cd);
setTimeout(function() {
soundWin();
game.style.display = "none";
gamewin.style.display = "block";
}, 4000);
bossHp = 100;
}
else if (myHp <= 0) {
s_coupe.pause();
soundGameover();
game.style.display = "none";
mytranslate.style.display = "none";
gameover.style.display = "block";
gameend = true;
clearInterval(cd);
myHp = 100;
}
requestAnimationFrame(check);
}
//Time
function countdown() {
time = totaltime;
cd = setInterval(
function(){
if (time >= 0) {
time--;
updateTime();
}
else{
clearInterval(cd);
}
}
,1000);
}
//Show time and check time = 0 to random new word
function updateTime() {
theTime.innerText = time;
if (time <= 0 && hit == 0) {
myHp -= damage;
myhealth.style.width = myHp + "%";
combo = 0;
updatecombo.innerHTML = combo;
updatecombo.style.color = "white";
soundHurt();
random();
time = totaltime+1;
}
}
//Set to levelselect
function levelSelect() {
mainmenu.style.display = "none";
game.style.display = "none";
gamebox.style.display = "none";
startdelay.style.display = "none";
gamewin.style.display = "none";
gameover.style.display = "none";
scoreboard.style.display = "none";
level.style.display = "block";
soundClick();
soundCoupe();
}
//Delay before start
function delayStart() {
var firstdelay = 2;
level.style.display = "none";
mainmenu.style.display = "none";
gamebox.style.display = "none";
gamewin.style.display = "none";
gameover.style.display = "none";
game.style.display = "block";
startdelay.style.display = "block";
ds = setInterval(
function(){
if (firstdelay <= 0) {
clearInterval(ds);
startGame();
startdelay.style.display = "none";
gamebox.style.display = "block";
startdelay.innerText = 3;
}
else{
soundCountdown();
startdelay.innerText = firstdelay;
firstdelay -= 1;
}
}
,1000);
}
//StartGame
function startGame() {
scoreboard.style.display = "block";
setGame();
random();
check();
countdown();
updateTime();
}
//Set to Menu
function menuGame() {
document.body.style.backgroundImage = "url('./img/bg.jpg')";
soundClick();
soundMusic();
load.style.display = "none";
presshere.style.display = "none";
game.style.display = "none";
gamewin.style.display = "none";
gameover.style.display = "none";
scoreboard.style.display = "none";
loadsuccess.style.display = "none";
mainmenu.style.display = "block";
contact.style.display = "block";
}
//Sound
function soundClick() {
s_click.pause();
s_click.currentTime = 0;
s_click.play();
}
function soundCountdown() {
s_countdown.pause();
s_countdown.currentTime = 0;
s_countdown.play();
}
function soundPress() {
if (gameend) {
return;
}
s_press.pause();
s_press.currentTime = 0;
s_press.play();
}
function soundDamage() {
s_damage.pause();
s_damage.currentTime = 0;
s_damage.play();
}
function soundHurt() {
s_hurt.pause();
s_hurt.currentTime = 0;
s_hurt.play();
}
function soundDie() {
s_die.pause();
s_die.currentTime = 0;
s_die.play();
}
function soundWin() {
s_win.pause();
s_win.currentTime = 0;
s_win.play();
}
function soundGameover() {
s_gameover.pause();
s_gameover.currentTime = 0;
s_gameover.play();
}
function soundMiss() {
s_miss.pause();
s_miss.currentTime = 0;
s_miss.play();
}
function soundMusic() {
s_music.currentTime = 0;
s_music.play();
s_music.loop = true;
}
function soundCoupe() {
s_music.pause();
s_coupe.currentTime = 0;
s_coupe.play();
}
//Loading Page
function myFunction() {
myTime = setTimeout(showPage, 500);
}
function showPage() {
loadingstart.style.display = "none";
load.style.display = "none";
loadsuccess.style.display = "block";
presshere.style.display = "block";
}
document.addEventListener("keydown", typing, false);