-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
123 lines (101 loc) · 3.8 KB
/
game.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
// Divs
var body = document.querySelector("body");
var welcomewindow = document.querySelector("#welcome-section");
var end = document.querySelector("#end");
var joke = document.querySelector("#joke-window");
var fixClose = document.getElementsByClassName('error');
// Buttons
var startbtn = document.querySelector("#play-btn");
var buttonClose = document.getElementsByClassName("will-close");
// Dinamic Texts
var finalScore = document.querySelector("#finalScore");
// Functional variables
var errorArray = [];
var score = 0;
var verify;
startbtn.addEventListener("click", function(){
welcomewindow.style.display = "none";
startGame();
// Joke window functioning
window.setTimeout( function(){joke.style.display = 'block'}, 2000);
window.setTimeout( function(){joke.style.display = 'none'}, 7000);
})
function startGame()
{
// Recursive function that increases the speed of error windows spams
increasingSpeedSpan(generator, 10000, 1000);
verify = setInterval(verifier, 500);
}
function increasingSpeedSpan(callback, factor, times)
{
var internalCallback = function(tick, counter) {
return function() {
if (--tick >= 0 && verifier()) {
window.setTimeout(internalCallback, factor/++counter);
callback();
fix();
}
}
}(times, 0);
window.setTimeout(internalCallback, factor);
}
// function that erases the error windows that are closed
function fix()
{
Array.prototype.forEach.call(buttonClose, element => {
element.addEventListener("click", function(){
console.log("cliquei");
score++;
finalScore.textContent = score;
let errorToClose = element.closest(".error");
errorToClose.style.display = "none";
errorArray.pop();
})
});
}
// function that creates error windows and place them randomly in the screen
function generator()
{
let min, max;
min = Math.ceil(0);
max = Math.floor(70);
let randomX = Math.floor(Math.random()* (max + min + 1)) + min;
min = Math.ceil(0);
max = Math.floor(80);
let randomY = Math.floor(Math.random()* (max + min + 1)) + min;
/* optimize span areas */
let errorWindow = `<div class='window error random' style='z-index: 1; top:${randomY}vh; left:${randomX}vw'>
<div class='title-bar'>
<div class='title-bar-text'>Error </div>
<div class='title-bar-controls'>
<button aria-label='Close'></button> </div>
</div>
<div class='window-body'>
<p>Click 'fix' to fix error.</p>
<button class='will-close'>Fix</button></div>
</div>`;
body.insertAdjacentHTML("beforeend", errorWindow);
errorArray.push(errorWindow);
}
// function that verifies if the limit hasnt been exceeded
function verifier()
{
if(errorArray.length >= 10)
{
clearInterval(verify);
endGame();
return false;
}
return true;
}
function endGame()
{
end.style.visibility = "visible";
// the windows present before endgame have to be erased for two reasons
// 1. if they are still visible, they can still be clicked and the person can still make points after endgame
// 2. the #end window will be overlayed by them, so depending on the spans x,y's they will hide the user result
Array.prototype.forEach.call(buttonClose, element => {
let errorToClose = element.closest(".error");
errorToClose.classList.add("no");
});
}