-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
307 lines (252 loc) · 8.05 KB
/
Copy pathscript.js
File metadata and controls
307 lines (252 loc) · 8.05 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
/*
Resurrection W: Save the Plant
Beginner-friendly JavaScript game logic.
Customize game settings here:
*/
const SCORE_GOAL = 10;
const START_TIME = 30;
const NORMAL_DROP_SPEED = 900; // score 0-4
const FAST_DROP_SPEED = 700; // score 5-7
const FASTEST_DROP_SPEED = 550; // score 8+
// Customize messages here.
const MESSAGES = {
start: "Click clean water drops!",
ready: "Press Start to begin!",
clean: "Great! Clean water helps the plant grow. +1",
jerryCan: "Jerry Can bonus! +3",
dirty: "Dirty water! -1",
combo: "Combo! Keep the clean water coming!",
win: "You saved the plant!",
lose: "Time is up! Try again to bring clean water."
};
// Get important HTML elements.
const scoreDisplay = document.getElementById("scoreDisplay");
const timerDisplay = document.getElementById("timerDisplay");
const feedbackText = document.getElementById("feedbackText");
const gameArea = document.getElementById("gameArea");
const plant = document.getElementById("plant");
const plantEmoji = plant.querySelector(".plant-emoji");
const plantLabel = document.getElementById("plantLabel");
const startButtonContainer = document.getElementById("startButtonContainer");
const startButton = document.getElementById("startButton");
const resetButton = document.getElementById("resetButton");
const resultOverlay = document.getElementById("resultOverlay");
const resultTitle = document.getElementById("resultTitle");
const starRating = document.getElementById("starRating");
const finalScore = document.getElementById("finalScore");
const resultMessage = document.getElementById("resultMessage");
const playAgainButton = document.getElementById("playAgainButton");
const closeModalButton = document.getElementById("closeModalButton");
// Track game state.
let score = 0;
let timeLeft = START_TIME;
let timerInterval;
let dropInterval;
let gameRunning = false;
let currentDropSpeed = NORMAL_DROP_SPEED;
let comboCount = 0;
// Show the ready screen when the page loads. The game does not start yet.
showStartState();
// Button controls.
startButton.addEventListener("click", startGame);
resetButton.addEventListener("click", showStartState);
playAgainButton.addEventListener("click", showStartState);
closeModalButton.addEventListener("click", closeResultModal);
function showStartState() {
score = 0;
timeLeft = START_TIME;
gameRunning = false;
comboCount = 0;
currentDropSpeed = NORMAL_DROP_SPEED;
clearInterval(timerInterval);
clearInterval(dropInterval);
clearDrops();
closeResultModal();
scoreDisplay.textContent = `Score: ${score}`;
timerDisplay.textContent = `Time: ${timeLeft}`;
feedbackText.textContent = MESSAGES.ready;
plant.className = "plant dry";
plant.setAttribute("aria-label", "Dry plant");
plantEmoji.textContent = "🥀";
plantLabel.textContent = "Dry plant";
// Show the big centered Start button before the game begins.
startButtonContainer.classList.remove("hidden");
startButton.disabled = false;
}
function startGame() {
if (gameRunning) {
return;
}
gameRunning = true;
feedbackText.textContent = MESSAGES.start;
// Hide the Start button so it does not block water drops.
startButtonContainer.classList.add("hidden");
startButton.disabled = true;
timerInterval = setInterval(updateTimer, 1000);
dropInterval = setInterval(createWaterDrop, currentDropSpeed);
createWaterDrop();
}
function updateTimer() {
timeLeft--;
timerDisplay.textContent = `Time: ${timeLeft}`;
if (timeLeft <= 0) {
endGame(false);
}
}
function createWaterDrop() {
if (!gameRunning) {
return;
}
const drop = document.createElement("button");
const dropType = chooseDropType();
drop.classList.add("water-drop", dropType);
drop.type = "button";
drop.setAttribute("aria-label", dropType === "jerry-can" ? "Jerry Can bonus" : `${dropType} water drop`);
// Jerry Can bonus uses the provided transparent image without recoloring or distortion.
if (dropType === "jerry-can") {
const jerryCanImage = document.createElement("img");
jerryCanImage.src = "png/water-can-transparent.png";
jerryCanImage.alt = "Jerry Can bonus";
jerryCanImage.classList.add("jerry-can-image");
drop.appendChild(jerryCanImage);
}
// Keep drops inside the visible game area and away from the plant at the bottom.
const dropSize = 52;
const maxX = gameArea.clientWidth - dropSize;
const maxY = gameArea.clientHeight - 150;
drop.style.left = `${randomNumber(8, Math.max(8, maxX))}px`;
drop.style.top = `${randomNumber(8, Math.max(8, maxY))}px`;
drop.addEventListener("click", () => handleDropClick(drop, dropType));
gameArea.appendChild(drop);
// Remove old drops if the player does not click them.
setTimeout(() => {
if (drop.parentElement) {
drop.remove();
}
}, 2200);
}
function chooseDropType() {
const chance = Math.random();
if (chance < 0.15) {
return "jerry-can";
}
if (chance < 0.42) {
return "dirty";
}
return "clean";
}
function handleDropClick(drop, dropType) {
if (!gameRunning) {
return;
}
if (dropType === "clean") {
score++;
comboCount++;
feedbackText.textContent = MESSAGES.clean;
} else if (dropType === "jerry-can") {
score += 3;
comboCount++;
feedbackText.textContent = MESSAGES.jerryCan;
} else {
score = Math.max(0, score - 1);
comboCount = 0;
feedbackText.textContent = MESSAGES.dirty;
}
if (comboCount >= 3 && dropType !== "dirty") {
feedbackText.textContent += ` ${MESSAGES.combo}`;
}
scoreDisplay.textContent = `Score: ${score}`;
drop.remove();
updateDropSpeed();
if (score >= SCORE_GOAL) {
endGame(true);
}
}
function endGame(playerWon) {
gameRunning = false;
clearInterval(timerInterval);
clearInterval(dropInterval);
clearDrops();
startButton.disabled = true;
if (playerWon) {
feedbackText.textContent = MESSAGES.win;
plant.className = "plant healthy";
plant.setAttribute("aria-label", "Healthy saved plant");
plantEmoji.textContent = "🌱";
plantLabel.textContent = "Plant saved!";
} else {
feedbackText.textContent = MESSAGES.lose;
}
showResultModal(playerWon);
}
function updateDropSpeed() {
let newSpeed = NORMAL_DROP_SPEED;
if (score >= 8) {
newSpeed = FASTEST_DROP_SPEED;
} else if (score >= 5) {
newSpeed = FAST_DROP_SPEED;
}
if (newSpeed !== currentDropSpeed) {
currentDropSpeed = newSpeed;
clearInterval(dropInterval);
dropInterval = setInterval(createWaterDrop, currentDropSpeed);
}
}
function showResultModal(playerWon) {
const stars = getStarCount();
resultTitle.textContent = playerWon ? "You Saved the Plant!" : "Round Complete";
starRating.textContent = "★".repeat(stars) + "☆".repeat(3 - stars);
finalScore.textContent = `Final Score: ${score}`;
resultMessage.textContent = getRatingMessage(stars);
resultOverlay.classList.remove("hidden");
resultOverlay.setAttribute("aria-hidden", "false");
if (stars === 3) {
celebrate();
}
}
function closeResultModal() {
resultOverlay.classList.add("hidden");
resultOverlay.setAttribute("aria-hidden", "true");
}
function getStarCount() {
if (score >= SCORE_GOAL) {
return 3;
}
if (score >= 8) {
return 2;
}
if (score >= 3) {
return 1;
}
return 0;
}
function getRatingMessage(stars) {
if (stars === 3) {
return "Plant saved!";
}
if (stars === 2) {
return "Almost there!";
}
if (stars === 1) {
return "Keep trying!";
}
return "Please try again!";
}
function clearDrops() {
const drops = document.querySelectorAll(".water-drop, .confetti");
drops.forEach((drop) => drop.remove());
}
function celebrate() {
const colors = ["#ffc907", "#1ca3ec", "#35a853", "#ffffff", "#111827"];
for (let i = 0; i < 35; i++) {
const confetti = document.createElement("span");
confetti.classList.add("confetti");
confetti.style.left = `${randomNumber(0, gameArea.clientWidth)}px`;
confetti.style.backgroundColor = colors[i % colors.length];
confetti.style.animationDelay = `${Math.random() * 0.6}s`;
gameArea.appendChild(confetti);
}
}
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}