Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
1942 lines (1848 loc) · 87.9 KB
/
Copy pathindex.html
File metadata and controls
1942 lines (1848 loc) · 87.9 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>MultiMagicDungeonWeb — Vibe Jam 2026</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #000; overflow: hidden; font-family: monospace; }
#game-canvas { display: block; }
#hud { position: fixed; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 10; }
#health-bar-wrap { position: absolute; top: 20px; left: 20px; background: rgba(0,0,0,0.6); padding: 8px 12px; border-radius: 4px; }
#health-label { color: #aaa; font-size: 11px; margin-bottom: 4px; }
#health-bar-bg { width: 160px; height: 10px; background: #333; border-radius: 2px; }
#health-bar { height: 10px; background: #e44; border-radius: 2px; transition: width 0.2s; }
#player-count { position: absolute; top: 70px; left: 20px; background: rgba(0,0,0,0.5); color: #888; padding: 4px 10px; border-radius: 4px; font-size: 11px; }
#arrived-from { position: absolute; top: 60px; right: 20px; background: rgba(0,0,0,0.6); color: #0f0; padding: 6px 12px; border-radius: 4px; font-size: 12px; display: none; }
#portal-hint { position: absolute; bottom: 80px; left: 50%; transform: translateX(-50%); background: rgba(0,255,0,0.15); color: #0f0; padding: 8px 20px; border-radius: 4px; font-size: 14px; border: 1px solid #0f0; display: none; text-align: center; }
#spells { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; gap: 12px; }
.spell-slot { width: 60px; height: 60px; background: rgba(0,0,0,0.7); border: 2px solid #555; border-radius: 6px; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #ccc; font-size: 10px; text-align: center; gap: 3px; }
.spell-slot .spell-icon { font-size: 20px; }
.spell-slot.cooling { border-color: #333; opacity: 0.5; }
.spell-slot.ready { border-color: #888; }
#settings-gear { position: absolute; top: 20px; right: 20px; background: rgba(0,0,0,0.6); border: 1px solid #555; color: #ccc; width: 34px; height: 34px; border-radius: 4px; font-size: 18px; cursor: pointer; display: flex; align-items: center; justify-content: center; pointer-events: all; }
#settings-gear:hover { background: rgba(0,0,0,0.8); color: #fff; border-color: #777; }
#settings-panel { position: absolute; top: 62px; right: 20px; background: rgba(0,0,0,0.7); padding: 6px 10px; border-radius: 4px; display: none; gap: 6px; align-items: center; pointer-events: all; }
#settings-panel.open { display: flex; }
#rename-input { background: #111; border: 1px solid #555; color: #fff; padding: 4px 8px; font-size: 12px; font-family: monospace; border-radius: 3px; width: 130px; }
#rename-btn { padding: 4px 10px; background: #555; border: none; color: #fff; font-size: 12px; font-family: monospace; border-radius: 3px; cursor: pointer; }
#rename-btn:hover { background: #777; }
#minimap { position: absolute; top: 120px; right: 20px; width: 180px; height: 180px; background: rgba(0,0,0,0.55); border-radius: 4px; border: 1px solid #444; }
#interact-hint { position: absolute; bottom: 160px; left: 50%; transform: translateX(-50%); background: rgba(60,20,100,0.22); color: #c090ff; padding: 8px 20px; border-radius: 4px; font-size: 14px; border: 1px solid #8040cc; display: none; text-align: center; cursor: pointer; user-select: none; }
#interact-btn { position: fixed; bottom: 120px; left: 50%; transform: translateX(-50%); background: rgba(60,20,100,0.72); color: #c090ff; padding: 12px 28px; border-radius: 8px; font-size: 16px; border: 2px solid #8040cc; display: none; text-align: center; cursor: pointer; user-select: none; touch-action: none; z-index: 100; font-family: monospace; pointer-events: auto; }
#rotate-prompt { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.85); z-index: 200; align-items: center; justify-content: center; color: #fff; font-size: 24px; text-align: center; pointer-events: none; }
#crosshair { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: rgba(255,255,255,0.7); font-size: 18px; user-select: none; }
#game-canvas { touch-action: none; }
#joystick-outer { display: none; position: fixed; bottom: 30px; left: 30px; width: 200px; height: 200px; border-radius: 50%; background: rgba(255,255,255,0.07); border: 2px solid rgba(255,255,255,0.22); touch-action: none; z-index: 100; }
#joystick-knob { position: absolute; width: 80px; height: 80px; border-radius: 50%; background: rgba(255,255,255,0.30); border: 2px solid rgba(255,255,255,0.45); top: 50%; left: 50%; transform: translate(-50%,-50%); pointer-events: none; }
#mobile-spells { display: none; position: fixed; bottom: 30px; right: 20px; flex-direction: column; gap: 10px; z-index: 100; }
.mbtn { width: 70px; height: 70px; border-radius: 10px; background: rgba(0,0,0,0.72); border: 2px solid #888; color: #fff; font-size: 26px; display: flex; align-items: center; justify-content: center; touch-action: none; user-select: none; cursor: pointer; }
.mbtn.cooling { opacity: 0.4; border-color: #444; }
</style>
</head>
<body>
<canvas id="game-canvas"></canvas>
<div id="joystick-outer"><div id="joystick-knob"></div></div>
<div id="mobile-spells">
<div class="mbtn" id="mbtn-fireball">🔥</div>
<div class="mbtn" id="mbtn-frostbolt">❄️</div>
<div class="mbtn" id="mbtn-telekinesis">🌀</div>
</div>
<div id="hud">
<div id="health-bar-wrap">
<div id="health-label">HP</div>
<div id="health-bar-bg"><div id="health-bar" style="width:100%"></div></div>
</div>
<div id="player-count">1 player in dungeon</div>
<div id="arrived-from"></div>
<div id="portal-hint">↑ VIBE JAM 2026 PORTAL — walk in to travel</div>
<canvas id="minimap" width="180" height="180"></canvas>
<div id="interact-hint"></div>
<button id="interact-btn"></button>
<div id="spells">
<div class="spell-slot ready" id="spell-0"><span class="spell-icon">🔥</span><span>Fireball</span><span style="color:#888;font-size:9px">LClick</span></div>
<div class="spell-slot ready" id="spell-1"><span class="spell-icon">❄️</span><span>Frostbolt</span><span style="color:#888;font-size:9px">RClick</span></div>
<div class="spell-slot ready" id="spell-2"><span class="spell-icon">🌀</span><span>Telekinesis</span><span style="color:#888;font-size:9px">E</span></div>
</div>
<div id="crosshair">+</div>
<div id="rotate-prompt">↻ Rotate for best experience</div>
<button id="settings-gear" title="Settings">⚙</button>
<div id="settings-panel"><input id="rename-input" type="text" placeholder="Rename…" maxlength="24" /><button id="rename-btn">Set</button></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/loaders/GLTFLoader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/utils/SkeletonUtils.js"></script>
<script>
// ============================================================
// MultiMagicDungeonWeb - Vibe Jam 2026
// Phase 1: Three.js dungeon + portals + WebSocket multiplayer
// ============================================================
// === CONSTANTS ===
const WS_URL = 'ws://5.161.208.234:8080'; // production Hetzner server
const PORTAL_EXIT_URL = 'https://jam.pieter.com/portal/2026';
const PORTAL_PRELOAD_DIST = 50;
const PORTAL_ENTER_DIST = 18; // 2D XZ distance
const START_PORTAL_DELAY = 5000;
const PLAYER_SPEED = 14; // units/sec
const PLAYER_HEIGHT = 5;
const HP_MAX = 100;
const SEND_MOVE_HZ = 20;
// Portal geometry
const PORTAL_RING_RADIUS = 4.0; // torus major radius (units)
const PORTAL_RING_TUBE = 0.25; // torus tube radius — thin elegant ring
const PORTAL_VOID_EMISSIVE = 0.06; // void circle emissive — faint inner colour hint
const PORTAL_EMISSIVE_INTENSITY = 2.0; // ring torus emissive — bright, this is the visible beacon
const PORTAL_LIGHT_INTENSITY = 1.5; // point light — reduced to avoid green-washing the dungeon
const PORTAL_LIGHT_DIST = 40; // point light falloff distance
// Portal animation
const PORTAL_RING_SPIN = 0.8; // rad/sec ring rotation speed
const PORTAL_VOID_SPIN = 0.5; // rad/sec void counter-rotation speed
const PORTAL_PULSE_AMP = 0.06; // scale pulse amplitude (±6%)
const PORTAL_PULSE_FREQ = 2.0; // scale pulse frequency (rad/sec)
// Torch flicker
const TORCH1_BASE_INTENSITY = 1.3;
const TORCH1_FLICKER_AMP = 0.2;
const TORCH1_FLICKER_FREQ = 7; // rad/sec
const TORCH2_BASE_INTENSITY = 0.9;
const TORCH2_FLICKER_AMP = 0.15;
const TORCH2_FLICKER_FREQ = 5; // rad/sec
// URL params (Vibe Jam portal spec)
const urlParams = new URLSearchParams(window.location.search);
const incomingPortal = urlParams.get('portal') === 'true';
const incomingRef = urlParams.get('ref') || '';
const incomingUser = urlParams.get('username') || '';
const incomingColor = urlParams.get('color') || 'white';
const incomingSpeed = Math.max(1, Math.min(30, parseFloat(urlParams.get('speed')) || PLAYER_SPEED));
const rawHp = parseInt(urlParams.get('hp'), 10);
const incomingHp = Number.isFinite(rawHp) ? Math.max(0, Math.min(HP_MAX, rawHp)) : HP_MAX;
// Validate ref - hostname pattern only, no protocol injection
const REF_PATTERN = /^[a-zA-Z0-9.-]+(:\d+)?$/;
const safeRef = (incomingRef && REF_PATTERN.test(incomingRef)) ? incomingRef : '';
// ?pos=x,z — teleport player on spawn (used by Playwright tests)
const incomingPos = urlParams.get('pos');
const isMobile = 'ontouchstart' in window || matchMedia('(hover:none)').matches;
// === SPELL SYSTEM ===
// SPELL_DEFS registry - single source of truth for all spell properties
const SPELL_DEFS = {
fireball: { color: 0xff6600, size: 0.8, speed: 22, maxLifeMs: 6000, maxDist: 80, cooldownMs: 666, icon: '🔥', label: 'Fireball', key: 'LClick' },
frostbolt: { color: 0x44aaff, size: 0.6, speed: 14, maxLifeMs: 6000, maxDist: 60, cooldownMs: 1000, icon: '❄', label: 'Frostbolt', key: 'RClick' },
telekinesis: { color: 0xaa44ff, size: 1.0, speed: 18, maxLifeMs: 6000, maxDist: 90, cooldownMs: 1500, icon: '🌀', label: 'Telekinesis', key: 'E' },
};
// Shared geo/mat cache - no per-projectile GPU alloc
const _geoCache = {}, _matCache = {};
let _charGltf = null; // Character GLTF — loaded once, cloned per remote player
function getSpellGeo(s) { return _geoCache[s] ??= new THREE.SphereGeometry(s, 8, 8); }
function getSpellMat(c) { return _matCache[c] ??= new THREE.MeshBasicMaterial({ color: c }); }
// === DUNGEON LAYOUT ===
// DungeonLayout data model - decoupled from rendering
// Server can send a different layout for player-built dungeons (Phase 2)
const DEFAULT_DUNGEON = {
rooms: [
{ x: 0, z: 0, w: 80, d: 60 },
{ x: 80, z: 0, w: 50, d: 40 },
{ x: -80, z: 0, w: 50, d: 40 },
{ x: 0, z: -80, w: 60, d: 50 },
{ x: 0, z: -200, w: 110, d: 90 }, // boss chamber
],
corridors: [
{ x: 47.5, z: 0, w: 15, d: 15 }, // room 1 (maxX=40) → room 2 (minX=55)
{ x: -47.5, z: 0, w: 15, d: 15 }, // room 1 (minX=-40) → room 3 (maxX=-55)
{ x: 0, z: -42.5, w: 15, d: 25 }, // room 1 (minZ=-30) → room 4 (maxZ=-55)
{ x: 80, z: -50, w: 12, d: 60 }, // trap corridor — room 2 (minZ=-20) south to z=-80
{ x: 52, z: -80, w: 44, d: 12 }, // connector: trap (minX=74) → room 4 (maxX=30)
{ x: 0, z:-130, w: 15, d: 50 }, // room 4 (minZ=-105) → boss chamber (maxZ=-155)
],
pillars: [
[15,15],[-15,15],[15,-15],[-15,-15],
[20,-180],[-20,-180],[35,-180],[-35,-180],
[20,-220],[-20,-220],[35,-220],[-35,-220],
],
spawnPoints: [
{ x: 10, y: PLAYER_HEIGHT, z: 10 },
{ x: -10, y: PLAYER_HEIGHT, z: 10 },
{ x: 10, y: PLAYER_HEIGHT, z: -10 },
{ x: -10, y: PLAYER_HEIGHT, z: -10 },
{ x: 70, y: PLAYER_HEIGHT, z: 5 },
{ x: -70, y: PLAYER_HEIGHT, z: 5 },
{ x: 5, y: PLAYER_HEIGHT, z: -70 },
{ x: -5, y: PLAYER_HEIGHT, z: -70 },
],
};
// === THREE.JS CORE ===
const canvas = document.getElementById('game-canvas');
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
renderer.setSize(window.innerWidth, window.innerHeight);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08030d);
scene.fog = new THREE.Fog(0x08030d, 15, 70);
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 500);
camera.position.set(0, PLAYER_HEIGHT, 0); // prevent camera-inside-floor black view before player spawns
const clock = new THREE.Clock();
// Lighting
// Low ambient for dark dungeon depth — torches are the primary light source
const ambient = new THREE.AmbientLight(0x110a1a, 0.25);
scene.add(ambient);
// Torch cluster: warm orange/yellow point lights simulate wall sconces
const _torchPositions = [
[0, 7, 0], // center
[30, 7, 20], // east wing
[-30, 7, 20], // west wing
[0, 7, -35], // south corridor
[70, 7, 0], // east room
[-70, 7, 0], // west room
[0, 7, -80], // south room
];
const _torchLights = _torchPositions.map(([x,y,z]) => {
const t = new THREE.PointLight(0xffaa33, 1.5, 22);
t.position.set(x, y, z); scene.add(t); return t;
});
// Keep torch1/torch2 references for the game loop flicker animation
const torch1 = _torchLights[0];
const torch2 = _torchLights[1];
// Cool rim from above — gives ceiling a slightly different cast
const rimLight = new THREE.DirectionalLight(0x6655aa, 0.2);
rimLight.position.set(0, 30, 0); scene.add(rimLight);
// === PROCEDURAL TEXTURES ===
// Canvas-generated textures — no CORS, no external deps, reliable across all browsers
function makeCanvasTex(w, h, drawFn) {
const canvas = document.createElement('canvas');
canvas.width = w; canvas.height = h;
drawFn(canvas.getContext('2d'));
const tex = new THREE.CanvasTexture(canvas);
tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
return tex;
}
// Seeded pseudo-random for deterministic brick variation
function seededRand(seed) {
let s = seed;
return () => { s = (s * 1664525 + 1013904223) & 0xffffffff; return (s >>> 0) / 0xffffffff; };
}
// Stone brick wall — staggered rows, mortar joints, per-brick hue/lightness variation
const _wallTex = makeCanvasTex(512, 512, ctx => {
const rand = seededRand(42);
const COLS = 8, ROWS = 4;
const bw = 512 / COLS, bh = 512 / ROWS;
const mortar = 6; // pixels
// Mortar background
ctx.fillStyle = 'hsl(0,0%,13%)';
ctx.fillRect(0, 0, 512, 512);
for (let row = 0; row < ROWS; row++) {
const offset = (row % 2) * (bw / 2);
for (let col = -1; col <= COLS; col++) {
const x = col * bw + offset + mortar / 2;
const y = row * bh + mortar / 2;
const w = bw - mortar;
const h = bh - mortar;
const hue = 20 + rand() * 12;
const sat = 18 + rand() * 22;
const light = 24 + rand() * 18;
ctx.fillStyle = `hsl(${hue},${sat}%,${light}%)`;
ctx.fillRect(x, y, w, h);
// Subtle face variation — darker top edge (shadow), lighter bottom
const grad = ctx.createLinearGradient(x, y, x, y + h);
grad.addColorStop(0, `hsla(0,0%,0%,0.18)`);
grad.addColorStop(0.5, `hsla(0,0%,0%,0.0)`);
grad.addColorStop(1, `hsla(0,0%,100%,0.06)`);
ctx.fillStyle = grad;
ctx.fillRect(x, y, w, h);
// Per-pixel noise simulation via tiny rects
for (let n = 0; n < 8; n++) {
const nx = x + rand() * w;
const ny = y + rand() * h;
const alpha = rand() * 0.08;
ctx.fillStyle = `rgba(0,0,0,${alpha})`;
ctx.fillRect(nx, ny, 2, 2);
}
}
}
});
_wallTex.repeat.set(4, 2);
// Stone floor — large square tiles with grout lines and slight color variation
const _floorTex = makeCanvasTex(512, 512, ctx => {
const rand = seededRand(99);
const TILES = 4;
const tw = 512 / TILES;
const grout = 5;
ctx.fillStyle = 'hsl(0,0%,10%)';
ctx.fillRect(0, 0, 512, 512);
for (let row = 0; row < TILES; row++) {
for (let col = 0; col < TILES; col++) {
const x = col * tw + grout / 2;
const y = row * tw + grout / 2;
const s = tw - grout;
const hue = 210 + rand() * 20;
const sat = 5 + rand() * 8;
const light = 18 + rand() * 10;
ctx.fillStyle = `hsl(${hue},${sat}%,${light}%)`;
ctx.fillRect(x, y, s, s);
// Subtle beveled highlight top-left
const hi = ctx.createLinearGradient(x, y, x + s, y + s);
hi.addColorStop(0, `hsla(0,0%,100%,0.07)`);
hi.addColorStop(0.5, `hsla(0,0%,0%,0.0)`);
hi.addColorStop(1, `hsla(0,0%,0%,0.1)`);
ctx.fillStyle = hi;
ctx.fillRect(x, y, s, s);
}
}
});
_floorTex.repeat.set(8, 8);
// Dark ceiling — similar to wall but much darker, minimal detail reads at distance
const _ceilTex = makeCanvasTex(256, 256, ctx => {
const rand = seededRand(7);
ctx.fillStyle = 'hsl(270,8%,7%)';
ctx.fillRect(0, 0, 256, 256);
const COLS = 4, ROWS = 4;
const bw = 256 / COLS, bh = 256 / ROWS;
const mortar = 4;
for (let row = 0; row < ROWS; row++) {
const offset = (row % 2) * (bw / 2);
for (let col = -1; col <= COLS; col++) {
const x = col * bw + offset + mortar / 2;
const y = row * bh + mortar / 2;
const hue = 260 + rand() * 20;
const light = 7 + rand() * 6;
ctx.fillStyle = `hsl(${hue},10%,${light}%)`;
ctx.fillRect(x, y, bw - mortar, bh - mortar);
}
}
});
_ceilTex.repeat.set(4, 4);
// Shared material caches — one material per unique UV-repeat key
const _wallMatCache = new Map();
const _floorMatCache = new Map();
const _ceilMatCache = new Map();
function makeWallMat(repeatX = 1, repeatY = 1) {
const key = `${repeatX},${repeatY}`;
if (_wallMatCache.has(key)) return _wallMatCache.get(key);
const tex = _wallTex.clone();
tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
tex.repeat.set(repeatX, repeatY);
const mat = new THREE.MeshStandardMaterial({ map: tex, roughness: 0.9, metalness: 0.03 });
_wallMatCache.set(key, mat);
return mat;
}
function makeFloorMat(repeatX = 1, repeatY = 1) {
const key = `${repeatX},${repeatY}`;
if (_floorMatCache.has(key)) return _floorMatCache.get(key);
const tex = _floorTex.clone();
tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
tex.repeat.set(repeatX, repeatY);
const mat = new THREE.MeshStandardMaterial({ map: tex, roughness: 0.85, metalness: 0.04 });
_floorMatCache.set(key, mat);
return mat;
}
function makeCeilMat(repeatX = 1, repeatY = 1) {
const key = `${repeatX},${repeatY}`;
if (_ceilMatCache.has(key)) return _ceilMatCache.get(key);
const tex = _ceilTex.clone();
tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
tex.repeat.set(repeatX, repeatY);
const mat = new THREE.MeshStandardMaterial({ map: tex, roughness: 1.0, metalness: 0.0 });
_ceilMatCache.set(key, mat);
return mat;
}
// === DUNGEON RENDERER ===
// Wall meshes collected here so the spell raycaster can test against them
const wallMeshes = [];
// Dungeon renderer - builds geometry from layout data
function renderDungeon(layout) {
const pMat = new THREE.MeshStandardMaterial({ color: 0x3a2e50, roughness: 0.9, metalness: 0.05 });
const pGeo = new THREE.CylinderGeometry(1.5, 2, 20, 8);
const WH = 20;
const DOOR_H = 10; // doorway opening height — leaves a solid lintel above
const EPS = 0.5; // snap tolerance for shared-face detection
const addWall = (px, py, pz, rx, ry, rz, gw, gh) => {
if (gw < EPS || gh < EPS) return; // skip degenerate panels
const mat = makeWallMat(gw / 12, gh / 12);
const m = new THREE.Mesh(new THREE.PlaneGeometry(gw, gh), mat);
m.position.set(px, py, pz); m.rotation.set(rx, ry, rz); scene.add(m);
wallMeshes.push(m); // collected for spell raycasting
};
// Pre-compute AABB for every room and corridor
const allBoxes = [...layout.rooms, ...layout.corridors].map(b => ({
minX: b.x - b.w / 2, maxX: b.x + b.w / 2,
minZ: b.z - b.d / 2, maxZ: b.z + b.d / 2,
}));
// For each face, check if another box's OPPOSITE face touches it with overlapping span.
// North face of A (at z=z0): skip if any B has maxZ≈z0 and overlapping X span.
const hasN = (z, x0, x1) => allBoxes.some(b => Math.abs(b.maxZ - z) < EPS && b.minX < x1 - EPS && b.maxX > x0 + EPS);
const hasS = (z, x0, x1) => allBoxes.some(b => Math.abs(b.minZ - z) < EPS && b.minX < x1 - EPS && b.maxX > x0 + EPS);
const hasW = (x, z0, z1) => allBoxes.some(b => Math.abs(b.maxX - x) < EPS && b.minZ < z1 - EPS && b.maxZ > z0 + EPS);
const hasE = (x, z0, z1) => allBoxes.some(b => Math.abs(b.minX - x) < EPS && b.minZ < z1 - EPS && b.maxZ > z0 + EPS);
// Find the overlap span in X between this box face (at z) and its neighbour.
// Returns [lo, hi] clamped to [x0, x1].
const overlapN = (z, x0, x1) => {
const nb = allBoxes.find(b => Math.abs(b.maxZ - z) < EPS && b.minX < x1 - EPS && b.maxX > x0 + EPS);
return nb ? [Math.max(x0, nb.minX), Math.min(x1, nb.maxX)] : [x0, x1];
};
const overlapS = (z, x0, x1) => {
const nb = allBoxes.find(b => Math.abs(b.minZ - z) < EPS && b.minX < x1 - EPS && b.maxX > x0 + EPS);
return nb ? [Math.max(x0, nb.minX), Math.min(x1, nb.maxX)] : [x0, x1];
};
const overlapW = (x, z0, z1) => {
const nb = allBoxes.find(b => Math.abs(b.maxX - x) < EPS && b.minZ < z1 - EPS && b.maxZ > z0 + EPS);
return nb ? [Math.max(z0, nb.minZ), Math.min(z1, nb.maxZ)] : [z0, z1];
};
const overlapE = (x, z0, z1) => {
const nb = allBoxes.find(b => Math.abs(b.minX - x) < EPS && b.minZ < z1 - EPS && b.maxZ > z0 + EPS);
return nb ? [Math.max(z0, nb.minZ), Math.min(z1, nb.maxZ)] : [z0, z1];
};
// Draw doorway framing for a North/South wall face (opening lies along X axis).
// Emits: left jamb, right jamb (full height), lintel (above door), and the door opening stays open.
// rx/ry/rz are the wall's rotation arguments passed straight through to addWall.
const frameNS = (faceX, faceZ, wallX0, wallX1, rx, ry, rz) => {
const [openX0, openX1] = faceX; // [overlapLo, overlapHi] in world X
// Left jamb: from wall left edge to opening left edge
const lw = openX0 - wallX0;
if (lw > EPS) addWall(wallX0 + lw / 2, WH / 2, faceZ, rx, ry, rz, lw, WH);
// Right jamb: from opening right edge to wall right edge
const rw = wallX1 - openX1;
if (rw > EPS) addWall(openX1 + rw / 2, WH / 2, faceZ, rx, ry, rz, rw, WH);
// Lintel: spans the opening width, sits between DOOR_H and WH
const lintH = WH - DOOR_H;
const openW = openX1 - openX0;
if (openW > EPS && lintH > EPS)
addWall((openX0 + openX1) / 2, DOOR_H + lintH / 2, faceZ, rx, ry, rz, openW, lintH);
};
// Draw doorway framing for an East/West wall face (opening lies along Z axis).
const frameEW = (openZ, faceX, wallZ0, wallZ1, rx, ry, rz) => {
const [openZ0, openZ1] = openZ; // [overlapLo, overlapHi] in world Z
// Left jamb (lower Z)
const lw = openZ0 - wallZ0;
if (lw > EPS) addWall(faceX, WH / 2, wallZ0 + lw / 2, rx, ry, rz, lw, WH);
// Right jamb (higher Z)
const rw = wallZ1 - openZ1;
if (rw > EPS) addWall(faceX, WH / 2, openZ1 + rw / 2, rx, ry, rz, rw, WH);
// Lintel
const lintH = WH - DOOR_H;
const openW = openZ1 - openZ0;
if (openW > EPS && lintH > EPS)
addWall(faceX, DOOR_H + lintH / 2, (openZ0 + openZ1) / 2, rx, ry, rz, openW, lintH);
};
// Unified loop over rooms + corridors — solid walls where no neighbour; framed doorway where shared
[...layout.rooms, ...layout.corridors].forEach(b => {
const x0 = b.x - b.w / 2, x1 = b.x + b.w / 2;
const z0 = b.z - b.d / 2, z1 = b.z + b.d / 2;
// Floor
const fl = new THREE.Mesh(new THREE.PlaneGeometry(b.w, b.d), makeFloorMat(b.w / 8, b.d / 8));
fl.rotation.x = -Math.PI/2; fl.position.set(b.x, 0, b.z); scene.add(fl);
// Ceiling
const ce = new THREE.Mesh(new THREE.PlaneGeometry(b.w, b.d), makeCeilMat(b.w / 10, b.d / 10));
ce.rotation.x = Math.PI/2; ce.position.set(b.x, WH, b.z); scene.add(ce);
// Walls — solid where no neighbour, framed doorway where a face is shared with an adjacent box
if (!hasN(z0, x0, x1)) addWall(b.x, WH/2, z0, 0, 0, 0, b.w, WH);
else frameNS(overlapN(z0, x0, x1), z0, x0, x1, 0, 0, 0);
if (!hasS(z1, x0, x1)) addWall(b.x, WH/2, z1, 0, Math.PI, 0, b.w, WH);
else frameNS(overlapS(z1, x0, x1), z1, x0, x1, 0, Math.PI, 0);
if (!hasW(x0, z0, z1)) addWall(x0, WH/2, b.z, 0, Math.PI/2, 0, b.d, WH);
else frameEW(overlapW(x0, z0, z1), x0, z0, z1, 0, Math.PI/2, 0);
if (!hasE(x1, z0, z1)) addWall(x1, WH/2, b.z, 0, -Math.PI/2, 0, b.d, WH);
else frameEW(overlapE(x1, z0, z1), x1, z0, z1, 0, -Math.PI/2, 0);
});
layout.pillars.forEach(([px, pz]) => {
const m = new THREE.Mesh(pGeo, pMat); m.position.set(px, 10, pz); scene.add(m);
});
}
renderDungeon(DEFAULT_DUNGEON);
// Corridor lights — one point light per corridor center; boss approach uses warm red
DEFAULT_DUNGEON.corridors.forEach((c, i) => {
const color = (i === 5) ? 0xff5500 : 0xffaa44; // index 5 = boss approach connector
const light = new THREE.PointLight(color, 1.0, 20);
light.position.set(c.x, 6, c.z);
scene.add(light);
});
// === PROPS ===
// All props use Three.js r128 primitives; no new CDN imports.
// Materials use same style conventions as dungeon (MeshStandardMaterial, seededRand, makeCanvasTex).
// --- Torch sconce factory ---
// Group: iron bracket (Box) + wood stick (Cylinder) + flame cone + child PointLight
function makeTorch(wx, wy, wz) {
const g = new THREE.Group();
const ironMat = new THREE.MeshStandardMaterial({ color: 0x1e1e1e, roughness: 0.6, metalness: 0.8 });
const woodMat = new THREE.MeshStandardMaterial({ color: 0x4a2808, roughness: 0.95 });
const flameMat = new THREE.MeshStandardMaterial({ color: 0xff5500, emissive: 0xff3300, emissiveIntensity: 3.0 });
g.add(new THREE.Mesh(new THREE.BoxGeometry(0.5, 0.3, 0.8), ironMat));
const stick = new THREE.Mesh(new THREE.CylinderGeometry(0.12, 0.14, 1.8, 6), woodMat);
stick.position.y = 0.9; g.add(stick);
const flame = new THREE.Mesh(new THREE.ConeGeometry(0.25, 0.7, 6), flameMat);
flame.position.y = 2.05; g.add(flame);
const pl = new THREE.PointLight(0xff9944, 0.9, 14);
pl.position.y = 2.2; g.add(pl);
g.position.set(wx, wy, wz);
scene.add(g);
return g;
}
// Main room (80×60, center 0,0)
makeTorch(-30, 10, -29.5); makeTorch( 30, 10, -29.5); // south wall
makeTorch(-30, 10, 29.5); makeTorch( 30, 10, 29.5); // north wall
// East room (50×40, center 80,0): east wall x=105
makeTorch(104.5, 10, 12); makeTorch(104.5, 10, -12);
// West room (50×40, center -80,0): west wall x=-105
makeTorch(-104.5, 10, 12); makeTorch(-104.5, 10, -12);
// South room (60×50, center 0,-80)
makeTorch(-22, 10, -55.5); makeTorch( 22, 10, -55.5); // north wall
makeTorch(-22, 10, -104.5); makeTorch( 22, 10, -104.5); // south wall
// Boss chamber (110×90, center 0,-200)
makeTorch(-40, 10, -155.5); makeTorch( 40, 10, -155.5); // north wall
makeTorch(-40, 10, -244.5); makeTorch( 40, 10, -244.5); // south wall
makeTorch(-54.5, 10, -185); makeTorch( 54.5, 10, -185); // side walls
// Boss chamber dedicated point lights (brazier glow — no sconce mesh needed)
[[-32, 3, -200], [32, 3, -200]].forEach(([lx, ly, lz]) => {
const bl = new THREE.PointLight(0xff3300, 1.2, 35);
bl.position.set(lx, ly, lz); scene.add(bl);
});
// --- Treasure chests (south room, two on a shared raised dais) ---
const chests = [];
function makeChest(cx, cz) {
const g = new THREE.Group();
const woodMat = new THREE.MeshStandardMaterial({ color: 0x3d1f0a, roughness: 0.9 });
const bandMat = new THREE.MeshStandardMaterial({ color: 0x7a6020, roughness: 0.5, metalness: 0.6 });
const daisMat = new THREE.MeshStandardMaterial({ color: 0x1e1c2e, roughness: 0.9 });
const dais = new THREE.Mesh(new THREE.BoxGeometry(4.2, 0.4, 3.0), daisMat);
dais.position.y = 0.2; g.add(dais);
const base = new THREE.Mesh(new THREE.BoxGeometry(3, 1.5, 2), woodMat);
base.position.y = 1.15; g.add(base);
const band = new THREE.Mesh(new THREE.BoxGeometry(3.1, 0.22, 2.08), bandMat);
band.position.y = 1.45; g.add(band);
// Lid group — pivot at back edge so it swings open correctly
const lidGroup = new THREE.Group();
lidGroup.position.set(0, 1.9, -1.0);
const lid = new THREE.Mesh(new THREE.BoxGeometry(3, 0.8, 2), woodMat);
lid.position.set(0, 0.4, 1.0); lidGroup.add(lid);
const lock = new THREE.Mesh(new THREE.SphereGeometry(0.18, 8, 8), bandMat);
lock.position.set(0, 0.25, -1.02); lidGroup.add(lock);
g.add(lidGroup);
g.position.set(cx, 0, cz);
scene.add(g);
chests.push({ group: g, lidGroup, open: false, openT: 0 });
}
makeChest(-8, -80);
makeChest( 8, -80);
// --- Bone piles (deterministic per-position seeding) ---
function makeBonePile(bx, bz) {
const g = new THREE.Group();
const boneMat = new THREE.MeshStandardMaterial({ color: 0xd4c9a8, roughness: 0.8 });
const rand = seededRand(((bx * 73 + bz * 31) & 0xffffffff) >>> 0);
for (let i = 0; i < 3; i++) {
const bone = new THREE.Mesh(new THREE.CylinderGeometry(0.12, 0.12, 1.2 + rand() * 0.6, 5), boneMat);
bone.position.set(rand() * 1.4 - 0.7, 0.2, rand() * 1.4 - 0.7);
bone.rotation.set(rand() * 0.6 - 0.3, rand() * Math.PI * 2, rand() * 0.8 - 0.4);
g.add(bone);
}
const skull = new THREE.Mesh(new THREE.SphereGeometry(0.35, 7, 7), boneMat);
skull.position.set(rand() * 0.6 - 0.3, 0.35, rand() * 0.6 - 0.3);
skull.scale.set(1.0, 0.8, 1.1); g.add(skull);
for (let i = 0; i < 3; i++) {
const joint = new THREE.Mesh(new THREE.SphereGeometry(0.11, 5, 5), boneMat);
joint.position.set(rand() * 1.5 - 0.75, 0.12, rand() * 1.5 - 0.75);
g.add(joint);
}
g.position.set(bx, 0, bz);
scene.add(g);
}
makeBonePile( 100, 17); // east room SE
makeBonePile(-100, 17); // west room SW
makeBonePile( -26, -98); // south room SW
makeBonePile( 26, -98); // south room SE
makeBonePile( -48, -237); // boss chamber SW
makeBonePile( 48, -237); // boss chamber SE
makeBonePile( -48, -163); // boss chamber NW
makeBonePile( 48, -163); // boss chamber NE
// --- Magic rune floor overlays (rotate slowly in game loop) ---
const rotatingRunes = [];
function placeRune(rx, rz, size, emissiveHex, canvasColor) {
const tex = makeCanvasTex(256, 256, ctx => {
ctx.clearRect(0, 0, 256, 256);
ctx.strokeStyle = canvasColor; ctx.lineWidth = 3;
ctx.shadowColor = canvasColor; ctx.shadowBlur = 14;
// Concentric circles
for (let r = 25; r <= 115; r += 22) {
ctx.beginPath(); ctx.arc(128, 128, r, 0, Math.PI * 2); ctx.stroke();
}
// Star lines (8-pointed)
for (let i = 0; i < 8; i++) {
const a = i * Math.PI / 4;
ctx.beginPath();
ctx.moveTo(128 + Math.cos(a) * 28, 128 + Math.sin(a) * 28);
ctx.lineTo(128 + Math.cos(a) * 115, 128 + Math.sin(a) * 115);
ctx.stroke();
}
// Inner rune marks
ctx.lineWidth = 1.5;
for (let i = 0; i < 6; i++) {
const a = i * Math.PI / 3;
ctx.beginPath();
ctx.moveTo(128 + Math.cos(a) * 50, 128 + Math.sin(a) * 50);
ctx.lineTo(128 + Math.cos(a + 0.4) * 80, 128 + Math.sin(a + 0.4) * 80);
ctx.stroke();
}
});
tex.wrapS = tex.wrapT = THREE.ClampToEdgeWrapping;
const mat = new THREE.MeshStandardMaterial({
map: tex, emissiveMap: tex, transparent: true, side: THREE.DoubleSide,
emissive: new THREE.Color(emissiveHex), emissiveIntensity: 1.2, roughness: 1.0,
});
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(size, size), mat);
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(rx, 0.02, rz);
scene.add(mesh);
rotatingRunes.push(mesh);
}
placeRune(0, -80, 12, 0x7722bb, '#aa44ff'); // south room — purple
// Ritual circle (boss chamber — large pentagram, emissive red)
(function() {
const tex = makeCanvasTex(512, 512, ctx => {
ctx.clearRect(0, 0, 512, 512);
ctx.strokeStyle = '#cc2200'; ctx.shadowColor = '#ff0000'; ctx.shadowBlur = 22;
ctx.lineWidth = 5;
ctx.beginPath(); ctx.arc(256, 256, 245, 0, Math.PI * 2); ctx.stroke();
ctx.beginPath(); ctx.arc(256, 256, 145, 0, Math.PI * 2); ctx.stroke();
// Pentagram — connect every 2nd of 5 points on outer circle
ctx.lineWidth = 4;
const pts = Array.from({ length: 5 }, (_, i) => {
const a = i * 4 * Math.PI / 5 - Math.PI / 2;
return [256 + Math.cos(a) * 245, 256 + Math.sin(a) * 245];
});
ctx.beginPath(); ctx.moveTo(pts[0][0], pts[0][1]);
for (let i = 1; i < 5; i++) ctx.lineTo(pts[i][0], pts[i][1]);
ctx.closePath(); ctx.stroke();
ctx.lineWidth = 2;
for (let i = 0; i < 10; i++) {
const a = i * Math.PI / 5;
ctx.beginPath();
ctx.moveTo(256 + Math.cos(a) * 145, 256 + Math.sin(a) * 145);
ctx.lineTo(256 + Math.cos(a) * 245, 256 + Math.sin(a) * 245);
ctx.stroke();
}
});
tex.wrapS = tex.wrapT = THREE.ClampToEdgeWrapping;
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
map: tex, emissiveMap: tex, transparent: true, side: THREE.DoubleSide,
emissive: new THREE.Color(0xaa0000), emissiveIntensity: 1.8, roughness: 1.0,
}));
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(0, 0.03, -200);
scene.add(mesh);
rotatingRunes.push(mesh);
})();
// --- Altar (south room, against north wall z≈-55) ---
(function() {
const stoneMat = new THREE.MeshStandardMaterial({ color: 0x242035, roughness: 0.9 });
const crystalMat = new THREE.MeshStandardMaterial({ color: 0x8844cc, emissive: 0x441188, emissiveIntensity: 2.0, roughness: 0.3, metalness: 0.4 });
const goldMat = new THREE.MeshStandardMaterial({ color: 0x5a4010, roughness: 0.5, metalness: 0.6 });
const g = new THREE.Group();
const base = new THREE.Mesh(new THREE.BoxGeometry(8, 2, 4), stoneMat);
base.position.y = 1.0; g.add(base);
const slab = new THREE.Mesh(new THREE.BoxGeometry(7, 0.5, 3.5), stoneMat);
slab.position.y = 2.25; g.add(slab);
// Torus ring lying flat atop the slab
const ring = new THREE.Mesh(new THREE.TorusGeometry(2.0, 0.22, 8, 24), goldMat);
ring.rotation.x = Math.PI / 2; ring.position.y = 2.6; g.add(ring);
// OctahedronGeometry = two pyramids base-to-base — serves as arcane crystal
const crystal = new THREE.Mesh(new THREE.OctahedronGeometry(0.85, 0), crystalMat);
crystal.position.y = 3.85; g.add(crystal);
g.position.set(0, 0, -57);
scene.add(g);
})();
// --- Spike traps (trap corridor: center 80,-45, width 12, depth 30) ---
// 3 pressure plates with 3×3 spike grid each; spikes start below floor
const spikeTraps = [];
function makeSpikeTrap(tx, tz) {
const plateMat = new THREE.MeshStandardMaterial({ color: 0x222222, roughness: 0.7, metalness: 0.5 });
const spikeMat = new THREE.MeshStandardMaterial({ color: 0x999999, roughness: 0.3, metalness: 0.8 });
const g = new THREE.Group();
const plate = new THREE.Mesh(new THREE.PlaneGeometry(4.5, 4.5), plateMat);
plate.rotation.x = -Math.PI / 2; plate.position.y = 0.01; g.add(plate);
const spikeMeshes = [];
for (let sx = -1; sx <= 1; sx++) {
for (let sz = -1; sz <= 1; sz++) {
const spike = new THREE.Mesh(new THREE.ConeGeometry(0.18, 2.0, 5), spikeMat);
spike.position.set(sx * 1.4, -2.0, sz * 1.4); // retracted below floor
g.add(spike); spikeMeshes.push(spike);
}
}
g.position.set(tx, 0, tz);
scene.add(g);
spikeTraps.push({ group: g, spikeMeshes, state: 'idle', activeT: 0 });
}
makeSpikeTrap(80, -34);
makeSpikeTrap(80, -45);
makeSpikeTrap(80, -56);
// === PORTAL SYSTEM ===
// VIBEVERSE PORTAL SPEC (by @levelsio)
// Make an exit portal players can walk into (label: "Vibe Jam Portal").
// On entry, redirect to: https://jam.pieter.com/portal/2026
// Pass GET query params:
// ?username= (player name)
// ?color= (player color in hex)
// ?speed= (meters per second)
// ?ref= (URL of this game, so they can portal back)
// Optional: avatar_url, team, hp, speed_x/y/z, rotation_x/y/z
//
// The portal redirector adds ?portal=true to all URLs it forwards to.
//
// START PORTAL: when this game receives ?portal=true AND ?ref=,
// spawn a portal at the player's starting position that goes BACK
// to the ref URL. Pass all original params back when returning.
//
// IMPORTANT: No loading screens, no input screens — instant load
// when arriving from a portal (?portal=true).
//
// Sample code: https://gist.github.com/levelsio/ffdbfe356b421b97a31664ded4bc961d
// Portal helpers
function makeLabelCanvas(text) {
const c = document.createElement('canvas'); c.width=256; c.height=64;
const ctx = c.getContext('2d');
ctx.font='bold 22px monospace'; ctx.fillStyle='#fff'; ctx.textAlign='center';
ctx.fillText(text, 128, 40); return c;
}
function createPortal(color, position, label) {
const g = new THREE.Group();
g.position.copy(position);
// Outer ring — MeshStandardMaterial with emissive gives glow without post-processing
const ring = new THREE.Mesh(
new THREE.TorusGeometry(PORTAL_RING_RADIUS, PORTAL_RING_TUBE, 16, 64),
new THREE.MeshStandardMaterial({ color, emissive: color, emissiveIntensity: PORTAL_EMISSIVE_INTENSITY })
);
g.add(ring);
g._ring = ring;
// Inner void — CircleGeometry radius snug inside torus inner edge, no corner clipping ever
const _voidR = PORTAL_RING_RADIUS - PORTAL_RING_TUBE - 0.15;
const voidMesh = new THREE.Mesh(
new THREE.CircleGeometry(_voidR, 64),
new THREE.MeshStandardMaterial({
color: 0x001a00, emissive: color, emissiveIntensity: PORTAL_VOID_EMISSIVE,
transparent: true, opacity: 0.93, side: THREE.DoubleSide, depthWrite: false,
})
);
g.add(voidMesh);
g._void = voidMesh;
// Glow halo — wider tube torus, additive blending = soft bloom without post-processing
const glowRing = new THREE.Mesh(
new THREE.TorusGeometry(PORTAL_RING_RADIUS, PORTAL_RING_TUBE * 3.0, 8, 64),
new THREE.MeshBasicMaterial({
color, transparent: true, opacity: 0.13,
blending: THREE.AdditiveBlending, depthWrite: false, side: THREE.DoubleSide,
})
);
g.add(glowRing);
g._glowRing = glowRing;
// Particle ring — 72 bright points orbiting the torus equator
const _PC = 72;
const _pPos = new Float32Array(_PC * 3);
const _pPhase = new Float32Array(_PC);
for (let i = 0; i < _PC; i++) {
_pPhase[i] = (i / _PC) * Math.PI * 2;
_pPos[i*3] = Math.cos(_pPhase[i]) * PORTAL_RING_RADIUS;
_pPos[i*3+1] = Math.sin(_pPhase[i]) * PORTAL_RING_RADIUS;
_pPos[i*3+2] = 0;
}
const _pGeo = new THREE.BufferGeometry();
_pGeo.setAttribute('position', new THREE.BufferAttribute(_pPos, 3));
const particles = new THREE.Points(_pGeo, new THREE.PointsMaterial({
color, size: 0.20, transparent: true, opacity: 0.95,
blending: THREE.AdditiveBlending, depthWrite: false,
}));
particles.userData.phases = _pPhase;
g.add(particles);
g._particles = particles;
if (label) {
const lm = new THREE.Mesh(new THREE.PlaneGeometry(8, 1.8),
new THREE.MeshBasicMaterial({ map: new THREE.CanvasTexture(makeLabelCanvas(label)), transparent: true, side: THREE.DoubleSide }));
lm.position.set(0, PORTAL_RING_RADIUS + 1.5, 0); g.add(lm);
}
const portalLight = new THREE.PointLight(color, PORTAL_LIGHT_INTENSITY, PORTAL_LIGHT_DIST);
g.add(portalLight);
g._light = portalLight;
scene.add(g); return g;
}
// Exit portal inside west wing — faces east so player approaching from spawn (0,0,0) sees the ring face-on
const exitPortalGroup = createPortal(0x00ff00, new THREE.Vector3(-30, PLAYER_HEIGHT, 0), 'VIBE JAM 2026');
exitPortalGroup.rotation.y = Math.PI / 2; // faces east toward spawn at (0,0,0)
let startPortalGroup = null, startPortalReady = false;
if (incomingPortal && safeRef) {
startPortalGroup = createPortal(0xff0000, new THREE.Vector3(0, PLAYER_HEIGHT, 25), 'RETURN PORTAL');
setTimeout(() => { startPortalReady = true; }, START_PORTAL_DELAY);
}
// Shared toon cel-shading gradient (3 bands: shadow / mid / highlight)
const _toonGradientMap = (() => {
const data = new Uint8Array([90, 160, 255]);
const tex = new THREE.DataTexture(data, 3, 1, THREE.LuminanceFormat);
// NearestFilter keeps toon bands crisp — linear filtering blurs the step edges
tex.minFilter = THREE.NearestFilter;
tex.magFilter = THREE.NearestFilter;
tex.needsUpdate = true;
return tex;
})();
// === VOXEL ASSETS ===
// Shared voxel dragon asset used in-world and in /assets viewer page.
const VOXEL_DRAGON_PALETTE = {
body: 0x0f2134,
shadow: 0x08131f,
cyan: 0x1b9bb3,
horn: 0xbe9d83,
claw: 0xead8bc,
eye: 0x0a0a0a,
};
function buildVoxelDragonVoxels() {
const v = [];
const add = (x, y, z, c) => v.push([x, y, z, c]);
// Torso core
for (let x = -3; x <= 3; x++) {
for (let y = 3; y <= 6; y++) {
for (let z = -2; z <= 2; z++) {
if (Math.abs(x) + Math.abs(z) <= 6) add(x, y, z, 'body');
}
}
}
// Belly cyan striping
for (let x = -2; x <= 2; x++) for (let z = -1; z <= 1; z++) add(x, 3, z, 'cyan');
// Neck + head
for (let i = 0; i < 4; i++) {
for (let y = 5; y <= 6; y++) for (let z = -1; z <= 1; z++) add(4 + i, y, z, 'body');
}
for (let y = 5; y <= 7; y++) for (let z = -2; z <= 2; z++) add(8, y, z, 'body');
for (let z = -1; z <= 1; z++) { add(9, 6, z, 'body'); add(10, 6, z, 'body'); }
add(9, 5, 0, 'cyan'); add(10, 5, 0, 'cyan');
add(10, 7, -1, 'eye'); add(10, 7, 1, 'eye');
// Horns
for (let i = 0; i < 5; i++) {
add(7 + i, 8 + Math.floor(i / 2), -2 - i, 'horn');
add(7 + i, 8 + Math.floor(i / 2), 2 + i, 'horn');
}
// Wings (voxel arcs)
for (let side of [-1, 1]) {
for (let i = 0; i < 8; i++) {
add(-1 - i, 7 + Math.floor(i / 3), side * (4 + i), 'horn');
add(-1 - i, 6, side * (3 + i), 'shadow');
if (i > 1 && i < 7) add(-2 - i, 5, side * (3 + i), 'cyan');
}
}
// Legs
const legs = [[-2, -1], [-2, 1], [2, -1], [2, 1]];
for (const [x0, z0] of legs) {
for (let y = 0; y <= 2; y++) {
add(x0, y, z0, 'shadow');
add(x0 + (x0 > 0 ? 1 : -1), y, z0, 'body');
}
add(x0, -1, z0, 'claw');
add(x0 + (x0 > 0 ? 1 : -1), -1, z0, 'claw');
}
// Tail
for (let i = 0; i < 9; i++) {
const x = -4 - i;
const y = 4 + Math.floor(i / 3);
add(x, y, 0, 'body');
if (i % 2 === 0) add(x, y - 1, 0, 'cyan');
}
add(-13, 7, -1, 'claw'); add(-13, 7, 1, 'claw');
return v;
}
function createVoxelAsset(voxels, { cubeSize = 0.55, gradientMap = null, palette = VOXEL_DRAGON_PALETTE } = {}) {
const grouped = new Map();
for (const [x, y, z, key] of voxels) {
if (!grouped.has(key)) grouped.set(key, []);
grouped.get(key).push([x, y, z]);
}
const geo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
const root = new THREE.Group();
for (const [key, pos] of grouped) {
const mat = new THREE.MeshToonMaterial({ color: palette[key], gradientMap });
const im = new THREE.InstancedMesh(geo, mat, pos.length);
const m = new THREE.Matrix4();
pos.forEach(([x, y, z], i) => { m.setPosition(x * cubeSize, y * cubeSize, z * cubeSize); im.setMatrixAt(i, m); });
im.instanceMatrix.needsUpdate = true;
root.add(im);
}
root.position.y = 0.55;
return root;
}
const DRAGON_VOXELS = buildVoxelDragonVoxels();
// === VOXEL DRAGON ENEMY ===
class VoxelDragon {
constructor(scene, startX, startZ) {
this.mesh = createVoxelAsset(DRAGON_VOXELS, { cubeSize: 0.35, gradientMap: _toonGradientMap });
this.baseY = PLAYER_HEIGHT - 2.6;
this.mesh.position.set(startX, this.baseY, startZ);
scene.add(this.mesh);
this.hp = 3;
this.state = 'patrol'; // 'patrol' | 'alert' | 'dead'
this.waypoints = [{x: startX - 8, z: startZ}, {x: startX + 8, z: startZ}];
this.waypointIndex = 0;
this.speed = 2.5;
this.t = 0;
}
update(dt, playerPos, spells) {
if (this.state === 'dead') return;
this.t += dt;
const dx = playerPos.x - this.mesh.position.x;
const dz = playerPos.z - this.mesh.position.z;
const dist = Math.sqrt(dx * dx + dz * dz);
// Detection
if (dist < 12) this.state = 'alert';
else if (dist > 16 && this.state === 'alert') this.state = 'patrol';
// Idle bob
this.mesh.position.y = this.baseY + Math.sin(this.t * 1.2) * 0.1;
// Patrol movement
if (this.state === 'patrol') {
const target = this.waypoints[this.waypointIndex];
const tdx = target.x - this.mesh.position.x;
const tdz = target.z - this.mesh.position.z;
const d = Math.sqrt(tdx * tdx + tdz * tdz);
if (d < 0.3) {
this.waypointIndex = (this.waypointIndex + 1) % this.waypoints.length;
} else {
this.mesh.position.x += (tdx / d) * this.speed * dt;
this.mesh.position.z += (tdz / d) * this.speed * dt;
this.mesh.rotation.y = Math.atan2(tdx, tdz);
}
}
// Alert: face player
if (this.state === 'alert') {
this.mesh.rotation.y = Math.atan2(dx, dz);
}
// Spell hit detection
for (const sp of spells) {
if (!sp.alive) continue;
const sdx = this.mesh.position.x - sp.mesh.position.x;
const sdy = this.mesh.position.y - sp.mesh.position.y;
const sdz = this.mesh.position.z - sp.mesh.position.z;
if (Math.sqrt(sdx * sdx + sdy * sdy + sdz * sdz) < 1.5) {
spawnImpactFlash(sp.mesh.position.clone(), sp.def.color);
sp.destroy();
this.takeDamage();
break;
}
}
}
takeDamage() {
this.hp--;
// Flash red briefly
this.mesh.traverse(m => { if (m.isMesh && m.material.emissive) m.material.emissive.setHex(0xff0000); });
setTimeout(() => {
this.mesh.traverse(m => { if (m.isMesh && m.material.emissive) m.material.emissive.setHex(0x000000); });
}, 150);
if (this.hp <= 0) this.die();
}
die() {
this.state = 'dead';
this.mesh.traverse(m => { if (m.isMesh) m.visible = false; });
setTimeout(() => { this.mesh.parent?.remove(this.mesh); }, 500);
}
}
// Spawn dragons in non-starting rooms
const monsters = [
new VoxelDragon(scene, 80, 0), // east room
new VoxelDragon(scene, 0, -200), // boss chamber
];
// Per-player color derived from id string
function hashColor(id) {
let h = 0;