-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscreen.html
367 lines (334 loc) · 10.5 KB
/
screen.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
<html>
<head>
<script type="text/javascript" src="https://www.airconsole.com/api/airconsole-1.9.0.js"></script>
<script type="text/javascript">
var airconsole;
var paddles;
var ball;
var score = [0, 0];
var canvas;
var last = null;
var score_el;
var paused = false;
var initialized = false;
var LOBBY = "LOBBY";
var GAME = "GAME";
let screen = LOBBY;
let transitions = {
LOBBY: {
entryAction: () => {
screen = LOBBY;
setGameScreen(screen);
airconsole.setActivePlayers(0);
resetBall(50, 0);
score = [0, 0];
score_el.innerHTML = score.join(":");
checkTwoPlayers();
},
},
GAME: {
entryAction: () => {
screen = GAME;
setGameScreen(screen);
airconsole.setActivePlayers(2);
resetBall(50, 0);
score = [0, 0];
score_el.innerHTML = score.join(":");
document.getElementById("wait").innerHTML = "";
},
},
};
function setGameScreen(screen) {
airconsole.setCustomDeviceStateProperty("screen", screen);
}
/**
* Sets up the communication to game pads.
*/
function setupConsole() {
airconsole = new AirConsole({ silence_inactive_players: true });
airconsole.onPause = function () {
console.warn("Pausing game");
paused = true;
};
airconsole.onResume = function () {
console.warn("Resuming game");
paused = false;
};
airconsole.onConnect = function (device_id) {
if (!initialized) {
transitions["LOBBY"].entryAction();
transitions[LOBBY].entryAction();
initialized = true;
}
checkTwoPlayers();
};
airconsole.onDisconnect = function (device_id) {
var player = airconsole.convertDeviceIdToPlayerNumber(device_id);
if (player !== undefined) {
// Player that was in game left the game.
// Setting active players to length 0.
airconsole.setActivePlayers(0);
}
checkTwoPlayers();
};
airconsole.onMessage = function (device_id, data) {
if (data.start_game) {
transitions[GAME].entryAction();
return;
}
var player = airconsole.convertDeviceIdToPlayerNumber(device_id);
if (player !== undefined && data.move !== undefined) {
paddles[player].move.y = data.move;
}
};
}
/**
* Checks if two players are connected!
*/
function checkTwoPlayers(send = true) {
var active_players = airconsole.getActivePlayerDeviceIds();
var connected_controllers = airconsole.getControllerDeviceIds();
// Only update if the game didn't have active players.
if (active_players.length === 0) {
if (connected_controllers.length > 2) {
document.getElementById("wait").innerHTML = "Only 2 players can play, sorry";
} else if (connected_controllers.length === 2) {
document.getElementById("wait").innerHTML = "";
} else if (connected_controllers.length === 1) {
document.getElementById("wait").innerHTML = "Need 1 more player!";
} else if (connected_controllers.length === 0) {
document.getElementById("wait").innerHTML = "Need 2 more players!";
}
}
if(send) airconsole.setCustomDeviceStateProperty("enough_players", connected_controllers.length === 2);
}
/**
* Sends a message to the device that it should vibrate;
*/
function vibrate(player) {
airconsole.message(airconsole.convertPlayerNumberToDeviceId(player), {
vibrate: 1000,
});
}
/**
* Shows who scored and updates the score afterwards.
*/
function displayScore(player) {
var name = airconsole.getNickname(
airconsole.convertPlayerNumberToDeviceId(player)
);
score_el.innerHTML = `<div style='font-size: 20px;margin-top:16px'> ${name} scored!</div>`;
window.setTimeout(function () {
score_el.innerHTML = score.join(":");
}, 1000);
}
/**
* body.onload function.
*/
function init() {
setupConsole();
// Setting up the game. Nothing AirConsole specific.
canvas = document.getElementById("canvas");
score_el = document.getElementById("score");
paddles = [
{
pos: {x: 10, y: 50},
move: {x: 0, y: 0},
el: document.getElementById("player_1"),
},
{
pos: {x: 190, y: 53},
move: {x: 0, y: 0},
el: document.getElementById("player_2"),
},
];
ball = {
pos: {x: 100, y: 50},
move: {x: 0, y: 0},
el: document.getElementById("ball"),
};
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
window.onresize = clearCanvas;
last = new Date().getTime();
loop();
}
/**
* Moving an element. Nothing AirConsole specific.
* @param entity
*/
function updatePos(entity, delta) {
entity.pos.x += entity.move.x * delta;
entity.pos.y += entity.move.y * delta;
}
/**
* Moving the ball and drawing the canvas. Nothing AirConsole specific.
*/
function loop() {
if (!paused && screen === GAME) {
var now = new Date().getTime();
var delta = now - last;
delta = Math.min(20, delta);
delta /= 1000;
last = now;
draw(true);
updatePos(paddles[0], delta);
updatePos(paddles[1], delta);
updatePos(ball, delta);
// paddle limit
paddles[0].pos.y = Math.min(Math.max(10, paddles[0].pos.y), 90);
paddles[1].pos.y = Math.min(Math.max(10, paddles[1].pos.y), 90);
// Wall bounce
if (ball.pos.y <= 1) {
ball.pos.y = 1;
ball.move.y *= -1;
updatePos(ball, 0);
}
if (ball.pos.y >= 99) {
ball.pos.y = 99;
ball.move.y *= -1;
updatePos(ball, 0);
}
// Paddle bounce
if (
ball.pos.x >= paddles[0].pos.x - 2 &&
ball.pos.x <= paddles[0].pos.x + 2 &&
ball.pos.y >= paddles[0].pos.y - 10 &&
ball.pos.y <= paddles[0].pos.y + 10
) {
ball.move.x *= -1;
ball.move.y += (ball.pos.y - paddles[0].pos.y) * 2;
ball.pos.x = paddles[0].pos.x + 2;
}
if (
ball.pos.x >= paddles[1].pos.x - 2 &&
ball.pos.x <= paddles[1].pos.x + 2 &&
ball.pos.y >= paddles[1].pos.y - 10 &&
ball.pos.y <= paddles[1].pos.y + 10
) {
ball.move.x *= -1;
ball.move.y += (ball.pos.y - paddles[1].pos.y) * 2;
ball.pos.x = paddles[1].pos.x - 2;
}
// Score
if (ball.pos.x >= 200) {
score[0] += 1;
displayScore(0);
resetBall(-ball.move.x, ball.move.y / 2);
vibrate(1);
if(score[0] > 3){
transitions[LOBBY].entryAction();
}
}
if (ball.pos.x <= 0) {
score[1] += 1;
displayScore(1);
resetBall(-ball.move.x, ball.move.y / 2);
vibrate(0);
if(score[1] > 3){
transitions[LOBBY].entryAction();
}
}
}
draw();
requestAnimationFrame(loop);
}
/**
* Reseting the ball. Nothing Game Console specific.
* @param move_x
* @param move_y
*/
function resetBall(move_x, move_y) {
clearCanvas();
ball.pos.x = 100;
ball.pos.y = 50;
ball.move.x = move_x;
ball.move.y = move_y;
}
/**
* Reseting the ball. Nothing Game Console specific.
* @param move_x
* @param move_y
*/
function clearCanvas() {
var ctx = canvas.getContext("2d");
ctx.fillStyle = "black";
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
/**
* Drawing the game scene. Nothing Game Console specific.
* @param clear
*/
function draw(clear) {
// Draw
var ctx = canvas.getContext("2d");
var zoom = canvas.clientHeight / 100;
function transform(x) {
x *= zoom;
return x | 0; // round
}
ctx.fillStyle = clear ? "black" : "white";
ctx.fillRect(transform(paddles[0].pos.x - 1), transform(paddles[0].pos.y - 10), transform(2), transform(20));
ctx.fillRect(transform(paddles[1].pos.x - 1), transform(paddles[1].pos.y - 10), transform(2), transform(20));
ctx.beginPath();
ctx.arc(transform(ball.pos.x), transform(ball.pos.y), transform(clear ? 3 : 2), 0, 2 * Math.PI, false);
ctx.fill();
ctx.closePath();
}
</script>
<style type="text/css">
@font-face {
font-family: "PressStart2P";
src: url("PressStart2P.ttf") format("truetype");
}
html,
body {
height: 100%;
margin: 0px;
font-family: "PressStart2P", sans-serif;
color: white;
text-align: center;
}
.game {
background-color: black;
top: 50%;
margin-top: -25%;
padding-bottom: 50%;
overflow: hidden;
position: relative;
}
#canvas {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
}
#score {
position: absolute;
top: 10%;
left: 0px;
width: 100%;
font-size: 48px;
}
#wait {
position: absolute;
top: 10%;
margin-top: 80px;
left: 0px;
width: 100%;
font-size: 24px;
color: lime;
}
</style>
</head>
<body onload="init()">
<div class="game">
<canvas id="canvas"></canvas>
<div id="score">0:0</div>
<div id="wait"></div>
</div>
</body>
</html>