-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcontroller.html
More file actions
215 lines (197 loc) · 7.03 KB
/
controller.html
File metadata and controls
215 lines (197 loc) · 7.03 KB
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
<html>
<head>
<script type="text/javascript" src="https://www.airconsole.com/api/airconsole-1.9.0.js"></script>
<script type="text/javascript">
// Note: We manage the different div based views manually to keep the example simple.
// But you could use https://github.com/AirConsole/airconsole-ctrl-generator-example/blob/master/controller/libs/airconsole_view_manager.js
// with the Controller Generator or your own frontend framework based approach.
var LOBBY = "LOBBY";
var GAME = "GAME";
var transitions = {
LOBBY: {
entryAction: () => {
if(screen === LOBBY) return;
document.getElementById("lobby").style.display = "block";
document.getElementById("game_mode").style.display = "none";
this.screen = LOBBY;
},
},
GAME: {
entryAction: () => {
if(screen === GAME) return;
document.getElementById("lobby").style.display = "none";
document.getElementById("game_mode").style.display = "block";
this.screen = GAME;
},
},
};
var airconsole;
var screen = LOBBY;
/**
* Sets up the communication to the screen.
*/
function init() {
airconsole = new AirConsole({ orientation: "portrait" });
/*
* Checks if this device is part of the active game.
*/
airconsole.onActivePlayersChange = function (player) {
console.info(`ActivePlayersChange [${ airconsole.device_id }]: player number`, player);
var div = document.getElementById("game_output");
if (player !== undefined) {
div.innerHTML = ["Left Player", "Right Player"][player];
} else {
transitions[LOBBY].entryAction();
}
};
// We lose master controller status if the current master is not hero but a hero joins the session.
airconsole.onConnect = function(device_id) {
if (airconsole.getMasterControllerDeviceId() === airconsole.device_id) {
configurePrimaryController();
} else {
configureAdditionalController();
}
};
// The master controller status can change when the current master leaves the session.
airconsole.onDisconnect = function (device_id) {
if (airconsole.getMasterControllerDeviceId() === airconsole.device_id) {
configurePrimaryController();
} else {
configureAdditionalController();
}
};
function configureAdditionalController() {
if (airconsole.getDeviceId() !== airconsole.getMasterControllerDeviceId()) {
document.getElementById("lobby_players").style.display = "block";
document.getElementById("lobby_master").style.display = "none";
document.getElementById("wait_players").style.display = "none";
}
}
function configurePrimaryController() {
if (airconsole.getDeviceId() === airconsole.getMasterControllerDeviceId()) {
document.getElementById("lobby_players").style.display = "none";
document.getElementById("lobby_master").style.display = "block";
document.getElementById("wait_players").style.display = "block";
}
}
airconsole.onReady = function () {
if (!airconsole.arePlayersSilenced()) transitions[LOBBY].entryAction();
configurePrimaryController();
};
airconsole.onCustomDeviceStateChange = function (device_id, data) {
if(device_id === AirConsole.SCREEN) {
if(!!data.screen) transitions[data.screen].entryAction();
if (screen === LOBBY) {
if(data.enough_players){
document.getElementById("start_game").style.display = "block";
document.getElementById("wait_players").style.display = "none";
} else {
document.getElementById("start_game").style.display = "none";
document.getElementById("wait_players").style.display = "block";
}
}
}
};
/*
* Makes the device vibrate if the screen says so.
*/
airconsole.onMessage = function (from, data) {
if (from === AirConsole.SCREEN && data.vibrate) {
airconsole.vibrate(data.vibrate);
console.log("Vibrating: " + data.vibrate);
}
};
}
/**
* Tells the screen to move the paddle of this player.
* @param amount
*/
function move(amount) {
airconsole.message(AirConsole.SCREEN, { move: amount });
}
function startGame() {
airconsole.message(AirConsole.SCREEN, { start_game: true });
}
</script>
<style type="text/css">
@font-face {
font-family: "PressStart2P";
src: url("PressStart2P.ttf") format("truetype");
text-align: center;
}
html,
body {
height: 100%;
margin: 0px;
font-family: "PressStart2P", sans-serif;
color: white;
text-align: center;
background-color: black;
}
.button {
display: inline-block;
height: 45%;
width: 98%;
background-color: #222;
position: relative;
}
.button_label {
position: absolute;
left: 0px;
width: 100%;
top: 50%;
margin-top: -6px;
}
#game_output {
position: absolute;
top: 50%;
left: 0%;
width: 100%;
margin-top: -8px;
color: #777;
}
</style>
</head>
<body onload="init()">
<div style="height: 1%"></div>
<div id="lobby" style="width: 100%; height: 100%; display: block">
<div id="lobby_players" style="margin: auto; width: 60%; height: 60%; display: none">
<h1>Lobby</h1>
<p>... Waiting for Game Master ...</p>
</div>
<div id="lobby_master" style="margin: auto; width: 60%; height: 60%; display: none">
<h1>Lobby</h1>
<p id="start_game"
style="width: 200px; height: 100px; display: none; border-color: green; border-width: 2px; border-style: solid;"
onclick="startGame()">
Click to start
</p>
<p id="wait_players" style="width: 200px; height: 100px; display: none; border-color: red; border-width: 2px; border-style: solid">
Waiting for more players
</p>
</div>
</div>
<div id="game_mode" style="display: none">
<div
class="button"
ontouchstart="move(-50)"
ontouchend="move(0)"
onmousedown="move(-50)"
onmouseup="move(0)"
>
<div class="button_label">UP</div>
</div>
<div style="height: 8%"></div>
<div
class="button"
ontouchstart="move(50)"
ontouchend="move(0)"
onmousedown="move(50)"
onmouseup="move(0)"
>
<div class="button_label">DOWN</div>
</div>
</div>
<div id="game_output">... Waiting ...</div>
</body>
</html>