forked from youngrichu/infra_runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
406 lines (339 loc) · 14.2 KB
/
ui.js
File metadata and controls
406 lines (339 loc) · 14.2 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
export class UIManager {
constructor() {
this.scoreElement = null;
this.gameOverElement = null;
this.splashElement = null;
this.startMenuElement = null;
this.userInfoElement = null;
this.leaderboardElement = null;
this.powerUpElements = [];
this.activePowerUps = [];
this.score = 0;
this.blueprints = 0;
this.waterDrops = 0;
this.energyCells = 0;
this.createUI();
}
createUI() {
this.createScoreDisplay();
this.createGameOverScreen();
this.createSplashScreen();
this.createStartMenuScreen();
this.createUserInfoScreen();
this.createLeaderboardScreen();
}
createScoreDisplay() {
this.scoreElement = document.createElement('div');
this.scoreElement.className = 'score-display';
document.body.appendChild(this.scoreElement);
this.updateScoreDisplay();
}
createGameOverScreen() {
this.gameOverElement = document.createElement('div');
this.gameOverElement.className = 'game-over';
this.gameOverElement.style.display = 'none';
this.gameOverElement.innerHTML = `
<div class="game-over-title">GAME OVER</div>
<div class="game-over-stats" id="game-over-stats"></div>
<div class="game-over-instruction">Press R to Restart</div>
`;
document.body.appendChild(this.gameOverElement);
}
/* ------------------------------------------------------------------ */
/* NEW SCREENS */
/* ------------------------------------------------------------------ */
createSplashScreen() {
this.splashElement = document.createElement('div');
this.splashElement.className = 'splash-screen';
this.splashElement.innerHTML = `
<div class="splash-logo">Infrastructure Runner</div>
<div class="splash-subtitle">African City Edition</div>
`;
document.body.appendChild(this.splashElement);
this.hideElement(this.splashElement);
}
createStartMenuScreen() {
this.startMenuElement = document.createElement('div');
this.startMenuElement.className = 'start-menu';
const title = document.createElement('h1');
title.className = 'game-title';
title.innerText = 'Infrastructure Runner';
const subtitle = document.createElement('p');
subtitle.innerText = 'African City Edition';
subtitle.style.fontSize = '18px';
subtitle.style.marginBottom = '40px';
subtitle.style.opacity = '0.8';
const playBtn = document.createElement('button');
playBtn.className = 'menu-button';
playBtn.innerText = 'Play';
this.startMenuElement.appendChild(title);
this.startMenuElement.appendChild(subtitle);
this.startMenuElement.appendChild(playBtn);
document.body.appendChild(this.startMenuElement);
this.hideElement(this.startMenuElement);
// Expose event so game.js can hook into starting game
this.onStartButtonClicked = null;
playBtn.addEventListener('click', () => {
if (typeof this.onStartButtonClicked === 'function') {
this.onStartButtonClicked();
}
});
}
createUserInfoScreen() {
this.userInfoElement = document.createElement('div');
this.userInfoElement.className = 'user-info';
const container = document.createElement('div');
container.className = 'user-info-container';
const title = document.createElement('h2');
title.className = 'user-info-title';
title.innerText = 'Great Run! Enter Your Name';
const form = document.createElement('div');
form.className = 'user-info-form';
const input = document.createElement('input');
input.type = 'text';
input.maxLength = 15;
input.className = 'user-info-input';
input.placeholder = 'Your Name';
const submitBtn = document.createElement('button');
submitBtn.innerText = 'Submit Score';
form.appendChild(input);
form.appendChild(submitBtn);
container.appendChild(title);
container.appendChild(form);
this.userInfoElement.appendChild(container);
document.body.appendChild(this.userInfoElement);
this.hideElement(this.userInfoElement);
this.onUserInfoSubmitted = null; // callback(name)
const submitScore = () => {
const name = input.value.trim() || 'Anonymous';
if (typeof this.onUserInfoSubmitted === 'function') {
this.onUserInfoSubmitted(name);
}
};
submitBtn.addEventListener('click', submitScore);
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') submitScore();
});
}
createLeaderboardScreen() {
this.leaderboardElement = document.createElement('div');
this.leaderboardElement.className = 'leaderboard';
const container = document.createElement('div');
container.className = 'leaderboard-container';
const title = document.createElement('h2');
title.className = 'leaderboard-title';
title.innerText = 'Top Scores';
container.appendChild(title);
// Create professional table
this.leaderboardTable = document.createElement('table');
this.leaderboardTable.className = 'leaderboard-table';
container.appendChild(this.leaderboardTable);
// Back button
const backBtn = document.createElement('button');
backBtn.className = 'leaderboard-back';
backBtn.innerText = 'Back to Menu';
container.appendChild(backBtn);
this.leaderboardElement.appendChild(container);
document.body.appendChild(this.leaderboardElement);
this.hideElement(this.leaderboardElement);
this.onLeaderboardBack = null; // callback()
backBtn.addEventListener('click', () => {
if (typeof this.onLeaderboardBack === 'function') {
this.onLeaderboardBack();
}
});
}
/* --------------------------- Screen Helpers ------------------------ */
showElement(el) { if (el) el.style.display = 'flex'; }
hideElement(el) { if (el) el.style.display = 'none'; }
showSplash() { this.showElement(this.splashElement); }
hideSplash() { this.hideElement(this.splashElement); }
showStartMenu() { this.showElement(this.startMenuElement); }
hideStartMenu() { this.hideElement(this.startMenuElement); }
showUserInfo() {
// Focus on input when showing user info screen
this.showElement(this.userInfoElement);
setTimeout(() => {
const input = this.userInfoElement.querySelector('.user-info-input');
if (input) input.focus();
}, 100);
}
hideUserInfo() { this.hideElement(this.userInfoElement); }
showLeaderboard() { this.showElement(this.leaderboardElement); }
hideLeaderboard() { this.hideElement(this.leaderboardElement); }
/* --------------------------- Leaderboard --------------------------- */
/**
* Update leaderboard table with an array of entries:
* [{ name, score, blueprints, waterDrops, energyCells, date }, ...]
*/
updateLeaderboard(entries = []) {
if (!this.leaderboardTable) return;
console.log('🏆 DEBUG: Updating leaderboard with', entries.length, 'entries:', entries);
// Clear existing rows
this.leaderboardTable.innerHTML = '';
// Header row
const headerRow = document.createElement('tr');
['Rank','Name','Score','Blueprints','Water','Energy','Date'].forEach(h => {
const th = document.createElement('th');
th.innerText = h;
headerRow.appendChild(th);
});
this.leaderboardTable.appendChild(headerRow);
if (entries.length === 0) {
// Show "no scores yet" message
const emptyRow = document.createElement('tr');
const emptyCell = document.createElement('td');
emptyCell.colSpan = 7;
emptyCell.style.textAlign = 'center';
emptyCell.style.padding = '40px';
emptyCell.style.opacity = '0.7';
emptyCell.innerText = 'No scores yet - be the first!';
emptyRow.appendChild(emptyCell);
this.leaderboardTable.appendChild(emptyRow);
return;
}
entries.forEach((entry, idx) => {
const row = document.createElement('tr');
// Add rank styling for top 3
if (idx === 0) row.className = 'rank-1';
else if (idx === 1) row.className = 'rank-2';
else if (idx === 2) row.className = 'rank-3';
const cells = [
idx + 1,
entry.name || 'Anonymous',
(entry.score || 0).toLocaleString(),
entry.blueprints || 0,
entry.waterDrops || 0,
entry.energyCells || 0,
entry.date ? new Date(entry.date).toLocaleDateString() : 'Today'
];
cells.forEach(val => {
const td = document.createElement('td');
td.innerText = val;
row.appendChild(td);
});
this.leaderboardTable.appendChild(row);
});
console.log('✅ Leaderboard updated successfully');
}
updateScore(points) {
this.score += points;
this.updateScoreDisplay();
}
updateCollectables(type, points) {
switch (type) {
case 'blueprint':
this.blueprints++;
break;
case 'waterDrop':
this.waterDrops++;
break;
case 'energyCell':
this.energyCells++;
break;
}
this.updateScore(points);
}
updateScoreDisplay() {
if (this.scoreElement) {
this.scoreElement.innerHTML = `
<div class="score-value">Score: ${Math.floor(this.score)}</div>
<div>BP: ${this.blueprints} | WD: ${this.waterDrops} | EC: ${this.energyCells}</div>
`;
}
}
showGameOver() {
const statsElement = this.gameOverElement.querySelector('#game-over-stats');
if (statsElement) {
statsElement.innerHTML = `
<div><span class="stat-label">Final Score:</span> ${Math.floor(this.score)}</div>
<div><span class="stat-label">Blueprints:</span> ${this.blueprints}</div>
<div><span class="stat-label">Water Drops:</span> ${this.waterDrops}</div>
<div><span class="stat-label">Energy Cells:</span> ${this.energyCells}</div>
`;
}
this.gameOverElement.style.display = 'block';
console.log('Game Over! Final Score:', Math.floor(this.score));
console.log(`Blueprints: ${this.blueprints}, Water Drops: ${this.waterDrops}, Energy Cells: ${this.energyCells}`);
}
hideGameOver() {
this.gameOverElement.style.display = 'none';
}
addPowerUpToUI(name, durationSeconds) {
const powerUpElement = document.createElement('div');
powerUpElement.className = 'power-up';
powerUpElement.style.top = `${70 + this.powerUpElements.length * 60}px`;
powerUpElement.innerHTML = `
<span class="power-up-icon">${name.substring(0, 2)}</span>
<span>${name.substring(3)}</span>
<span class="power-up-timer">${durationSeconds}s</span>
`;
document.body.appendChild(powerUpElement);
const powerUp = {
element: powerUpElement,
name: name,
duration: durationSeconds,
startTime: Date.now()
};
this.powerUpElements.push(powerUp);
this.activePowerUps.push(powerUp);
setTimeout(() => {
this.removePowerUpFromUI(powerUp);
}, durationSeconds * 1000);
}
removePowerUpFromUI(powerUp) {
if (powerUp.element && powerUp.element.parentNode) {
document.body.removeChild(powerUp.element);
}
this.powerUpElements = this.powerUpElements.filter(p => p !== powerUp);
this.activePowerUps = this.activePowerUps.filter(p => p !== powerUp);
// Reposition remaining power-ups
this.powerUpElements.forEach((p, index) => {
if (p.element) {
p.element.style.top = `${70 + index * 60}px`;
}
});
}
// Performance optimized: Only update timers every 100ms instead of every frame
updatePowerUpTimers() {
// Only update every 6 frames (about 100ms at 60fps) to reduce performance impact
if (!this.timerUpdateCounter) this.timerUpdateCounter = 0;
this.timerUpdateCounter++;
if (this.timerUpdateCounter % 6 !== 0) return;
for (const powerUp of this.activePowerUps) {
const elapsed = (Date.now() - powerUp.startTime) / 1000;
const remaining = Math.max(0, powerUp.duration - elapsed);
const timerElement = powerUp.element?.querySelector('.power-up-timer');
if (timerElement) {
timerElement.innerText = `${remaining.toFixed(1)}s`;
}
}
}
getScore() {
return this.score;
}
getCollectableStats() {
return {
blueprints: this.blueprints,
waterDrops: this.waterDrops,
energyCells: this.energyCells
};
}
reset() {
this.score = 0;
this.blueprints = 0;
this.waterDrops = 0;
this.energyCells = 0;
this.timerUpdateCounter = 0;
// Clear power-up UI elements
this.powerUpElements.forEach(p => {
if (p.element && p.element.parentNode) {
p.element.parentNode.removeChild(p.element);
}
});
this.powerUpElements = [];
this.activePowerUps = [];
this.hideGameOver();
this.updateScoreDisplay();
}
}