-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
203 lines (177 loc) · 6.63 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
<!DOCTYPE html>
<head>
<title>Maze</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap"
rel="stylesheet"
/>
<style>
html,
body {
background-color: #555;
flex-direction: column;
position: absolute;
display: flex;
height: 100%;
width: 100%;
margin: 0;
font-family: 'Press Start 2P', -apple-system, BlinkMacSystemFont,
'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell',
'Open Sans', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
}
#main {
object-fit: contain;
height: 100%;
width: 100%;
}
#timer {
text-shadow: 1px 1px 2px black;
background-color: #0000009c;
font-size: xx-large;
position: absolute;
width: fit-content;
color: white;
padding: 8px;
margin: 8px;
right: 0;
top: 0;
}
</style>
<script defer>
let bestTime = Number(localStorage.getItem('score') || Infinity);
let timerRunning = false;
let startTime = 0;
const now = () => +new Date();
const env = {
t: (start) => {
if (start) {
startTime = now();
} else {
let elapsed = now() - startTime;
if (elapsed < bestTime) {
bestTime = elapsed;
}
// Reuse `startTime` as the final time in the display.
startTime = elapsed;
}
timerRunning = start;
}
};
setInterval(() => {
function toSec(x) {
return x == Infinity ? '0.0' : (x / 1000).toFixed(2);
}
const elapsed = timerRunning ? now() - startTime : startTime;
const timer = document.getElementById('timer');
timer.textContent = `${toSec(elapsed)} / ${toSec(bestTime)}`;
localStorage.setItem('score', bestTime);
}, 100);
fetch('./index.wasm')
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, { Math, env }))
.then((source) => {
const instance = source.instance;
const canvasData = new Uint8Array(
instance.exports.mem.buffer,
0x3000,
307200
);
const canvas = document.getElementById('main');
const context = canvas.getContext('2d');
const imageData = context.createImageData(320, 240);
const u8 = new Uint8Array(instance.exports.mem.buffer, 0, 4);
function onkey(down, event) {
switch (event.code) {
case 'KeyA': {
u8[0] = down;
break;
}
case 'KeyD': {
u8[1] = down;
break;
}
case 'KeyW': {
u8[2] = down;
break;
}
case 'KeyS': {
u8[3] = down;
break;
}
}
}
// prettier-ignore
document.addEventListener('keydown', onkey.bind(null, 1), false);
document.addEventListener('keyup', onkey.bind(null, 0), false);
function onClick() {
if (document.pointerLockElement) {
document.exitPointerLock();
} else {
(
canvas.requestPointerLock ||
canvas.mozRequestPointerLock ||
canvas.webkitRequestPointerLock
).call(canvas);
}
}
function onMouseMove(event) {
instance.exports.movementX.value += event.movementX;
}
function onPointerLockChange() {
if (document.pointerLockElement === null) {
document.removeEventListener('mousemove', onMouseMove);
} else {
document.addEventListener('mousemove', onMouseMove);
}
}
canvas.addEventListener('click', onClick);
document.addEventListener(
'pointerlockchange',
onPointerLockChange
);
onPointerLockChange();
const touches = {};
function onTouch(down, event) {
for (let touch of event.changedTouches) {
if (down) {
// prettier-ignore
index = touch.clientX < event.target.clientWidth * 0.5
? 0 // L
: 1 // R
u8[index] = 1;
touches[touch.identifier] = index;
} else {
u8[touches[touch.identifier]] = 0;
delete touches[touch.identifier];
}
}
// Move forward when touching L and R side of screen.
u8[2] = u8[0] & u8[1];
event.preventDefault();
}
canvas.addEventListener(
'touchstart',
onTouch.bind(null, 1),
false
);
canvas.addEventListener(
'touchend',
onTouch.bind(null, 0),
false
);
(function update() {
requestAnimationFrame(update);
instance.exports.run();
imageData.data.set(canvasData);
context.putImageData(imageData, 0, 0);
})();
});
</script>
</head>
<body>
<span id="timer">0.0 / 0.0</span>
<canvas id="main" width="320" height="240"></canvas>
</body>