refactor
This commit is contained in:
parent
232e9f44d3
commit
2f8c39b8b6
1 changed files with 13 additions and 26 deletions
|
@ -16,17 +16,19 @@ const ACCELERATION = 100;
|
||||||
|
|
||||||
let score = 0;
|
let score = 0;
|
||||||
|
|
||||||
(function () {
|
function start() {
|
||||||
|
document.getElementsByTagName("button")[0].disabled = true;
|
||||||
|
gameState = GAME_STARTED;
|
||||||
|
endTimeout = setTimeout(end, 30_000);
|
||||||
|
roundTimeout = setTimeout(round, 1);
|
||||||
|
|
||||||
zones = document.getElementsByClassName("zone");
|
zones = document.getElementsByClassName("zone");
|
||||||
for (const zone of zones) {
|
for (const zone of zones) {
|
||||||
zone.addEventListener("click", event => {
|
zone.addEventListener("click", event => {
|
||||||
if (gameState !== GAME_STARTED) {
|
if (gameState !== GAME_STARTED) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (event.target.classList.contains("inactive")) {
|
if (!event.target.classList.contains("inactive")) {
|
||||||
// gameState = GAME_LOST;
|
|
||||||
// end();
|
|
||||||
} else {
|
|
||||||
event.target.style.transition = "";
|
event.target.style.transition = "";
|
||||||
event.target.classList.remove("fade-out");
|
event.target.classList.remove("fade-out");
|
||||||
event.target.classList.add("inactive");
|
event.target.classList.add("inactive");
|
||||||
|
@ -34,13 +36,6 @@ let score = 0;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})();
|
|
||||||
|
|
||||||
function start() {
|
|
||||||
document.getElementsByTagName("button")[0].disabled = true;
|
|
||||||
gameState = GAME_STARTED;
|
|
||||||
endTimeout = setTimeout(end, 30_000);
|
|
||||||
roundTimeout = setTimeout(round, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function round() {
|
function round() {
|
||||||
|
@ -50,31 +45,27 @@ function round() {
|
||||||
}
|
}
|
||||||
|
|
||||||
let zone = zones[Math.floor(Math.random() * zones.length)];
|
let zone = zones[Math.floor(Math.random() * zones.length)];
|
||||||
log("selected zone", zone);
|
|
||||||
zone.classList.remove("inactive", "fade-out");
|
zone.classList.remove("inactive", "fade-out");
|
||||||
log("removed inactive");
|
|
||||||
setTimeout(function () {
|
|
||||||
console.log("clicking", zone);
|
|
||||||
zone.click();
|
|
||||||
}, 200);
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
zone.style.transition = `opacity ${timeBetween-(MIN_TIME_BETWEEN / 2)}ms linear`;
|
zone.style.transition = `opacity ${timeBetween-(MIN_TIME_BETWEEN / 2)}ms linear`;
|
||||||
log("set transition opacity");
|
|
||||||
}, 100);
|
}, 100);
|
||||||
|
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
zone.classList.add("fade-out");
|
zone.classList.add("fade-out");
|
||||||
log("set fade out");
|
|
||||||
}, MIN_TIME_BETWEEN / 2);
|
}, MIN_TIME_BETWEEN / 2);
|
||||||
|
|
||||||
let zoneTimeout = setTimeout(() => {
|
let zoneTimeout = setTimeout(() => {
|
||||||
if (!zone.classList.contains("inactive") && gameState === GAME_STARTED) {
|
if (!zone.classList.contains("inactive") && gameState === GAME_STARTED) {
|
||||||
log("lost because didn't click fast enough", zone.id);
|
console.log("lost because didn't click fast enough", zone.id);
|
||||||
gameState = GAME_LOST;
|
gameState = GAME_LOST;
|
||||||
end();
|
end();
|
||||||
}
|
}
|
||||||
}, timeBetween);
|
}, timeBetween);
|
||||||
zone.addEventListener("click", () => {clearTimeout(zoneTimeout)})
|
zone.addEventListener("click", () => {clearTimeout(zoneTimeout)})
|
||||||
|
|
||||||
// Recursive call
|
// Recursive call, sorta
|
||||||
timeBetween -= ACCELERATION;
|
timeBetween -= ACCELERATION;
|
||||||
if (timeBetween <= MIN_TIME_BETWEEN) {timeBetween = MIN_TIME_BETWEEN;}
|
if (timeBetween <= MIN_TIME_BETWEEN) {timeBetween = MIN_TIME_BETWEEN;}
|
||||||
roundTimeout = setTimeout(round, timeBetween);
|
roundTimeout = setTimeout(round, timeBetween);
|
||||||
|
@ -95,7 +86,3 @@ function increment() {
|
||||||
score += Math.floor(Math.random() * zones.length) + 1;
|
score += Math.floor(Math.random() * zones.length) + 1;
|
||||||
document.getElementById("score").innerText = score.toString();
|
document.getElementById("score").innerText = score.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
function log(...args) {
|
|
||||||
console.log(...args);
|
|
||||||
}
|
|
Loading…
Reference in a new issue