From 3705189f91d36c693d38ad9f35f971d3355b8152 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 27 Jan 2024 10:11:44 +0100 Subject: [PATCH] display stopwatch --- potain05/assets/jeu.js | 30 ++++++++++++++++++++++++++++-- potain05/index.html | 10 ++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/potain05/assets/jeu.js b/potain05/assets/jeu.js index a5a7733..0d392a1 100644 --- a/potain05/assets/jeu.js +++ b/potain05/assets/jeu.js @@ -4,23 +4,31 @@ const GAME_NOT_STARTED = 0; const GAME_STARTED = 1; const GAME_WON = 2; const GAME_LOST = 3; +const CHEAT = false; let gameState = GAME_NOT_STARTED; let zones = undefined; +let timer = undefined; + let endTimeout = undefined; let roundTimeout = undefined; +let timerInterval = undefined; + let timeBetween = 2000; const MIN_TIME_BETWEEN = 1000; const ACCELERATION = 100; +const DURATION = 60_500; let score = 0; +let zonesCleared = 0; +let startTime = undefined; function start() { document.getElementsByTagName("button")[0].disabled = true; gameState = GAME_STARTED; - endTimeout = setTimeout(end, 30_000); - roundTimeout = setTimeout(round, 1); + startTime = new Date(); + timer = document.getElementById("timer"); zones = document.getElementsByClassName("zone"); for (const zone of zones) { @@ -36,6 +44,15 @@ function start() { } }) } + + endTimeout = setTimeout(end, DURATION); + timerInterval = setInterval(updateTimer, 500); + roundTimeout = setTimeout(round, 1); +} + +function updateTimer() { + const now = new Date(); + timer.innerText = Math.floor(Math.abs(startTime - now) / 1000).toString(); } function round() { @@ -48,6 +65,12 @@ function round() { zone.classList.remove("inactive", "fade-out"); + if (CHEAT) { + setTimeout(() => { + zone.click(); + }, 200); + } + setTimeout(function() { zone.style.transition = `opacity ${timeBetween-(MIN_TIME_BETWEEN / 2)}ms linear`; }, 100); @@ -74,6 +97,7 @@ function round() { function end() { clearTimeout(roundTimeout); clearTimeout(endTimeout); + clearInterval(timerInterval); if (gameState === GAME_STARTED) { gameState = GAME_WON; document.getElementsByClassName("success stamp")[0].classList.remove("hidden"); @@ -84,5 +108,7 @@ function end() { function increment() { score += Math.floor(Math.random() * zones.length) + 1; + zonesCleared++; document.getElementById("score").innerText = score.toString(); + document.getElementById("zonesCount").innerText = zonesCleared.toString(); } diff --git a/potain05/index.html b/potain05/index.html index 4c6b2aa..4c84e7c 100644 --- a/potain05/index.html +++ b/potain05/index.html @@ -22,8 +22,9 @@

Jeu interactif

- Aide Pimpin le technicien à reboucher les fuites de fluide frigorigène !
- Score : 0 kg CO2e non rejetés dans l'atmosphère
+ Tu as une minute pour aider Pimpin le technicien à reboucher les fuites de fluide frigorigène !
+ Score : 0 kg CO2e non rejetés dans l'atmosphère (0 fuites).
+ Temps écoulé : 0s.

@@ -43,6 +44,7 @@
+

N'hésite pas à partager ton score sur Teams 😉

- - + \ No newline at end of file