-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
726 lines (619 loc) · 25.1 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, user-scalable=no">
<meta name="description" content="Fully three dimensional mazes"/>
<meta name="theme-color" content="#1c0bcc">
<meta property="og:title" content="3D Snake | Chris Raff" />
<meta property="og:description" content="Classic snake game, but in a cube" />
<meta property="og:image" content="https://www.chrisraff.com/3dsnake/screenshot.png" />
<meta property="og:image:type" content="image/png" />
<meta property="og:image:width" content="1058" />
<meta property="og:image:height" content="835" />
<title>3D Snake | Chris Raff</title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
body {
margin: 0;
color: white;
}
canvas { display: block; }
#lower-left-quarter {
position: absolute;
bottom: calc(10px + env(safe-area-inset-bottom));
left: calc(10px + env(safe-area-inset-left));
}
#snake-lives {
display: flex;
font-size: 30pt;
}
#lower-right-quarter {
position: absolute;
bottom: calc(10px + env(safe-area-inset-bottom));
right: calc(10px + env(safe-area-inset-right));
margin: auto;
}
#menu-overlay {
position: absolute;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
pointer-events: none;
overflow-y: auto;
}
#menu-overlay > * {
pointer-events: auto;
}
.menu {
display: none;
flex-direction: column;
justify-content: center;
text-align: center;
max-height: 100vh;
}
.menu > * {
margin: 10px;
}
.menu.menu-active {
display: flex;
}
.menu h2 {
font-weight: bold;
}
.scrollable-y {
height: 100%;
overflow-y: auto;
}
button {
background-color: transparent;
color: white;
border: 2px solid white;
border-radius: 7px;
padding: 8px 15px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
outline: none;
align-self: center;
pointer-events: auto;
}
button:disabled {
background-color: rgba(128, 128, 128, 0.2);
color: rgba(200, 200, 200, 0.75);
border-color: #888888;
cursor: auto;
pointer-events: none;
}
button.button-main {
font-size: 24pt;
padding: 10px 20px;
}
button:hover {
background-color: rgba(255, 255, 255, 0.2);
}
#menu-bindings {
width: 80%;
max-width: 800px;
}
#control-bindings {
display: flex;
flex-direction: column;
width: 100%;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
padding: 1px 10px;
}
.control-row:nth-child(odd) {
background-color: rgba(255, 255, 255, 0.05);
}
.binding-desc {
flex-shrink: 0;
}
.binding-wrapper {
display: flex;
align-items: center;
justify-content: flex-end;
flex-grow: 1;
max-width: 70%;
gap: 10px;
}
.binding-area {
display: flex;
overflow-x: auto;
white-space: nowrap;
padding: 5px;
gap: 10px;
}
.button-add-binding {
flex-shrink: 0;
padding: 0px 4px !important;
align-self: center;
}
/* Scrollbar styling */
::-webkit-scrollbar {
height: 8px;
width: 8px;
}
::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.0);
border-radius: 5px;
}
::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.5);
border-radius: 5px;
}
::-webkit-scrollbar-thumb:hover {
background: rgba(255, 255, 255, 0.7);
}
.dpad {
position: fixed;
display: grid;
grid-template-columns: repeat(3, 1fr); /* Creates a 3-column grid */
grid-template-rows: repeat(3, 1fr); /* Creates a 3-row grid */
left: 5%;
top: 50%;
transform: translateY(-50%);
width: min(60vh, 30vw);
height: min(60vh, 30vw);
justify-content: space-around;
align-content: space-around;
}
.dpad .dpad-up { grid-area: 1 / 2 / 2 / 3; } /* Top center */
.dpad .dpad-right { grid-area: 2 / 3 / 3 / 4; } /* Middle right */
.dpad .dpad-down { grid-area: 3 / 2 / 4 / 3; } /* Bottom center */
.dpad .dpad-left { grid-area: 2 / 1 / 3 / 2; } /* Middle left */
.inoutpad {
position: fixed;
display: grid;
grid-template-columns: 1fr; /* Creates a 1-column grid */
grid-template-rows: repeat(2, 1fr); /* Creates a 2-row grid */
right: 5%;
top: 50%;
transform: translateY(-50%);
width: min(25vh, 15vw);
height: min(50vh, 30vw);
justify-content: space-around;
align-content: space-around;
}
.rotatepad {
position: fixed;
display: grid;
grid-template-columns: repeat(2, 1fr); /* Creates a 2-column grid */
grid-template-rows: 1fr; /* Creates a 1-row grid */
right: 20%;
bottom: 5%;
width: min(40vh, 20vw);
height: min(20vh, 10vw);
justify-content: space-around;
align-content: space-around;
}
.touch-controls .button {
display: flex;
justify-content: center;
border: 2px solid rgba(255, 255, 255, 0.5);
border-radius: 20%;
align-items: center;
font-size: 4vh;
cursor: pointer;
}
body.menu-overlay-active .touch-controls {
visibility: hidden;
}
#menu-gameover.menu.menu-active {
display: flex;
flex-direction: column;
align-items: center;
}
#menu-gameover .leaderboard-container {
flex-grow: 1;
max-height: 50%;
overflow-y: auto;
pointer-events: auto;
}
#copyright {
position: absolute;
bottom: env(safe-area-inset-bottom);
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
}
.small-details {
color: rgba(255,255,255,0.25);
font-family: Arial;
font-size: 10px;
padding: 10px;
text-align: center;
margin: auto;
display: block;
visibility: hidden;
}
.menu-overlay-active .small-details {
visibility: visible;
}
#leaderboard {
border-collapse: collapse;
width: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
#leaderboard th,
#leaderboard td {
border: 1px solid white;
padding: 8px;
text-align: left;
}
#leaderboard th {
background-color: rgba(100, 100, 100, 0.5);
position: sticky;
top: 0;
z-index: 1;
}
#leaderboard tr:nth-child(even) {
background-color: rgba(255, 255, 255, 0.1);
}
@keyframes leaderboard-highlight {
0% {
background: rgba(255, 255, 255, 0.0);
}
80% {
background: rgba(255, 255, 255, 0.5);
}
100% {
background: rgba(255, 255, 255, 0.0);
}
}
.hide {
visibility: hidden;
}
.tutorial-text {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
width: 100%;
font-family: Arial;
font-size: 25pt;
animation-duration: 0.5s;
animation-iteration-count: 1;
pointer-events: none;
}
body.formfactor-touchfirst .tutorial-text {
font-size: 20pt;
}
.menu-overlay-active .tutorial-text {
display: none;
}
.tip-different-direction {
font-size: 50%;
}
.prevent-highlight
{
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
.prevent-tap-highlight
{
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
body.formfactor-touchfirst .formfactor-desktop {
display: none;
}
body:not(.formfactor-touchfirst) .formfactor-touchfirst {
display: none;
}
@keyframes tutorial-text-fade-in {
0% {
color: rgba(255,255,255,0.0);
}
100% {
color: rgba(255,255,255,1.0);
}
}
@keyframes tutorial-text-fade-out {
0% {
color: rgba(255,255,255,1.0);
}
100% {
color: rgba(255,255,255,0.0);
}
}
@keyframes tutorial-highlight {
0% {
background: rgba(255, 255, 255, 0.0);
}
90% {
background: rgba(255, 255, 255, 0.8);
}
100% {
background: rgba(255, 255, 255, 0.0);
}
}
@keyframes tutorial-highlight-secondary {
0% {
background: rgba(255, 255, 255, 0.0);
}
90% {
background: rgba(255, 255, 255, 0.5);
}
100% {
background: rgba(255, 255, 255, 0.0);
}
}
#pauseButton {
top: calc(5px + env(safe-area-inset-top));
left: calc(5px + env(safe-area-inset-left));
position: absolute;
}
.pause-button {
width: 40px;
height: 40px;
background-color: transparent;
border: 1px solid transparent;
position: relative;
cursor: pointer;
outline: none;
display: flex;
justify-content: center;
align-items: center;
}
.menu-overlay-active .pause-button {
display: none;
}
.pause-button::before,
.pause-button::after {
content: "";
display: block;
width: 7px;
height: 24px;
background-color: #fff;
}
.pause-button::before {
transform: translateX(-4px);
}
.pause-button::after {
transform: translateX(4px);
}
</style>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-120805992-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-120805992-1');
</script>
</head>
<body class="prevent-highlight prevent-tap-highlight menu-overlay-active">
<!-- <script type="text/javascript" src="js/detectmobilebrowser.js"></script> -->
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/examples/": "https://unpkg.com/[email protected]/examples/"
}
}
</script>
<script id="vertexShader" type="x-shader/x-vertex">
varying vec3 vWorldPosition;
void main() {
vWorldPosition = (modelMatrix * vec4(position, 1.0)).xyz;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform sampler2D gridTexture;
uniform vec3 playerPosition;
varying vec3 vWorldPosition;
const float maxDist = 4.0;
void main() {
// Compute the highlighting for this fragment based on player distance
float distance = length(playerPosition - vWorldPosition);
float intensity = clamp(maxDist - distance, 0.0, maxDist) / maxDist; // Highlight intensity
// Compute the uv for the grid texture
vec3 normalizedPos = vWorldPosition + 0.5;
vec2 uv;
vec3 absPos = abs(vWorldPosition);
if (absPos.z >= absPos.x && absPos.z >= absPos.y) {
if (vWorldPosition.z > 0.0) {
// Front face
uv = normalizedPos.xy;
} else {
// Back face
uv = vec2(normalizedPos.x, 1.0 - normalizedPos.y);
}
} else if (absPos.x >= absPos.y && absPos.x >= absPos.z) {
if (vWorldPosition.x > 0.0) {
// Right face
uv = vec2(normalizedPos.z, normalizedPos.y);
} else {
// Left face
uv = vec2(1.0 - normalizedPos.z, normalizedPos.y);
}
} else {
if (vWorldPosition.y > 0.0) {
// Top face
uv = vec2(normalizedPos.x, normalizedPos.z);
} else {
// Bottom face
uv = vec2(normalizedPos.x, 1.0 - normalizedPos.z);
}
}
gl_FragColor = texture2D(gridTexture, uv) * intensity;
}
</script>
<script type="module" src="js/game.js"></script>
<div id="lower-left-quarter">
<div>
Length: <span id="snake-realtime-length">4</span>
</div>
<div id="snake-lives" class="prevent-highlight">
<div class="snake-life" snake-life-idx="0">🟩</div>
<div class="snake-life" snake-life-idx="1">🟩</div>
<div class="snake-life" snake-life-idx="2">🟩</div>
</div>
</div>
<button class="pause-button formfactor-touchfirst" id="pauseButton"></button>
<div class="dpad formfactor-touchfirst touch-controls">
<div target-action="actionUp" class="button dpad-up">↑</div>
<div target-action="actionRight" class="button dpad-right">→</div>
<div target-action="actionDown" class="button dpad-down">↓</div>
<div target-action="actionLeft" class="button dpad-left">←</div>
</div>
<div class="inoutpad formfactor-touchfirst touch-controls">
<div target-action="actionIn" class="button dpad-in">↑</div>
<div target-action="actionOut" class="button dpad-out">↓</div>
</div>
<div class="rotatepad formfactor-touchfirst touch-controls">
<div target-action="actionRotateLeft" class="button dpad-rotate-left">↺</div>
<div target-action="actionRotateRight" class="button dpad-rotate-right">↻</div>
</div>
<div id="tutorial-move-screenplane" class="tutorial-text hide">
<span class="formfactor-desktop">Use the WASD keys to move</span>
<span class="formfactor-touchfirst">Use the left D-Pad to move</span>
<p class="tip-different-direction hide">Try picking a different direction.</p>
</div>
<div id="tutorial-move-in" class="tutorial-text hide">
<span class="formfactor-desktop">Use Left Click to move in</span>
<span class="formfactor-touchfirst">Press Up on the right pad to move in</span>
</div>
<div id="tutorial-move-screenplane2" class="tutorial-text hide">
<span class="formfactor-desktop">Use WASD again for the next food</span>
<span class="formfactor-touchfirst">Use the left D-Pad for the next food</span>
<p class="tip-different-direction hide">Try picking a different direction.</p>
</div>
<div id="tutorial-move-out" class="tutorial-text hide">
<span class="formfactor-desktop">Use Right Click to move out</span>
<span class="formfactor-touchfirst">Press Down on the right pad to move out</span>
</div>
<div id="tutorial-move-panning" class="tutorial-text hide">
<span class="formfactor-desktop">Move the mouse to look around</span>
<span class="formfactor-touchfirst">Swipe the screen to look around</span>
</div>
<div id="tutorial-info-lives" class="tutorial-text hide scrollable-y">
<p>Lives are shown in the bottom left corner.</p>
<p>You lose lives by running into yourself and going out of bounds.</p>
<button id="buttonCompleteTutorial" class="hide">Press to Complete Tutorial</button>
</div>
<div id="copyright" class="small-details">
<p>© <a href="https://chrisraff.com" target="_blank">Chris Raff</a> 2024 </p>
</div>
<div id="lower-right-quarter">
<p class="small-details">Version 0.0.0</p>
</div>
<div id="menu-overlay">
<div id="menu-main" class="menu menu-active">
<h1>3D Snake</h2>
<br/>
<button class="button-main button-play">Play</button>
<button target-menu="menu-options" class="formfactor-desktop">Options</button>
<button class="button-fullscreen formfactor-touchfirst">Fullscreen</button>
</div>
<div id="menu-options" class="menu">
<h2>Options</h2>
<button class="button-back button-main">Back</button>
<button target-menu="menu-bindings">Edit Controls</button>
<button class="button-tutorial">Play Tutorial</button>
</div>
<div id="menu-bindings" class="menu">
<h2>Edit Controls</h2>
<button class="button-back button-main">Back</button>
<p>Click on a control to delete its binding. Click plus to add a new one.</p>
<div id="control-bindings">
<div class="control-row" target-action="actionUp">
<span class="binding-desc">Up</span>
<div class="binding-wrapper">
<div class="binding-area"></div>
<button class="button-add-binding">+</button>
</div>
</div>
<div class="control-row" target-action="actionDown">
<span class="binding-desc">Down</span>
<div class="binding-wrapper">
<div class="binding-area"></div>
<button class="button-add-binding">+</button>
</div>
</div>
<div class="control-row" target-action="actionLeft">
<span class="binding-desc">Left</span>
<div class="binding-wrapper">
<div class="binding-area"></div>
<button class="button-add-binding">+</button>
</div>
</div>
<div class="control-row" target-action="actionRight">
<span class="binding-desc">Right</span>
<div class="binding-wrapper">
<div class="binding-area"></div>
<button class="button-add-binding">+</button>
</div>
</div>
<div class="control-row" target-action="actionIn">
<span class="binding-desc">In</span>
<div class="binding-wrapper">
<div class="binding-area"></div>
<button class="button-add-binding">+</button>
</div>
</div>
<div class="control-row" target-action="actionOut">
<span class="binding-desc">Out</span>
<div class="binding-wrapper">
<div class="binding-area"></div>
<button class="button-add-binding">+</button>
</div>
</div>
<div class="control-row" target-action="actionRotateLeft">
<span class="binding-desc">Rotate Left</span>
<div class="binding-wrapper">
<div class="binding-area"></div>
<button class="button-add-binding">+</button>
</div>
</div>
<div class="control-row" target-action="actionRotateRight">
<span class="binding-desc">Rotate Right</span>
<div class="binding-wrapper">
<div class="binding-area"></div>
<button class="button-add-binding">+</button>
</div>
</div>
</div>
</div>
<div id="menu-pause" class="menu">
<h2>Paused</h2>
<button class="button-main button-resume">Resume</button>
<button target-menu="menu-options" class="formfactor-desktop">Options</button>
<button class="button-fullscreen formfactor-touchfirst">Fullscreen</button>
</div>
<div id="menu-gameover" class="menu">
<h2>Game Over</h2>
<div class="leaderboard-container">
<table id="leaderboard">
</table>
</div>
<button class="button-main" target-menu="menu-main" menu-no-queue>Main Menu</button>
</div>
<div id="menu-tutorial-complete" class="menu">
<h2>Tutorial Complete</h2>
<br/>
<button class="button-main button-play">Play</button>
<button target-menu="menu-options" class="formfactor-desktop">Options</button>
<button class="button-tutorial">Restart Tutorial</button>
</div>
</div>
</body>
</html>