Skip to content

Commit fa3842d

Browse files
committed
Add instructions and touch controls to web version
1 parent 05b1aac commit fa3842d

File tree

2 files changed

+117
-4
lines changed

2 files changed

+117
-4
lines changed

html/index.html

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,135 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta name="color-scheme" content="dark">
57
<title>Space Travel</title>
68
<style>
79
body {
810
background: black;
11+
width: 100%;
12+
height: 100%;
13+
margin: 0;
14+
position: absolute;
15+
touch-action: none;
16+
user-select: none;
17+
-webkit-user-select: none;
918
}
1019
canvas {
11-
position: absolute;
20+
display: block;
21+
margin: 0 auto;
1222
max-width: 100%;
1323
max-height: 100%;
24+
position: relative;
1425
top: 50%;
15-
left: 50%;
16-
transform: translate(-50%, -50%);
26+
transform: translateY(-50%);
27+
touch-action: none;
28+
user-select: none;
29+
-webkit-user-select: none;
1730
}
1831
</style>
1932
</head>
2033
<body>
2134
<canvas id="canvas"></canvas>
2235
<script>Module = { canvas }</script>
2336
<script src="st.js"></script>
37+
<script>
38+
const intro =
39+
"Welcome to Space Travel\n\n" +
40+
"Travel to other planets and land slowly.\n\n";
41+
42+
const instructions =
43+
intro +
44+
"The arrow keys control your ship's acceleration and yaw.\n" +
45+
"The keys 3 and 4 control thrust.\n\n" +
46+
"If you crash, the bottom-right indicator will flash CL. " +
47+
"Press 2 to try again.";
48+
49+
const touchInstructions =
50+
intro +
51+
"Swipe vertically and horizontally to control your ship's acceleration and yaw.\n" +
52+
"Tap the top and bottom of the screen to control thrust.\n\n" +
53+
"If you crash, the bottom-right indicator will flash CL. " +
54+
"Tap anywhere to try again.";
55+
56+
let started = false;
57+
let crashed = false;
58+
59+
Module.oncrash = () => (crashed = true);
60+
61+
document.body.onclick = () => window.alert(instructions);
62+
63+
function press(code, duration = 50) {
64+
canvas.dispatchEvent(new KeyboardEvent("keydown", { code, bubbles: true }));
65+
setTimeout(
66+
() =>
67+
canvas.dispatchEvent(new KeyboardEvent("keyup", { code, bubbles: true })),
68+
duration
69+
);
70+
}
71+
72+
const touches = new Map();
73+
74+
document.body.ontouchstart = (e) => {
75+
e.preventDefault();
76+
77+
if (!started) {
78+
window.alert(touchInstructions);
79+
started = true;
80+
return;
81+
}
82+
83+
if (crashed) {
84+
press("Digit2");
85+
crashed = false;
86+
return;
87+
}
88+
89+
for (const t of e.changedTouches)
90+
touches.set(t.identifier, { x: t.pageX, y: t.pageY });
91+
};
92+
93+
document.body.ontouchmove = (e) => {
94+
e.preventDefault();
95+
96+
for (const t of e.changedTouches) {
97+
const touch = touches.get(t.identifier);
98+
if (!touch) continue;
99+
100+
const dx = t.pageX - touch.x;
101+
const dy = t.pageY - touch.y;
102+
103+
const distance = Math.hypot(dx, dy);
104+
105+
if (Math.abs(dx) > Math.abs(dy))
106+
press(dx < 0 ? "ArrowLeft" : "ArrowRight", 50 * distance);
107+
else {
108+
if (distance < 10) continue;
109+
press(dy > 0 ? "ArrowUp" : "ArrowDown", 50 * distance);
110+
}
111+
112+
touch.x = t.pageX;
113+
touch.y = t.pageY;
114+
touch.handled = true;
115+
}
116+
};
117+
118+
document.body.ontouchend = (e) => {
119+
e.preventDefault();
120+
121+
for (const t of e.changedTouches) {
122+
const touch = touches.get(t.identifier);
123+
touches.delete(t.identifier);
124+
if (!touch || touch.handled) continue;
125+
press(2 * e.pageY < window.innerHeight ? "Digit3" : "Digit4");
126+
}
127+
};
128+
129+
document.body.ontouchcancel = (e) => {
130+
e.preventDefault();
131+
for (const t of e.changedTouches) touches.delete(t.identifier);
132+
};
133+
</script>
24134
</body>
25135
</html>

st.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,9 @@ void updacc(int p)
683683
if (!lanflg) {
684684
if (ftmp1 * ftmp1 + ftmp2 * ftmp2 > crash) {
685685
crflg = goflg = true;
686+
#ifdef __EMSCRIPTEN__
687+
EM_ASM(Module.oncrash());
688+
#endif
686689
}
687690
lanflg = true;
688691
ftmp1 = rpar / dpar;

0 commit comments

Comments
 (0)