-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
115 lines (101 loc) · 3.64 KB
/
script.js
File metadata and controls
115 lines (101 loc) · 3.64 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
const gaugeFill = document.querySelector('.gauge-fill');
const gaugeCover = document.querySelector('.gauge-cover');
const refillButtons = document.querySelectorAll('.refill-buttons button');
const lilyImage = document.querySelector('.lily-image');
const cheerSound = document.getElementById('cheerSound');
const crySound = document.getElementById('crySound');
let fuelLevel = 0;
let stickers = 0;
let emojiIndex = 0;
const emojis = ['🦄', '💓', '🌈', '🌟'];
let incrementAmount = 0.20;
let depletionRatePerMinute = 0.01;
let depletionAmountPerSecond = depletionRatePerMinute / 60;
let incrementInterval;
let depletionInterval;
let isPaused = false;
const pauseButton = document.getElementById('pauseButton');
pauseButton.addEventListener('click', togglePause);
function togglePause() {
isPaused = !isPaused;
pauseButton.textContent = isPaused ? 'Start' : 'Pause';
if (isPaused) {
clearInterval(depletionInterval);
} else {
startDepletion();
}
}
function updateGauge() {
const angle = fuelLevel * 360;
const radius = 90;
const centerX = 100;
const centerY = 100;
const startX = centerX;
const startY = centerY - radius;
const x = centerX + radius * Math.cos((angle - 90) * Math.PI / 180);
const y = centerY + radius * Math.sin((angle - 90) * Math.PI / 180);
const d = `M ${centerX} ${centerY} L ${startX} ${startY} A ${radius} ${radius} 0 ${angle > 180 ? 1 : 0} 1 ${x} ${y} Z`;
gaugeFill.setAttribute('d', d);
gaugeCover.textContent = `${Math.floor(fuelLevel * 100)}%`;
const stickersDisplay = document.querySelector('.stickers');
const starStickersDisplay = document.querySelector('.star-stickers');
let starString = '';
for (let i = 0; i < stickers; i++) {
starString += '🌟';
}
//starStickersDisplay.textContent = starString;
if (fuelLevel >= 1) {
clearInterval(incrementInterval);
clearInterval(depletionInterval);
gaugeCover.textContent = "Yay, Lily!";
lilyImage.src = "lily_happy.png";
cheerSound.play();
} else if (fuelLevel <= 0) {
clearInterval(incrementInterval);
clearInterval(depletionInterval);
gaugeCover.textContent = "Uh Oh!";
lilyImage.src = "lily_sad.png";
crySound.play();
} else {
lilyImage.src = "lily_normal.png";
}
}
function incrementFuel(event) {
stickers++;
const stickersDisplay = document.querySelector('.stickers');
stickersDisplay.textContent = `Stickers: ${stickers}`;
fuelLevel += incrementAmount;
if (fuelLevel > 1) fuelLevel = 1;
const starStickersDisplay = document.querySelector('.star-stickers');
starStickersDisplay.textContent += emojis[emojiIndex];
emojiIndex = (emojiIndex + 1) % emojis.length;
updateGauge();
startDepletion();
cheerSound.play();
// Disable the button for 1 second
const button = event.target;
button.disabled = true;
const originalImage = lilyImage.src;
lilyImage.src = "lily_happy.png";
setTimeout(() => {
button.disabled = false;
lilyImage.src = originalImage;
}, 3000);
}
function depleteFuel() {
if (isPaused) return;
fuelLevel -= depletionAmountPerSecond;
if (fuelLevel < 0) fuelLevel = 0;
updateGauge();
}
function startDepletion() {
if (isPaused) return;
clearInterval(depletionInterval);
depletionInterval = setInterval(() => {
depleteFuel();
}, 1000);
}
refillButtons.forEach(button => {
button.addEventListener('click', (event) => incrementFuel(event));
});
updateGauge();