/
controller.js
262 lines (208 loc) · 6.21 KB
/
controller.js
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
/*
Menu Controller
*/
function showHelpScreen() {
// show btn-save, btn-load
if (screenState === "menu") {
$("#btn-save").show();
$("#btn-load").show();
$("#menu-screen").hide();
} else {
$("#game-screen").hide();
}
// show help overlay
$("#help-overlay").show();
}
function hideHelpScreen() {
//
if (screenState === "menu") {
$("#btn-save").hide();
$("#btn-load").hide();
$("#menu-screen").show();
} else {
$("#game-screen").show();
}
// show help overlay
$("#help-overlay").hide();
}
function reloadGameMenu() {
log("Load game menu");
// sort gameList first
gameList.sort(function (a, b) {
return a > b ? 1 : -1;
});
// generate html
var listbox = $("#menu-container");
listbox.html('');
gameList.forEach(function (game) {
listbox.append(`<div class="menu-item unselectable" unselectable="on"><div><span>${game}</span></div></div>`);
});
}
function showMenuScreen() {
// clear scenes
$("#game-screen").hide();
$("#menu-screen").hide();
$("#btn-save").hide();
$("#btn-load").hide();
$("#btn-join").html("play");
// show menu scene
$("#game-screen").show().delay(DEBUG ? 0 : 0).fadeOut(DEBUG ? 0 : 0, function () {
log("Loading menu screen");
$("#menu-screen").fadeIn(DEBUG ? 0 : 0, function () {
pickGame(gameIdx);
screenState = "menu";
});
});
}
function pickGame(idx) {
// check boundaries
// cycle
if (idx < 0) idx = gameList.length - 1;
if (idx >= gameList.length) idx = 0;
// transition menu box
var listbox = $("#menu-container");
listbox.css("transition", "top 0.2s");
listbox.css("-moz-transition", "top 0.2s");
listbox.css("-webkit-transition", "top 0.2s");
menuTop = MENU_TOP_POSITION - idx * 36;
listbox.css("top", `${menuTop}px`);
// overflow marquee
$(".menu-item .pick").removeClass("pick");
$(`.menu-item:eq(${idx}) span`).addClass("pick");
gameIdx = idx;
log(`> [Pick] game ${gameIdx + 1}/${gameList.length} - ${gameList[gameIdx]}`);
}
function startGamePickerTimer(direction) {
if (gamePickerTimer === null) {
pickGame(gameIdx + (direction === "up" ? -1 : 1));
log("Start game picker timer");
// velocity?
gamePickerTimer = setInterval(function () {
pickGame(gameIdx + (direction === "up" ? -1 : 1));
}, 200);
}
}
function stopGamePickerTimer() {
if (gamePickerTimer !== null) {
log("Stop game picker timer");
clearInterval(gamePickerTimer);
gamePickerTimer = null;
}
}
/*
Game controller
*/
function sendKeyState() {
// check if state is changed
if (unchangePacket > 0) {
// pack keystate
var bits = "";
KEY_BIT.slice().reverse().forEach(elem => {
bits += keyState[elem] ? 1 : 0;
});
var data = parseInt(bits, 2);
var arrBuf = new Uint8Array(2);
arrBuf[0] = data & ((1 << 8) - 1);
arrBuf[1] = data >> 8;
inputChannel.send(arrBuf);
unchangePacket--;
}
}
function startGameInputTimer() {
if (gameInputTimer === null) {
log("Start game input timer");
gameInputTimer = setInterval(sendKeyState, 1000 / INPUT_FPS)
}
}
function stopGameInputTimer() {
if (gameInputTimer !== null) {
log("Stop game input timer");
clearInterval(gameInputTimer);
gameInputTimer = null;
}
}
function setKeyState(name, state) {
if (name in keyState) {
keyState[name] = state;
unchangePacket = INPUT_STATE_PACKET;
}
}
function doButtonDown(name) {
$(`#btn-${name}`).addClass("pressed");
if (screenState === "menu") {
if (name === "up" || name === "down") {
startGamePickerTimer(name);
}
} else if (screenState === "game") {
setKeyState(name, true);
}
if (name === "help") {
showHelpScreen();
}
}
function doButtonUp(name) {
$(`#btn-${name}`).removeClass("pressed");
if (screenState === "menu") {
switch (name) {
case "up":
case "down":
stopGamePickerTimer();
break;
case "join":
case "a":
case "b":
case "x":
case "y":
case "start":
case "select":
startGame();
break;
case "quit":
popup("You are already in menu screen!");
break;
case "load":
popup("Lets play to load game!");
break;
case "save":
popup("Lets play to save game!");
break;
}
} else if (screenState === "game") {
setKeyState(name, false);
switch (name) {
case "join":
copyToClipboard(window.location.href.split('?')[0] + `?id=${escape(roomID)}`)
popup("Copy link to clipboard!")
break;
case "save":
conn.send(JSON.stringify({ "id": "save", "data": "" }));
break;
case "load":
conn.send(JSON.stringify({ "id": "load", "data": "" }));
break;
case "full":
// Fullscreen
screen = document.getElementById("game-screen");
console.log(screen.height, window.innerHeight);
if (screen.height === window.innerHeight) {
closeFullscreen();
} else {
openFullscreen(screen);
}
break;
case "quit":
location.reload()
stopGameInputTimer();
showMenuScreen();
// TODO: Stop game
conn.send(JSON.stringify({ "id": "quit", "data": "", "room_id": roomID }));
roomID = ""
$("#room-txt").val(roomID);
popup("Quit!");
break;
}
}
if (name === "help") {
hideHelpScreen();
}
}