-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.html
437 lines (391 loc) · 11.9 KB
/
controller.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="msapplication-TileColor" content="#943827">
<meta name="msapplication-TileImage" content="/favicon-144.png">
<link rel="apple-touch-icon-precomposed" href="/favicon-152.png">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<title>Sharky Shark Controller</title>
<style>
html, body {
height: 100%;
}
body {
position: relative;
color: white;
background: #03a;
}
input, button {
display: block;
width: 80%;
max-width: 240px;
margin: .6em auto 1em;
padding: .2em;
border: 1px solid #888;
outline: 0;
text-transform: uppercase;
text-align: center;
font-size: 24px;
font-family: VT323, monospace;
}
#joinRoom {
padding-bottom: 1em;
}
button {
position: relative;
padding: .4em;
border-radius: 50%;
cursor: pointer;
background: #fff;
}
button:before,
button:after {
content: '';
position: absolute;
top: 50%;
height: 4px;
width: 24%;
margin-top: -2px;
background-color: #ffaaaa;
}
button:before {
left: 2%;
}
button:after {
right: 2%;
}
p {
height: 150px;
line-height: 150px;
margin: 0;
font-size: 48px;
}
#lobbyScreen img {
display: inline-block;
margin-right: 12px;
}
#lobbyScreen h1 {
display: inline-block;
margin-bottom: 8px;
}
#lobbyScreen .inner {
background-image: url(resource/pretty_flower/hibiscus.png) no-repeat top right;
}
#playScreen * {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}
#roundNo {
display: none;
}
.roundNo #roundNo {
display: block;
}
#playName,
#playScore
{
display: none;
}
.touchup #playName,
.touchup #playScore
{
display: block;
}
#chomping {
display: none;
font-size: 48px;
}
.touchdown #chomping {
display: block;
}
#deadScreen {
background-color: #000000;
}
body>div {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
display: none;
font-family: VT323, monospace;
-moz-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
text-size-adjust: 100%;
text-align: center;
}
.inner {
position: absolute;
top: 50%; left: 0; right: 0;
height: 300px;
margin-top: -150px;
}
.lobbyScreen #lobbyScreen,
.waitScreen #waitScreen,
.playScreen #playScreen,
.deadScreen #deadScreen {
display: block;
}
#waitScreen {
z-index: 100;
top: 50%;
height: 240px;
left: 10%;
right: 10%;
margin-top: -120px;
border: 2px solid black;
background: white;
box-shadow: 0 0 0 1000px rgba(255, 255, 255, 0.8);
}
#waitScreen h1 {
margin-bottom: .4em;
font-size: 48px;
color: black;
}
#waitScreen p {
line-height: 18px;
width: 80%;
margin: 1em auto 0;
font-size: 18px;
color: black;
}
.waiting #button {
/* load animation, if we have time */
}
</style>
<link href='http://fonts.googleapis.com/css?family=VT323' rel='stylesheet' type='text/css'>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','http://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-49939229-3', 'rl.io');
ga('send', 'pageview');
</script>
</head>
<body class="lobbyScreen">
<div id="lobbyScreen">
<div class="inner">
<img src="./favicon-32.png" alt="Ben Eath, Ace Surfer">
<h1>Ben Eath: the Surf Ace</h1>
<form id="joinRoom">
<input id="playerName" type="text" name="playerName" placeholder="name" maxlength="5">
<input id="roomId" type="text" name="roomId" placeholder="Room ID">
<button id="button" type="submit" name="submit">Surf's up!</button>
</form>
</div>
</div>
<div id="waitScreen">
<h1>Waiting</h1>
<p id="waitMessage">Tilt to move, tap to chomp. Place the top of your phone in your left hand and tap to start!</p>
</div>
<div class="roundNo" id="playScreen">
<div class="inner">
<p id="roundNo">Round <span id="actualRoundNumber">1</span></p>
<p id="playName">Riley</p>
<p id="playScore">0</p>
<p id="chomping">CHOMP!</p>
</div>
</div>
<div id="deadScreen">
<h1>You have died! Thanks for sharking!</h1>
</div>
<script src="./lib/socket.io.js"></script>
<script>
// This entire thing seriously needs some (refactoring)
;(function(global) {
var socket;
var connectSocket = function() {
socket = io.connect('http://surf.rl.io:9000', {
'reconnection delay': 400,
'max reconnection attempts': 10
});
};
connectSocket();
var joinRoom = function(nameValue, idValue) {
socket.emit('joinRoom', 'controller', nameValue, idValue);
};
navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate;
var vibratePhone = function(duration) {
if (navigator.vibrate) {
if(typeof duration !== 'number') {
duration = 600;
}
navigator.vibrate(duration);
}
};
// DOM elements
var body = document.body;
var form = document.getElementById('joinRoom');
var playerName = document.getElementById('playerName');
var roomId = document.getElementById('roomId');
var button = document.getElementById('button');
var waitScreen = document.getElementById('waitScreen');
var waitMessage = document.getElementById('waitMessage');
var playScreen = document.getElementById('playScreen');
var actualRoundNumber = document.getElementById('actualRoundNumber');
var scores = [
document.getElementById('playScore')
];
var names = [
document.getElementById('playName')
];
var reconnectName, reconnectId;
var shark = {
direction: 0,
depth: 0,
score: 0
};
var playerColors = [
'#f00',
'#00f',
'#0f0',
'#ff0',
'#f88',
'#80f',
'#fff',
'#000'
];
var addClass = (function() {
if (body.classList) {
return function(el, className) {
el.classList.add(className);
};
} else {
return function(el, className) {
el.className += ' ' + className;
};
}
})();
var changeScreen = function(name) {
body.className = name + 'Screen';
};
// updated: now using dynamic latching, meaning that any value
// [-1, 1] can be sent instead of a finite set of values
var latchDegrees = 3;
var maxDegrees = 30;
var latch = latchDegrees / maxDegrees;
function bounded(angle, max) {
return Math.min(Math.max(angle, -max), max);
}
function latchCheck(measurement, metric, fn) {
// normalize to [-1, 1]
measurement = bounded(measurement, maxDegrees) / maxDegrees;
// only sends if their thumb is on the device
if (Math.abs(measurement - shark[metric]) >= latch) {
shark[metric] = measurement;
socket.emit(fn, measurement);
}
}
form.addEventListener("submit", function(e) {
var nameValue = playerName.value;
var idValue = roomId.value;
if (nameValue && idValue) {
addClass(body, 'waiting');
joinRoom(nameValue, idValue);
reconnectName = nameValue;
reconnectId = idValue;
}
e.preventDefault();
return false;
}, false);
socket.on('verifyRoom', function(roomExists, index) {
if(roomExists) {
waitMessage.textContent = 'Tilt to move, tap to chomp. Place the top of your phone in your left hand and tap to start!';
changeScreen('wait');
body.style.backgroundColor = playerColors[index];
names.forEach(function(el) {
el.textContent = reconnectName;
});
socket.on('notifyDeath', function() {
changeScreen('dead');
vibratePhone();
});
socket.on('updateScore', function(score) {
shark.score = score;
scores.forEach(function(el) {
el.textContent = score;
});
vibratePhone();
});
socket.on('startRound', function(round) {
playScreen.className = "roundNo";
actualRoundNumber.textContent = round;
setTimeout(function() {
playScreen.className = "touchup";
}, 1600);
changeScreen('play');
vibratePhone();
});
socket.on('finishRound', function(round, message) {
waitMessage.innerText = message;
changeScreen('wait');
vibratePhone();
});
// Add other socket listeners here (blood, etc)
global.addEventListener('deviceorientation', function(eventData) {
// gamma: left-to-right tilt, right is positive;
// handles depth, -90deg to 90deg
var gamma = eventData.gamma;
// beta: front-to-back tilt, front is positive;
// handles direction, -90deg to 90deg
var beta = eventData.beta;
latchCheck(gamma, 'depth', 'setDepth');
latchCheck(beta, 'direction', 'setDirection');
}, false);
waitScreen.addEventListener('mousedown', function() {
socket.emit('chomp');
});
waitScreen.addEventListener('touchstart', function() {
socket.emit('chomp');
});
waitScreen.addEventListener('pointerdown', function() {
socket.emit('chomp');
});
playScreen.addEventListener('mousedown', function() {
playScreen.className = 'touchdown';
socket.emit('chomp');
}, false);
playScreen.addEventListener('touchstart', function() {
playScreen.className = 'touchdown';
socket.emit('chomp');
}, false);
playScreen.addEventListener('pointerdown', function() {
playScreen.className = 'touchdown';
socket.emit('chomp');
}, false);
playScreen.addEventListener('mouseup', function() {
playScreen.className = 'touchup';
}, false);
playScreen.addEventListener('touchend', function() {
playScreen.className = 'touchup';
}, false);
playScreen.addEventListener('pointerup', function() {
playScreen.className = 'touchup';
}, false);
}
});
socket.on('error', function(e) {
socket.socket.connect();
joinRoom(reconnectName, reconnectId);
});
socket.on('disconnect', function() {
socket.socket.connect();
changeScreen('lobby');
});
socket.on('bootPlayer', function() {
changeScreen('lobby');
});
})(this);
</script>
</body>
</html>