-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTDMenu.html
899 lines (782 loc) · 34.5 KB
/
TDMenu.html
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Teleporter Dash</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@700&display=swap');
:root {
--primary-color: #0F9D58;
--secondary-color: #4285F4;
--bg-dark: #1a1a2e;
--bg-darker: #16213e;
--bg-darkest: #182138;
--gradient: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
--shadow-glow: 0 0 20px var(--primary-color);
--transition-speed: 0.3s;
--menu-scale: 1;
--level-selector-scale: 1;
--content-scale: 1; /* New variable for content scaling */
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: var(--bg-dark);
color: white;
font-family: 'Orbitron', sans-serif;
overflow: hidden;
touch-action: manipulation;
}
.menu,
.options-menu,
.level-selector {
background-color: var(--bg-darker);
padding: calc(15px * var(--menu-scale));
border-radius: calc(12px * var(--menu-scale));
box-shadow: var(--shadow-glow);
text-align: center;
opacity: 1;
transition: transform var(--transition-speed) ease, opacity 0.5s ease-in-out;
visibility: visible;
transform-origin: center;
}
.menu,
.options-menu {
width: calc(450px * var(--menu-scale));
max-width: 75vw;
transform: scale(var(--menu-scale));
}
.level-selector {
width: calc(1200px * var(--level-selector-scale)); /* Adjusted base width */
max-width: 100vw;
height: calc(1000px * var(--level-selector-scale)); /* Increased base height */
max-height: 100vh; /* Increased for mobile */
display: flex;
flex-direction: column;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) scale(var(--level-selector-scale));
overflow: auto;
-webkit-overflow-scrolling: touch;
padding: calc(20px * var(--level-selector-scale)); /* Increased padding */
}
.built-in-levels,
.online-levels {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}
.level-display {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
flex: 1;
margin: calc(30px * var(--level-selector-scale)) 0;
}
h1 {
font-size: clamp(1.6rem, calc(1.8rem * var(--menu-scale)), 2.5rem);
margin: calc(12px * var(--menu-scale)) 0;
color: var(--primary-color);
text-shadow: var(--shadow-glow);
}
.level-selector h1 {
font-size: calc(2.5rem * var(--level-selector-scale) * var(--content-scale));
}
h2 {
font-size: clamp(0.9rem, calc(1rem * var(--menu-scale)), 1.8rem);
margin: calc(8px * var(--menu-scale)) 0;
}
h3 {
font-size: clamp(1rem, calc(1.3rem * var(--menu-scale)), 2rem);
margin: calc(10px * var(--menu-scale)) 0;
color: var(--primary-color);
}
button {
width: 100%;
margin: calc(10px * var(--menu-scale)) auto;
padding: calc(12px * var(--menu-scale));
font-size: clamp(0.9rem, calc(1rem * var(--menu-scale)), 1.8rem);
background: var(--gradient);
border: calc(2px * var(--menu-scale)) solid var(--primary-color);
border-radius: calc(10px * var(--menu-scale));
color: var(--bg-darker);
font-weight: bold;
cursor: pointer;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.5);
transition: var(--transition-speed);
}
button:hover {
background: linear-gradient(45deg, var(--secondary-color), var(--primary-color));
transform: scale(1.05);
}
button:active {
transform: scale(0.98);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}
.level-navigation {
display: flex;
flex-direction: column;
gap: calc(13px * var(--level-selector-scale) * var(--content-scale));
width: 90%;
max-width: calc(90vw * var(--level-selector-scale));
margin: calc(25px * var(--level-selector-scale)) auto;
}
.level-navigation button {
width: 100%;
padding: calc(25px * var(--level-selector-scale) * var(--content-scale));
font-size: calc(2rem * var(--level-selector-scale) * var(--content-scale));
background: var(--gradient);
border: none;
border-radius: calc(15px * var(--level-selector-scale) * var(--content-scale));
color: var(--bg-darker);
font-weight: bold;
cursor: pointer;
transition: transform 0.2s, background 0.2s;
font-family: 'Orbitron', sans-serif;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
}
.level-navigation button:hover:not(:disabled) {
transform: scale(1.05);
background: linear-gradient(45deg, var(--secondary-color), var(--primary-color));
}
.level-navigation button:active:not(:disabled) {
transform: scale(0.98);
}
.level-navigation button:disabled {
opacity: 0.5;
cursor: not-allowed;
background: #666;
}
.nav-button.prev::before {
content: "←";
margin-right: calc(10px * var(--level-selector-scale) * var(--content-scale));
}
.nav-button.next::after {
content: "→";
margin-left: calc(10px * var(--level-selector-scale) * var(--content-scale));
}
.level-info {
display: flex;
flex-direction: column;
align-items: center;
gap: calc(10px * var(--level-selector-scale) * var(--content-scale));
margin-top: calc(20px * var(--level-selector-scale));
color: white;
}
.level-title {
font-size: calc(2rem * var(--level-selector-scale) * var(--content-scale));
margin: 0;
color: var(--primary-color);
}
.level-stats {
display: flex;
justify-content: space-around;
width: 100%;
font-size: calc(1.2rem * var(--level-selector-scale) * var(--content-scale));
color: #888;
}
.level-preview {
width: calc(40vw * var(--level-selector-scale) * var(--content-scale));
height: calc(30vw * var(--level-selector-scale) * var(--content-scale));
max-width: calc(500px * var(--content-scale));
max-height: calc(375px * var(--content-scale));
background-color: var(--bg-darkest);
border-radius: calc(15px * var(--level-selector-scale) * var(--content-scale));
overflow: hidden;
margin: 0 auto;
box-shadow: var(--shadow-glow);
image-rendering: pixelated;
}
.completion {
height: calc(6px * var(--menu-scale));
background: #333;
border-radius: calc(3px * var(--menu-scale));
margin: calc(15px * var(--menu-scale)) auto;
width: 80%;
overflow: hidden;
}
.completion-fill {
height: 100%;
background: var(--gradient);
border-radius: calc(3px * var(--menu-scale));
width: 0%;
transition: width var(--transition-speed);
}
.fade-out {
opacity: 0;
visibility: hidden;
}
.fade-in {
opacity: 1;
visibility: visible;
}
.back-button {
align-self: flex-start;
margin-top: auto;
padding: calc(25px * var(--level-selector-scale) * var(--content-scale)) calc(40px * var(--level-selector-scale) * var(--content-scale));
font-size: calc(2rem * var(--level-selector-scale) * var(--content-scale));
}
.error-message {
background-color: rgba(255, 0, 0, 0.1);
border: calc(1px * var(--menu-scale)) solid #ff0000;
padding: calc(8px * var(--menu-scale));
margin: calc(8px * var(--menu-scale)) 0;
border-radius: calc(4px * var(--menu-scale));
text-align: center;
}
.error-message button {
margin-top: calc(8px * var(--menu-scale));
padding: calc(4px * var(--menu-scale)) calc(12px * var(--menu-scale));
background-color: #ff0000;
color: white;
border: none;
border-radius: calc(2px * var(--menu-scale));
cursor: pointer;
}
.error-message button:hover {
background-color: #cc0000;
}
</style>
</head>
<body>
<audio id="menu-music" loop preload="auto">
<source src="Sound/Basic Soundeffects/menutd.ogg" type="audio/ogg">
</audio>
<div class="menu">
<h1>Teleporter Dash</h1>
<button onclick="handleMenuTransition('menu', 'built-in-levels')">Levels</button>
<button onclick="handleMenuTransition('menu', 'online-levels')">Online Levels</button>
<button onclick="window.location.href='Levels/levelstore.html'">Level Store</button>
<button onclick="window.location.href='Level Editor/leveleditor.html'">Level Creator</button>
<button onclick="handleMenuTransition('menu', 'options-menu')">Settings</button>
<button onclick="showCredits();">Credits</button>
</div>
<div class="built-in-levels" style="display: none;">
<div class="level-selector">
<!-- Will be populated by loadBuiltInLevelRegistry() -->
</div>
</div>
<div class="online-levels" style="display: none;">
<div class="level-selector">
<!-- Will be populated by loadOnlineLevelRegistry() -->
</div>
</div>
<div class="options-menu" style="display: none;">
<h1>Settings</h1>
<div style="border-top: 1px solid #ccc; width: 100%; margin: 0 auto;"></div>
<h3>Volume Control</h3>
<input type="range" id="volume-slider" min="0" max="100" value="50" oninput="updateVolumeLabel()">
<p>Volume: <span id="volume-label">50</span>%</p>
<div style="border-top: 1px solid #ccc; width: 70%; margin: 0 auto;"></div>
<h3>Clear Data</h3>
<button onclick="clearData();">Clear All Data</button>
<div style="border-top: 1px solid #ccc; width: 70%; margin: 0 auto;"></div>
<button onclick="handleMenuTransition('options-menu', 'menu')">Back</button>
</div>
<script>
// Menu Navigation System
async function transitionMenu(fromMenu, toMenu) {
return new Promise((resolve) => {
if (fromMenu) {
fromMenu.classList.add('fade-out');
setTimeout(() => {
fromMenu.style.display = "none";
if (toMenu) {
toMenu.style.display = "block";
setTimeout(() => {
toMenu.classList.remove('fade-out');
resolve();
}, 50);
} else {
resolve();
}
}, 500);
} else if (toMenu) {
toMenu.style.display = "block";
setTimeout(() => {
toMenu.classList.remove('fade-out');
resolve();
}, 50);
}
});
}
async function handleMenuTransition(from, to) {
const fromMenu = document.querySelector('.' + from);
const toMenu = document.querySelector('.' + to);
if (to === 'built-in-levels') {
await loadBuiltInLevelRegistry();
} else if (to === 'online-levels') {
await loadOnlineLevelRegistry();
}
await transitionMenu(fromMenu, toMenu);
}
// Audio Setup
const menuMusic = document.getElementById('menu-music');
menuMusic.loop = true;
menuMusic.volume = 0.9;
menuMusic.preload = 'auto';
window.addEventListener('click', () => menuMusic.play(), { once: true });
let levelsLoaded = false;
let cssLoaded = false;
document.fonts.ready.then(() => {
cssLoaded = true;
});
// Navigation Functions
function startGame() {
console.log(currentLevelType);
if (currentLevelType === 'built-in' && window.builtInLevels) {
const level = window.builtInLevels[currentLevelIndex];
window.location.href = `Levels/gameloader.html?level=${level.number}`;
} else if (currentLevelType === 'online' && window.downloadedLevels) {
const level = window.downloadedLevels[currentLevelIndex];
window.location.href = `Levels/gameloader.html?online=true&levelFile=${encodeURIComponent(level.filename)}`;
}
transitionMenu(
document.querySelector(".menu"),
document.querySelector(".level-selector")
);
}
function openLevelEditor() {
window.location.href = "Level Editor/leveleditor.html";
}
function showCredits() {
alert("Credits to Etheblix for the Menu Music, Tranquill Teleportation!\nCredits to RobTopGames for the original game and music! \nCredits to ForeverBound, DJVI, and Step for the amazing original Geometry Dash music!");
}
function levelStore() {
window.location.href = 'Levels/levelstore.html';
}
// Settings Functions
function updateVolumeLabel() {
const volume = document.getElementById("volume-slider").value;
document.getElementById("volume-label").innerText = volume;
menuMusic.volume = volume / 100;
}
// Level System
let currentLevelIndex = 0;
let maxLevelIndex = 0;
let loadingStarted = false;
let currentLevelType = 'built-in';
let db;
const DB_NAME = 'TeleporterDashDB';
const DB_VERSION = 2;
const STORE_NAME = 'downloadedLevels';
function initDB() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onerror = () => {
console.error('Failed to open database:', request.error);
reject(request.error);
};
request.onsuccess = (event) => {
db = event.target.result;
console.log('Database opened successfully');
resolve(db);
};
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(STORE_NAME)) {
db.createObjectStore(STORE_NAME, { keyPath: 'filename' });
console.log('Object store created');
}
if (!db.objectStoreNames.contains('scores')) {
db.createObjectStore('scores');
console.log("Scores object store created");
}
};
});
}
async function getDownloadedLevels() {
if (!db) {
try {
await initDB();
} catch (error) {
console.error('Failed to initialize database:', error);
return [];
}
}
return new Promise((resolve, reject) => {
try {
const transaction = db.transaction([STORE_NAME], 'readonly');
const store = transaction.objectStore(STORE_NAME);
const request = store.getAll();
request.onsuccess = () => {
resolve(request.result || []);
};
request.onerror = () => {
console.error('Error fetching downloaded levels:', request.error);
reject(request.error);
};
} catch (error) {
console.error('Error in transaction:', error);
reject(error);
}
});
}
const BUILT_IN_LEVELS = [
{ filename: 'level1.js', number: 1 },
{ filename: 'level2.js', number: 2 },
{ filename: 'level3.js', number: 3 }
];
async function scanForLevels() {
return BUILT_IN_LEVELS;
}
async function loadBuiltInLevelRegistry() {
loadingStarted = true;
currentLevelType = 'built-in';
const levelSelector = document.querySelector('.built-in-levels .level-selector');
levelSelector.innerHTML = `
<h1>Select Level</h1>
<div class="level-display">
<div class="level-preview"></div>
<div class="level-info"></div>
<div class="level-navigation">
<button class="nav-button prev" onclick="handleLevelNavigation(-1)">Previous</button>
<button onclick="startGame()">Play Level</button>
<button class="nav-button next" onclick="handleLevelNavigation(1)">Next</button>
</div>
</div>
<button class="back-button" onclick="handleMenuTransition('built-in-levels', 'menu')">Back to Menu</button>
`;
const levels = await scanForLevels();
window.builtInLevels = levels;
currentLevelIndex = 0;
maxLevelIndex = levels.length - 1;
await updateLevelDisplay();
}
async function loadOnlineLevelRegistry() {
currentLevelType = 'online';
const levelSelector = document.querySelector('.online-levels .level-selector');
levelSelector.innerHTML = `
<h1>Downloaded Levels</h1>
<div class="level-display">
<div class="level-preview"></div>
<div class="level-info"></div>
<div class="level-navigation">
<button class="nav-button prev" onclick="handleLevelNavigation(-1)">Previous</button>
<button onclick="startGame()">Play Level</button>
<button class="nav-button next" onclick="handleLevelNavigation(1)">Next</button>
</div>
</div>
<button class="back-button" onclick="handleMenuTransition('online-levels', 'menu')">Back to Menu</button>
`;
try {
const levels = await getDownloadedLevels();
if (levels.length === 0) {
const levelDisplay = document.querySelector('.online-levels .level-display');
levelDisplay.innerHTML = '<p class="no-levels">No downloaded levels found.<br>Visit the Level Store to download levels!</p>';
return;
}
window.downloadedLevels = levels;
currentLevelIndex = 0;
maxLevelIndex = levels.length - 1;
await updateLevelDisplay();
} catch (error) {
console.error('Error loading downloaded levels:', error);
const levelDisplay = document.querySelector('.online-levels .level-display');
levelDisplay.innerHTML = '<p class="error-message">Error loading levels.<br>Please try again later.</p>';
}
}
async function handleLevelNavigation(direction) {
const newIndex = currentLevelIndex + direction;
if (newIndex >= 0 && newIndex <= maxLevelIndex) {
currentLevelIndex = newIndex;
await updateLevelDisplay();
}
}
const TILE_COLORS = {
'-1': '#ff6b6b',
'-2': '#4ecdc4',
'-3': '#45b7d1',
'-4': '#96ceb4',
'-5': '#ff9f1c',
'-6': '#ffbe0b',
'-7': '#ff006e',
'-8': '#8338ec',
'-9': '#3a86ff',
'0': '#000000',
'1': '#45b7d1',
'2': '#ff6b6b',
'3': '#9932CC',
'4': '#00ff00'
};
function parseBlockProperties(block) {
if (typeof block !== 'string') {
return { type: block, color: null, rotation: 0 };
}
const props = block.split('/');
const type = parseInt(props[0]);
let color = null;
let rotation = 0;
props.slice(1).forEach(prop => {
if (prop.startsWith('-')) {
color = TILE_COLORS[prop];
} else if (prop.startsWith('@')) {
rotation = parseInt(prop.substring(1));
}
});
return { type, color, rotation };
}
function drawSpike(ctx, x, y, size, color = '#ff6b6b', rotation = 0) {
ctx.save();
ctx.translate(x + size/2, y + size/2);
ctx.rotate((rotation * Math.PI) / 180);
ctx.beginPath();
ctx.moveTo(-size/2, size/2);
ctx.lineTo(0, -size/2);
ctx.lineTo(size/2, size/2);
ctx.closePath();
ctx.fillStyle = color;
ctx.fill();
ctx.restore();
}
function drawTeleporter(ctx, x, y, size, color = '#9932CC') {
const radius = size / 3;
ctx.beginPath();
ctx.arc(x + size/2, y + size/2, radius, 0, Math.PI * 2);
ctx.fillStyle = color;
ctx.fill();
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 2;
ctx.stroke();
}
function drawFinishLine(ctx, x, y, size) {
ctx.fillStyle = '#00ff00';
ctx.fillRect(x, y, size/4, size);
}
function generateLevelPreview(matrix) {
const scale = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--level-selector-scale')) || 1;
const contentScale = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--content-scale')) || 1;
const canvas = document.createElement('canvas');
canvas.width = Math.min(40 * scale * contentScale * window.innerWidth / 100, 500 * contentScale);
canvas.height = Math.min(30 * scale * contentScale * window.innerWidth / 100, 375 * contentScale);
const ctx = canvas.getContext('2d');
const previewRows = Math.min(20, matrix.length);
const previewCols = Math.min(17, matrix[0].length);
const tileSize = Math.min(
canvas.width / previewCols,
canvas.height / previewRows
);
const offsetX = (canvas.width - (previewCols * tileSize)) / 2;
const offsetY = (canvas.height - (previewRows * tileSize)) / 2;
ctx.fillStyle = '#1a1a2e';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let y = 0; y < previewRows; y++) {
for (let x = 0; x < previewCols; x++) {
const block = matrix[y][x];
const { type, color, rotation } = parseBlockProperties(block);
const tileX = offsetX + (x * tileSize);
const tileY = offsetY + (y * tileSize);
if (type === 0) continue;
switch(type) {
case 1:
ctx.fillStyle = color || TILE_COLORS['1'];
ctx.fillRect(tileX, tileY, tileSize, tileSize);
break;
case 2:
drawSpike(ctx, tileX, tileY, tileSize, color || TILE_COLORS['2'], rotation);
break;
case 3:
drawTeleporter(ctx, tileX, tileY, tileSize, color || TILE_COLORS['3']);
break;
case 4:
drawFinishLine(ctx, tileX, tileY, tileSize);
break;
}
}
}
return canvas;
}
function drawLevelPreview(level) {
if (!level || !level.matrix) {
console.error('Invalid level data for preview:', level);
return null;
}
try {
return generateLevelPreview(level.matrix);
} catch (error) {
console.error('Error generating level preview:', error);
return null;
}
}
async function updateLevelDisplay() {
const container = document.querySelector(
currentLevelType === 'built-in' ?
'.built-in-levels .level-selector' :
'.online-levels .level-selector'
);
if (!container) return;
const levelDisplay = container.querySelector('.level-display');
if (!levelDisplay) {
container.innerHTML = `
<h1>${currentLevelType === 'built-in' ? 'Select Level' : 'Downloaded Levels'}</h1>
<div class="level-display">
<div class="level-preview"></div>
<div class="level-info"></div>
<div class="level-navigation">
<button class="nav-button prev" onclick="handleLevelNavigation(-1)">Previous</button>
<button onclick="startGame()">Play Level</button>
<button class="nav-button next" onclick="handleLevelNavigation(1)">Next</button>
</div>
</div>
<button class="back-button" onclick="handleMenuTransition('${currentLevelType === 'built-in' ? 'built-in-levels' : 'online-levels'}', 'menu')">Back to Menu</button>
`;
}
try {
let levelData;
if (currentLevelType === 'built-in') {
const script = document.createElement('script');
script.src = `Levels/${BUILT_IN_LEVELS[currentLevelIndex].filename}`;
await new Promise((resolve, reject) => {
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
levelData = window.levelData;
document.head.removeChild(script);
window.levelData = null;
} else {
levelData = window.downloadedLevels[currentLevelIndex];
}
if (!levelData) throw new Error('No level data found');
const preview = container.querySelector('.level-preview');
preview.innerHTML = '';
const previewCanvas = drawLevelPreview(levelData);
if (previewCanvas) {
preview.appendChild(previewCanvas);
}
const info = container.querySelector('.level-info');
info.innerHTML = '';
const title = document.createElement('h3');
title.className = 'level-title';
title.textContent = levelData.title ||
(currentLevelType === 'built-in' ?
`Level ${BUILT_IN_LEVELS[currentLevelIndex].number}` :
levelData.filename.replace('.js', '')
);
const stats = document.createElement('div');
stats.className = 'level-stats';
stats.innerHTML = `
<span>Difficulty: ${levelData.difficulty || 'Normal'}</span>
<span>Author: ${levelData.author || 'Unknown'}</span>
`;
info.appendChild(title);
info.appendChild(stats);
const prevButton = container.querySelector('.nav-button.prev');
const nextButton = container.querySelector('.nav-button.next');
if (prevButton) prevButton.disabled = currentLevelIndex === 0;
if (nextButton) nextButton.disabled = currentLevelIndex === maxLevelIndex;
} catch (error) {
console.error('Error updating level display:', error);
showLoadingError('Failed to load level data', currentLevelType === 'built-in');
}
}
// Error Handling
function showError(message, container) {
const errorElement = document.createElement('div');
errorElement.className = 'error-message';
errorElement.innerHTML = `
<p>${message}</p>
<button onclick="this.parentElement.remove()">OK</button>
`;
container.appendChild(errorElement);
}
function showLoadingError(message, isBuiltIn = false) {
const container = document.querySelector(
isBuiltIn ? '.built-in-levels #current-level' : '.online-levels #current-level'
);
if (container) {
container.innerHTML = `<p>${message}</p>`;
}
}
// Resizing Function
function updateMenuScale() {
const width = window.innerWidth;
const height = window.innerHeight;
const zoomLevel = window.devicePixelRatio || 1;
// Menu scaling (unchanged)
const menuElement = document.querySelector('.menu');
if (menuElement) {
const baseMenuWidth = 450;
const baseMenuHeight = 300;
const menuWidthScale = (width / zoomLevel * 0.7) / baseMenuWidth;
const menuHeightScale = (height / zoomLevel * 0.7) / baseMenuHeight;
let menuScale = Math.min(menuWidthScale, menuHeightScale);
menuScale = Math.max(0.6, Math.min(menuScale, 1.2));
document.documentElement.style.setProperty('--menu-scale', menuScale);
}
// Level selector scaling
const levelSelectorElement = document.querySelector('.level-selector');
if (levelSelectorElement) {
const baseLevelWidth = 1200;
const baseLevelHeight = 1000; // Increased for taller mobile
const levelWidthScale = (width / zoomLevel * 1.0) / baseLevelWidth;
const levelHeightScale = (height / zoomLevel * 1.0) / baseLevelHeight;
let levelScale;
if (width < 768) { // Mobile
const levelHeightScale = (height / zoomLevel * 1.0) / baseLevelHeight; // 100% height ratio
levelScale = Math.min(levelWidthScale, levelHeightScale);
levelScale = Math.max(1.8, Math.min(levelScale, 2.5)); // Large mobile size
levelSelectorElement.style.maxHeight = '100vh'; // Full height on mobile
} else { // Desktop
const levelHeightScale = (height / zoomLevel * 0.9) / baseLevelHeight; // Reduced to 40% height ratio
levelScale = Math.min(levelWidthScale, levelHeightScale);
levelScale = Math.max(1.0, Math.min(levelScale, 1.6)); // Desktop range
levelSelectorElement.style.maxHeight = `${Math.min(baseLevelHeight * levelScale, height * 0.9)}px`; // Smaller desktop height
}
// Adjust for overflow
const scaledWidth = baseLevelWidth * levelScale;
const scaledHeight = baseLevelHeight * levelScale;
if (scaledWidth > width || scaledHeight > height) {
const overflowScale = Math.min(width / scaledWidth, height / scaledHeight);
levelScale *= overflowScale;
}
document.documentElement.style.setProperty('--level-selector-scale', levelScale);
// Adjust content scale: smaller on desktop, normal on mobile
const contentScale = (width >= 768) ? 0.6 : 1.0; // Reduce content size on desktop
document.documentElement.style.setProperty('--content-scale', contentScale);
}
}
// Initial scale setup
window.addEventListener('DOMContentLoaded', () => {
setTimeout(updateMenuScale, 100);
});
// Update scale on resize with debounce
let resizeTimeout;
window.addEventListener('resize', () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(updateMenuScale, 100);
});
// Clear Data Function
function clearData() {
if (confirm("Are you sure you want to clear all data? This action cannot be undone.")) {
localStorage.clear();
indexedDB.databases().then(databases => {
databases.forEach(db => {
indexedDB.deleteDatabase(db.name);
});
}).catch(error => {
console.error('Error deleting databases:', error);
});
if (confirm("Do you want to leave the page? (Clicking No/Cancel will reload the page)")) {
window.location.href = "https://github.com/NellowTCS/TeleporterDash/";
} else {
location.reload();
}
}
}
document.addEventListener('DOMContentLoaded', initDB);
</script>
</body>
</html>