-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1962 lines (1693 loc) · 75 KB
/
index.html
File metadata and controls
1962 lines (1693 loc) · 75 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
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FPV Racing Simulator - Betaflight Actual Rates</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<style>
:root {
--primary: #2196F3;
--accent: #FFC107;
--bg-light: #f5f5f5;
--surface-light: #ffffff;
--text-light: #333333;
--bg-dark: #121212;
--surface-dark: #1e1e1e;
--text-dark: #e0e0e0;
--border-radius: 12px;
}
body {
margin: 0;
font-family: 'Roboto', sans-serif;
overflow: hidden;
display: flex;
height: 100vh;
background-color: var(--bg-light);
color: var(--text-light);
transition: background-color 0.3s, color 0.3s;
}
body.dark-mode {
background-color: var(--bg-dark);
color: var(--text-dark);
}
/* --- SIDEBAR --- */
#sidebar {
width: 340px;
padding: 20px;
background-color: var(--surface-light);
box-shadow: 2px 0 10px rgba(0,0,0,0.05);
display: flex;
flex-direction: column;
gap: 15px;
z-index: 20;
overflow-y: auto;
transition: background-color 0.3s;
}
body.dark-mode #sidebar { background-color: var(--surface-dark); }
h2 { margin: 0; font-weight: 700; font-size: 1.5rem; }
h3 { margin: 0 0 8px 0; font-size: 0.85rem; text-transform: uppercase; color: var(--primary); letter-spacing: 0.5px; }
.control-group {
background: rgba(0,0,0,0.04);
padding: 12px;
border-radius: var(--border-radius);
}
body.dark-mode .control-group { background: rgba(255,255,255,0.05); }
.input-row {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
.input-row label { font-size: 0.8rem; flex: 1; }
.input-row input {
width: 70px;
padding: 6px;
border: 1px solid #ccc;
border-radius: 6px;
background: transparent;
color: inherit;
text-align: right;
}
/* --- BUTTONS --- */
button {
padding: 12px;
border: none;
border-radius: 8px;
cursor: pointer;
font-weight: 600;
transition: 0.2s;
width: 100%;
margin-bottom: 5px;
}
button:hover { filter: brightness(1.1); }
.btn-primary { background-color: var(--primary); color: white; }
.btn-secondary { background-color: #607D8B; color: white; }
.btn-accent { background-color: var(--accent); color: #333; }
/* --- CANVAS --- */
#canvas-container {
flex-grow: 1;
position: relative;
background: radial-gradient(circle at center, #e0e0e0 0%, #bdbdbd 100%);
}
body.dark-mode #canvas-container {
background: radial-gradient(circle at center, #2c2c2c 0%, #000000 100%);
}
/* --- MAPPING MODAL --- */
#mapping-modal {
position: fixed;
top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.6);
z-index: 100;
display: none; /* Hidden by default */
align-items: center;
justify-content: center;
backdrop-filter: blur(4px);
}
.modal-content {
background: var(--surface-light);
padding: 25px;
border-radius: 16px;
width: 450px;
max-width: 90%;
color: var(--text-light);
box-shadow: 0 10px 25px rgba(0,0,0,0.3);
}
body.dark-mode .modal-content {
background: var(--surface-dark);
color: var(--text-dark);
}
.map-row {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 1px solid rgba(0,0,0,0.1);
}
body.dark-mode .map-row { border-bottom: 1px solid rgba(255,255,255,0.1); }
.map-label { width: 60px; font-weight: bold; }
.map-select { flex: 1; padding: 8px; border-radius: 6px; }
.map-invert { display: flex; align-items: center; gap: 5px; font-size: 0.8rem; }
.axis-viz {
width: 100px;
height: 10px;
background: #ddd;
border-radius: 5px;
position: relative;
overflow: hidden;
}
.axis-bar {
position: absolute;
top: 0; left: 50%;
height: 100%;
width: 0%; /* Dynamic */
background: var(--primary);
transition: width 0.05s, left 0.05s;
}
/* --- NOTIFICATIONS --- */
#notification-area {
position: absolute;
top: 20px; right: 20px;
display: flex;
flex-direction: column;
gap: 10px;
z-index: 200;
}
.toast {
background-color: var(--surface-light);
color: var(--text-light);
padding: 12px 20px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
border-left: 5px solid var(--primary);
animation: slideIn 0.3s ease-out;
}
body.dark-mode .toast { background-color: var(--surface-dark); color: var(--text-dark); }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
/* --- HUD OVERLAY --- */
#hud {
position: absolute;
top: 20px;
left: 20px;
font-family: 'Roboto Mono', monospace;
font-size: 14px;
color: white;
text-shadow: 1px 1px 3px rgba(0,0,0,0.8);
pointer-events: none;
z-index: 50;
}
.hud-row {
margin-bottom: 5px;
display: flex;
gap: 15px;
}
.hud-label { opacity: 0.7; }
.hud-value { font-weight: bold; min-width: 60px; }
/* --- CRASH OVERLAY --- */
#crash-overlay {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(180, 0, 0, 0.6);
display: none;
align-items: center;
justify-content: center;
flex-direction: column;
z-index: 80;
backdrop-filter: blur(3px);
}
#crash-overlay h1 {
font-size: 4rem;
color: white;
margin: 0;
text-shadow: 3px 3px 6px rgba(0,0,0,0.5);
}
#crash-overlay p {
font-size: 1.2rem;
color: white;
opacity: 0.9;
}
/* --- COUNTDOWN OVERLAY --- */
#countdown-overlay {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
display: none;
align-items: center;
justify-content: center;
z-index: 75;
pointer-events: none;
}
#countdown-text {
font-size: 8rem;
font-weight: bold;
color: white;
text-shadow: 4px 4px 10px rgba(0,0,0,0.7);
}
/* --- RACE INFO --- */
#race-info {
position: absolute;
top: 20px;
right: 380px;
background: rgba(0,0,0,0.6);
padding: 15px 20px;
border-radius: 10px;
color: white;
font-family: 'Roboto Mono', monospace;
z-index: 50;
display: none;
}
#race-info .time { font-size: 2rem; font-weight: bold; }
#race-info .lap-info { font-size: 0.9rem; opacity: 0.8; margin-top: 5px; }
#race-info .gate-info { font-size: 1rem; margin-top: 8px; color: #4CAF50; }
/* --- MODE BUTTONS --- */
.mode-btn-group {
display: flex;
gap: 8px;
margin-bottom: 10px;
}
.mode-btn {
flex: 1;
padding: 10px;
font-size: 0.85rem;
}
.mode-btn.active {
background-color: var(--accent) !important;
color: #333 !important;
}
/* --- KEYBOARD HINTS --- */
.key-hints {
font-size: 0.75rem;
opacity: 0.6;
margin-top: 10px;
line-height: 1.6;
}
.key {
background: rgba(255,255,255,0.2);
padding: 2px 6px;
border-radius: 4px;
font-family: monospace;
}
</style>
</head>
<body>
<div id="sidebar">
<div>
<h2>FPV Racing Sim</h2>
<p style="font-size: 0.8rem; opacity: 0.7; margin-top:5px;">Betaflight Actual Rates</p>
</div>
<div class="control-group">
<h3>Game Mode</h3>
<div class="mode-btn-group">
<button id="mode-freefly" class="btn-secondary mode-btn active">Free Flight</button>
<button id="mode-race" class="btn-secondary mode-btn">Race</button>
</div>
<div class="key-hints">
<span class="key">R</span> Reset
<span class="key">Space</span> Start Race<br>
<span class="key">Esc</span> Free Flight<br>
<span class="key">W</span>/<span class="key">S</span> Camera Tilt
</div>
</div>
<button id="open-mapping" class="btn-accent">Configure Controller</button>
<div class="control-group">
<h3>Roll</h3>
<div class="input-row"><label>Center Sens</label><input type="number" id="roll-center" value="70"></div>
<div class="input-row"><label>Max Rate</label><input type="number" id="roll-max" value="670"></div>
<div class="input-row"><label>Expo</label><input type="number" id="roll-expo" value="0.55" step="0.01"></div>
</div>
<div class="control-group">
<h3>Pitch</h3>
<div class="input-row"><label>Center Sens</label><input type="number" id="pitch-center" value="70"></div>
<div class="input-row"><label>Max Rate</label><input type="number" id="pitch-max" value="670"></div>
<div class="input-row"><label>Expo</label><input type="number" id="pitch-expo" value="0.55" step="0.01"></div>
</div>
<div class="control-group">
<h3>Yaw</h3>
<div class="input-row"><label>Center Sens</label><input type="number" id="yaw-center" value="70"></div>
<div class="input-row"><label>Max Rate</label><input type="number" id="yaw-max" value="670"></div>
<div class="input-row"><label>Expo</label><input type="number" id="yaw-expo" value="0.55" step="0.01"></div>
</div>
<div style="margin-top: auto;">
<button id="reset-cam" class="btn-secondary">Reset View</button>
<button id="theme-toggle" class="btn-primary">Toggle Dark Mode</button>
</div>
</div>
<div id="canvas-container">
<!-- HUD Overlay -->
<div id="hud">
<div class="hud-row">
<span class="hud-label">Speed:</span>
<span class="hud-value" id="hud-speed">0</span>
<span>m/s</span>
</div>
<div class="hud-row">
<span class="hud-label">Alt:</span>
<span class="hud-value" id="hud-alt">0</span>
<span>m</span>
</div>
<div class="hud-row">
<span class="hud-label">Throttle:</span>
<span class="hud-value" id="hud-throttle">0</span>
<span>%</span>
</div>
<div class="hud-row">
<span class="hud-label">Cam Tilt:</span>
<span class="hud-value" id="hud-tilt">15</span>
<span>°</span>
</div>
</div>
<!-- Race Info Panel -->
<div id="race-info">
<div class="time" id="race-time">00:00.000</div>
<div class="lap-info">Best: <span id="best-lap">--:--</span></div>
<div class="gate-info" id="gate-counter">Gate 0 / 0</div>
</div>
<!-- Crash Overlay -->
<div id="crash-overlay">
<h1>CRASHED!</h1>
<p>Press <strong>R</strong> to reset</p>
</div>
<!-- Countdown Overlay -->
<div id="countdown-overlay">
<div id="countdown-text">3</div>
</div>
</div>
<div id="notification-area"></div>
<div id="mapping-modal">
<div class="modal-content">
<h2 style="margin-bottom: 20px;">Controller Mapping</h2>
<p style="font-size: 0.9rem; margin-bottom: 20px; opacity: 0.8;">
Move your sticks to identify axes. Assign them to the correct functions.
</p>
<div id="mapping-rows"></div>
<button id="close-mapping" class="btn-primary" style="margin-top: 20px;">Save & Close</button>
</div>
</div>
<script>
// ============================================================
// FPV RACING SIMULATOR - BETAFLIGHT ACTUAL RATES
// ============================================================
// --- 1. STATE & CONFIGURATION ---
let config = {
mapping: {
roll: { axis: 2, invert: false },
pitch: { axis: 3, invert: false },
yaw: { axis: 0, invert: true },
throttle: { axis: 1, invert: true }
}
};
// Load saved config
const savedConfig = localStorage.getItem('bf_rate_config');
if (savedConfig) {
try {
const parsed = JSON.parse(savedConfig);
if (parsed.mapping) config.mapping = parsed.mapping;
} catch(e) { console.log('Config load error'); }
}
function saveConfig() {
localStorage.setItem('bf_rate_config', JSON.stringify(config));
}
// --- 2. PHYSICS STATE ---
const physics = {
position: new THREE.Vector3(0, 5, -120),
velocity: new THREE.Vector3(0, 0, 0),
gravity: 9.81,
dragCoeff: 0.4, // Air resistance
maxThrust: 30, // Max thrust force (gives good power-to-weight)
crashed: false,
droneRadius: 0.5
};
const spawnPoint = new THREE.Vector3(0, 5, -120); // Start in clearing in the forest
// Thrust works like real acro:
// - Thrust vector points "up" from the drone's perspective
// - Pitch forward = thrust tilts forward = drone accelerates forward
// - At hover, ~40% throttle counters gravity
// --- 3. GAME STATE ---
const gameState = {
mode: 'freefly', // 'freefly' | 'countdown' | 'racing' | 'crashed'
raceStartTime: 0,
currentLapTime: 0,
currentGate: 0,
totalGates: 0,
lapTimes: [],
bestLap: null
};
// Load best lap from localStorage
const savedBestLap = localStorage.getItem('fpv_best_lap');
if (savedBestLap) gameState.bestLap = parseFloat(savedBestLap);
// --- 3b. MULTIPLAYER STATE ---
const multiplayer = {
connected: false,
playerId: null,
playerName: 'Player',
socket: null,
otherPlayers: new Map(),
npcs: [],
otherDrones: new Map() // THREE.js objects for other players/NPCs
};
// Drone colors for other players
const playerColors = [0xFF5733, 0x33FF57, 0x3357FF, 0xFF33F5, 0xF5FF33, 0x33FFF5, 0xFF8833, 0x8833FF];
// Create a simple drone mesh for other players
function createOtherDrone(color) {
const group = new THREE.Group();
const bodyMat = new THREE.MeshStandardMaterial({ color: color, roughness: 0.4, metalness: 0.6 });
const body = new THREE.Mesh(new THREE.BoxGeometry(0.6, 0.15, 0.3), bodyMat);
body.castShadow = true;
group.add(body);
// Arms
const armMat = new THREE.MeshStandardMaterial({ color: 0x333333 });
const arm1 = new THREE.Mesh(new THREE.BoxGeometry(0.08, 0.05, 1.5), armMat);
arm1.rotation.y = Math.PI / 4;
const arm2 = new THREE.Mesh(new THREE.BoxGeometry(0.08, 0.05, 1.5), armMat);
arm2.rotation.y = -Math.PI / 4;
group.add(arm1);
group.add(arm2);
return group;
}
// Connect to multiplayer server
function connectMultiplayer() {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${protocol}//${window.location.host}`;
try {
multiplayer.socket = new WebSocket(wsUrl);
multiplayer.socket.onopen = () => {
multiplayer.connected = true;
showNotification('Connected to server!');
};
multiplayer.socket.onmessage = (event) => {
const data = JSON.parse(event.data);
handleServerMessage(data);
};
multiplayer.socket.onclose = () => {
multiplayer.connected = false;
showNotification('Disconnected from server');
// Try to reconnect after 3 seconds
setTimeout(connectMultiplayer, 3000);
};
multiplayer.socket.onerror = (error) => {
console.log('WebSocket error - running in single player mode');
};
} catch (e) {
console.log('WebSocket not available - single player mode');
}
}
function handleServerMessage(data) {
switch (data.type) {
case 'welcome':
multiplayer.playerId = data.playerId;
multiplayer.playerName = data.playerName;
showNotification(`Welcome ${data.playerName}!`);
// Initialize other players
data.players.forEach(p => {
if (p.id !== multiplayer.playerId) {
addOtherPlayer(p);
}
});
// Initialize NPCs
data.npcs.forEach(npc => addNPC(npc));
break;
case 'playerJoined':
addOtherPlayer(data.player);
showNotification(`${data.player.name} joined`);
break;
case 'playerLeft':
removeOtherPlayer(data.playerId);
break;
case 'gameState':
// Update other players
data.players.forEach(p => {
if (p.id !== multiplayer.playerId) {
updateOtherPlayer(p);
}
});
// Update NPCs
data.npcs.forEach(npc => updateNPC(npc));
break;
case 'playerCrashed':
// Could add crash effects for other players here
break;
case 'nameChanged':
const player = multiplayer.otherPlayers.get(data.playerId);
if (player) {
player.name = data.name;
}
break;
}
}
function addOtherPlayer(playerData) {
multiplayer.otherPlayers.set(playerData.id, playerData);
const color = playerColors[playerData.id % playerColors.length];
const drone = createOtherDrone(color);
drone.position.set(playerData.position.x, playerData.position.y, playerData.position.z);
scene.add(drone);
multiplayer.otherDrones.set(playerData.id, drone);
}
function removeOtherPlayer(playerId) {
multiplayer.otherPlayers.delete(playerId);
const drone = multiplayer.otherDrones.get(playerId);
if (drone) {
scene.remove(drone);
multiplayer.otherDrones.delete(playerId);
}
}
function updateOtherPlayer(playerData) {
const drone = multiplayer.otherDrones.get(playerData.id);
if (drone) {
// Smooth interpolation
drone.position.lerp(
new THREE.Vector3(playerData.position.x, playerData.position.y, playerData.position.z),
0.3
);
drone.rotation.set(playerData.rotation.x, playerData.rotation.y, playerData.rotation.z);
drone.visible = !playerData.crashed;
} else if (!playerData.crashed) {
addOtherPlayer(playerData);
}
}
function addNPC(npcData) {
const color = 0x888888; // Grey for NPCs
const drone = createOtherDrone(color);
drone.position.set(npcData.position.x, npcData.position.y, npcData.position.z);
scene.add(drone);
multiplayer.otherDrones.set(npcData.id, drone);
}
function updateNPCDrone(npcData) {
let drone = multiplayer.otherDrones.get(npcData.id);
if (!drone && !npcData.crashed) {
addNPC(npcData);
drone = multiplayer.otherDrones.get(npcData.id);
}
if (drone) {
drone.position.lerp(
new THREE.Vector3(npcData.position.x, npcData.position.y, npcData.position.z),
0.3
);
drone.rotation.set(npcData.rotation.x, npcData.rotation.y, npcData.rotation.z);
drone.visible = !npcData.crashed;
}
}
function sendPlayerUpdate() {
if (multiplayer.socket && multiplayer.socket.readyState === WebSocket.OPEN) {
multiplayer.socket.send(JSON.stringify({
type: 'update',
position: {
x: physics.position.x,
y: physics.position.y,
z: physics.position.z
},
rotation: {
x: droneGroup.rotation.x,
y: droneGroup.rotation.y,
z: droneGroup.rotation.z
},
velocity: {
x: physics.velocity.x,
y: physics.velocity.y,
z: physics.velocity.z
},
crashed: physics.crashed
}));
}
}
// Try to connect when page loads
setTimeout(connectMultiplayer, 1000);
// --- 4. CAMERA CONTROLLER ---
const cameraController = {
offset: new THREE.Vector3(0, 0.12, 0.35), // Front of drone, between front props
tiltAngle: 15, // Camera tilt in degrees (positive = tilted up)
minTilt: -10, // Min tilt (looking down)
maxTilt: 45, // Max tilt (looking up, for fast forward flight)
};
// --- 5. THEME & UI ---
const body = document.body;
const themeBtn = document.getElementById('theme-toggle');
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
body.classList.add('dark-mode');
}
themeBtn.addEventListener('click', () => body.classList.toggle('dark-mode'));
function showNotification(msg) {
const area = document.getElementById('notification-area');
const toast = document.createElement('div');
toast.className = 'toast';
toast.innerText = msg;
area.appendChild(toast);
setTimeout(() => { toast.style.opacity = '0'; setTimeout(() => toast.remove(), 300); }, 3000);
}
// --- 6. THREE.JS SCENE SETUP ---
const container = document.getElementById('canvas-container');
const scene = new THREE.Scene();
// Create sky dome for static background
const skyGeometry = new THREE.SphereGeometry(500, 32, 32);
const skyCanvas = document.createElement('canvas');
skyCanvas.width = 1024;
skyCanvas.height = 1024;
const skyCtx = skyCanvas.getContext('2d');
// Gradient from top to bottom
const skyGradient = skyCtx.createLinearGradient(0, 0, 0, 1024);
skyGradient.addColorStop(0, '#1565C0'); // Deep blue at top
skyGradient.addColorStop(0.15, '#1976D2'); // Royal blue
skyGradient.addColorStop(0.3, '#42A5F5'); // Light blue
skyGradient.addColorStop(0.5, '#64B5F6'); // Sky blue
skyGradient.addColorStop(0.7, '#90CAF9'); // Powder blue
skyGradient.addColorStop(0.85, '#BBDEFB'); // Very light blue
skyGradient.addColorStop(1, '#E3F2FD'); // Almost white at horizon
skyCtx.fillStyle = skyGradient;
skyCtx.fillRect(0, 0, 1024, 1024);
const skyTexture = new THREE.CanvasTexture(skyCanvas);
const skyMaterial = new THREE.MeshBasicMaterial({
map: skyTexture,
side: THREE.BackSide
});
const skyDome = new THREE.Mesh(skyGeometry, skyMaterial);
scene.add(skyDome);
scene.background = new THREE.Color(0x87CEEB);
scene.fog = new THREE.Fog(0xBBDEFB, 150, 500);
const camera = new THREE.PerspectiveCamera(60, container.clientWidth / container.clientHeight, 0.1, 1000);
camera.position.set(0, 5, -120);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.2;
container.appendChild(renderer.domElement);
// Lighting - Ambient
const ambientLight = new THREE.AmbientLight(0x87CEEB, 0.4);
scene.add(ambientLight);
// Hemisphere light for natural sky/ground lighting
const hemiLight = new THREE.HemisphereLight(0x87CEEB, 0x3d8c40, 0.3);
scene.add(hemiLight);
// Sun light (directional)
const sunLight = new THREE.DirectionalLight(0xfffaf0, 1.2);
sunLight.position.set(100, 150, 80);
sunLight.castShadow = true;
sunLight.shadow.mapSize.width = 4096;
sunLight.shadow.mapSize.height = 4096;
sunLight.shadow.camera.near = 10;
sunLight.shadow.camera.far = 500;
sunLight.shadow.camera.left = -150;
sunLight.shadow.camera.right = 150;
sunLight.shadow.camera.top = 150;
sunLight.shadow.camera.bottom = -150;
scene.add(sunLight);
// Visible Sun sphere
const sunGeom = new THREE.SphereGeometry(15, 32, 32);
const sunMat = new THREE.MeshBasicMaterial({
color: 0xffff80,
transparent: true,
opacity: 0.95
});
const sunMesh = new THREE.Mesh(sunGeom, sunMat);
sunMesh.position.set(100, 150, 80);
scene.add(sunMesh);
// Sun glow
const sunGlowGeom = new THREE.SphereGeometry(25, 32, 32);
const sunGlowMat = new THREE.MeshBasicMaterial({
color: 0xffffcc,
transparent: true,
opacity: 0.3
});
const sunGlow = new THREE.Mesh(sunGlowGeom, sunGlowMat);
sunGlow.position.copy(sunMesh.position);
scene.add(sunGlow);
// --- 7. GROUND PLANE WITH CONCRETE TEXTURE ---
const groundSize = 400;
// Create procedural concrete/asphalt texture
const groundCanvas = document.createElement('canvas');
groundCanvas.width = 512;
groundCanvas.height = 512;
const groundCtx = groundCanvas.getContext('2d');
// Base concrete grey
groundCtx.fillStyle = '#6a6a6a';
groundCtx.fillRect(0, 0, 512, 512);
// Add concrete variation
for (let i = 0; i < 8000; i++) {
const x = Math.random() * 512;
const y = Math.random() * 512;
const shade = Math.floor(90 + Math.random() * 40);
groundCtx.fillStyle = `rgb(${shade}, ${shade}, ${shade + Math.random() * 10})`;
groundCtx.fillRect(x, y, 1 + Math.random() * 2, 1 + Math.random() * 2);
}
// Add some darker patches (oil stains)
for (let i = 0; i < 40; i++) {
const x = Math.random() * 512;
const y = Math.random() * 512;
groundCtx.fillStyle = 'rgba(40, 40, 45, 0.3)';
groundCtx.beginPath();
groundCtx.arc(x, y, 5 + Math.random() * 15, 0, Math.PI * 2);
groundCtx.fill();
}
// Add grid lines (like concrete slabs)
groundCtx.strokeStyle = 'rgba(80, 80, 85, 0.4)';
groundCtx.lineWidth = 2;
for (let i = 0; i < 512; i += 64) {
groundCtx.beginPath();
groundCtx.moveTo(i, 0);
groundCtx.lineTo(i, 512);
groundCtx.stroke();
groundCtx.beginPath();
groundCtx.moveTo(0, i);
groundCtx.lineTo(512, i);
groundCtx.stroke();
}
const groundTexture = new THREE.CanvasTexture(groundCanvas);
groundTexture.wrapS = THREE.RepeatWrapping;
groundTexture.wrapT = THREE.RepeatWrapping;
groundTexture.repeat.set(40, 40);
const groundGeom = new THREE.PlaneGeometry(groundSize, groundSize);
const groundMat = new THREE.MeshStandardMaterial({
map: groundTexture,
roughness: 0.9
});
const ground = new THREE.Mesh(groundGeom, groundMat);
ground.rotation.x = -Math.PI / 2;
ground.position.y = 0;
ground.receiveShadow = true;
scene.add(ground);
// --- 8. DRONE MODEL ---
const droneGroup = new THREE.Group();
const propellers = [];
// Body
const matBody = new THREE.MeshStandardMaterial({ color: 0x222222, roughness: 0.4, metalness: 0.6 });
const matArm = new THREE.MeshStandardMaterial({ color: 0x2196F3, roughness: 0.5, metalness: 0.3 });
const matProp = new THREE.MeshStandardMaterial({ color: 0xdddddd, transparent: true, opacity: 0.6 });
const bodyMesh = new THREE.Mesh(new THREE.BoxGeometry(0.6, 0.15, 0.3), matBody);
bodyMesh.castShadow = true;
droneGroup.add(bodyMesh);
// Arms (X shape)
const arm1 = new THREE.Mesh(new THREE.BoxGeometry(0.08, 0.05, 1.8), matArm);
arm1.rotation.y = Math.PI / 4;
arm1.castShadow = true;
const arm2 = new THREE.Mesh(new THREE.BoxGeometry(0.08, 0.05, 1.8), matArm);
arm2.rotation.y = -Math.PI / 4;
arm2.castShadow = true;
droneGroup.add(arm1);
droneGroup.add(arm2);
// Propellers
const propPositions = [
{ x: -0.6, z: 0.6 }, { x: 0.6, z: -0.6 },
{ x: 0.6, z: 0.6 }, { x: -0.6, z: -0.6 }
];
propPositions.forEach((pos, i) => {
const prop = new THREE.Mesh(new THREE.CylinderGeometry(0.35, 0.35, 0.02, 16), matProp);
prop.position.set(pos.x, 0.08, pos.z);
propellers.push(prop);
droneGroup.add(prop);
});
droneGroup.position.copy(physics.position);
scene.add(droneGroup);
// --- 9. GATES REMOVED - FREE FLIGHT MODE ---
const gates = []; // Empty for compatibility
// --- 10. BLOCKY WORLD ENVIRONMENT ---
const buildings = [];
const trees = []; // Empty - no trees in blocky world
// Vibrant primary color palette (Slow Roads style)
const blockColors = {
red: 0xE53935,
blue: 0x1E88E5,
yellow: 0xFDD835,
green: 0x43A047,
orange: 0xFB8C00,
purple: 0x8E24AA,
cyan: 0x00ACC1,
pink: 0xD81B60,
white: 0xF5F5F5,
darkGrey: 0x424242
};
const colorValues = Object.values(blockColors);
function randomBlockColor() {
return colorValues[Math.floor(Math.random() * (colorValues.length - 2))]; // Exclude white/grey
}
// Glass material for skyscrapers
const glassMat = new THREE.MeshStandardMaterial({
color: 0x88DDFF,
roughness: 0.05,
metalness: 0.95,
transparent: true,
opacity: 0.7
});
// --- 10a. RED BOUNDARY WALLS ---
const boundaryMat = new THREE.MeshStandardMaterial({
color: 0xFF0000,
transparent: true,
opacity: 0.3,
side: THREE.DoubleSide
});
const boundaryHeight = 100;
const boundarySize = groundSize / 2 - 5;
// Create 4 boundary walls
const boundaryWalls = [];
// North wall (+Z)
const northWall = new THREE.Mesh(
new THREE.PlaneGeometry(groundSize, boundaryHeight),
boundaryMat
);
northWall.position.set(0, boundaryHeight / 2, boundarySize);
scene.add(northWall);
boundaryWalls.push({ axis: 'z', value: boundarySize, normal: -1 });
// South wall (-Z)
const southWall = new THREE.Mesh(
new THREE.PlaneGeometry(groundSize, boundaryHeight),
boundaryMat
);
southWall.position.set(0, boundaryHeight / 2, -boundarySize);
southWall.rotation.y = Math.PI;
scene.add(southWall);
boundaryWalls.push({ axis: 'z', value: -boundarySize, normal: 1 });
// East wall (+X)
const eastWall = new THREE.Mesh(
new THREE.PlaneGeometry(groundSize, boundaryHeight),
boundaryMat
);
eastWall.position.set(boundarySize, boundaryHeight / 2, 0);
eastWall.rotation.y = -Math.PI / 2;
scene.add(eastWall);
boundaryWalls.push({ axis: 'x', value: boundarySize, normal: -1 });
// West wall (-X)
const westWall = new THREE.Mesh(
new THREE.PlaneGeometry(groundSize, boundaryHeight),
boundaryMat
);
westWall.position.set(-boundarySize, boundaryHeight / 2, 0);
westWall.rotation.y = Math.PI / 2;
scene.add(westWall);
boundaryWalls.push({ axis: 'x', value: -boundarySize, normal: 1 });
// Boundary collision check
function checkBoundaryCollision() {
const margin = 2;
if (Math.abs(physics.position.x) > boundarySize - margin ||
Math.abs(physics.position.z) > boundarySize - margin) {
return true;
}
return false;
}
// --- 10b. BLOCKY BUILDINGS ---
// Create a simple blocky building
function createBlockyBuilding(x, z, width, depth, height, color) {
const mat = new THREE.MeshStandardMaterial({ color: color, roughness: 0.8 });
const building = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
building.position.set(x, height / 2, z);
building.castShadow = true;
building.receiveShadow = true;
scene.add(building);
buildings.push({
position: new THREE.Vector3(x, 0, z),
width: width,
depth: depth,
height: height