-
Notifications
You must be signed in to change notification settings - Fork 1
/
menus.js
232 lines (209 loc) · 8.22 KB
/
menus.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
/*
Copyright (c) 2012, Tomi Leppänen
Copyright (c) 2012, Anssi "Miffyli" Kanervisto
Achtung, die Kurve! clone written in javascript and svg
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
var playerAmount = 1; // How many players are playing
var mainMenuOn; // If main menu is on or not
var playerSetButtonText = "1 Player";
/* Displays main menu */
function menu(dontClean) {
var OFFSETX = game_width/17; // Used to move whole menu
var OFFSETY = 0;
mainMenuOn = true;
if (!dontClean) {
timeout = clearTimeout(timeout);
clearArea(menuarea);
clearArea(gamearea);
bonuses = new Array();
startGameWithBots();
}
createText(game_width/2,160,"Line!","black",fontSize*2); // Title
createButton(game_width/4+OFFSETX, game_height/4,
400, 200, "Play", "play"); // Play
createButton(game_width/3*2+OFFSETX, // Changes amount of players
game_height/4,400, 100, playerSetButtonText, "plrAmount");
// Write "Player x" texts + keys + set buttons
var offset = 0;
for (var i = 0; i < playerAmount; i++) {
createText(game_width/6+OFFSETX, game_height/2+offset,
players[i].name, players[i].colour);
createText(game_width/3+30+OFFSETX, game_height/2+offset,
getKeyFromCode(players[i].keyL));
createText(game_width/2+30+OFFSETX, game_height/2+offset,
getKeyFromCode(players[i].keyR));
createButton(game_width/3*2+OFFSETX,game_height/2+offset, 160, 90,
"Set", "plr"+(i+1)+"Set");
offset += 100;
}
createText(game_width/3+30+OFFSETX, game_height/2-80, "Left");
createText(game_width/2+30+OFFSETX, game_height/2-80, "Right");
addSpaceHandler(function(){endBotGame();startGame();});
}
/* Displays retry menu, which is showed when all players are dead */
function retryMenu() {
var retryButton = createButton(game_width/2, game_height/2-150, 400, 200,
"Play again", "retry");
var menuButton = createButton(game_width/2, game_height/2+100, 400, 200,
"Main Menu", "rtnMenu");
addSpaceHandler(startGame);
}
/* Pauses the game */
function pauseGame() {
timeout = clearTimeout(timeout); // Stop the "loop"
removeInputKeyHandlers(); // Stop input system
createText(game_width/2,game_height/3,"Press space to continue!");
createButton(game_width/2,game_height/2,400,200,"Main menu",
"rtnMenu");
addSpaceHandler(continueGame);
}
/* Creates button with text and eventListener */
function createButton(x,y,width,height,text,type) {
var btn = document.createElementNS(NS,"rect");
btn = elementSetAttributes(btn,{"x":x-width/2, "y":y-height/2,
"width":width, "height":height, "fill":"#FFFFFF", "stroke":"black",
"stroke-width":2});
var btnText = document.createElementNS(NS,"text");
btnText = elementSetAttributes(btnText,{"x":x, "y":y+fontSize/4,
"font-family":font, "font-size":fontSize, "text-anchor":"middle"});
btnText.textContent = text;
menuarea.appendChild(btn);
menuarea.appendChild(btnText);
btn.addEventListener("click",
function(e){buttonClick(e,type,btn,btnText)},false);
btnText.addEventListener("click",
function(e){buttonClick(e,type,btn,btnText)},false);
btn.addEventListener("mouseover",function(e){buttonHoverOn(e,btn,btnText)},
false); // for effects when hovering ->
btnText.addEventListener("mouseover",
function(e){buttonHoverOn(e,btn,btnText)},false);
btn.addEventListener("mouseout",function(e){buttonHoverOff(e,btn,btnText)},
false);
btnText.addEventListener("mouseout",function(e){buttonHoverOff(e,btn,
btnText)},false);
return {"button":btn,"text":btnText}
}
/* Creates text */
function createText(x,y,text,colour,newFontSize,area) {
var svgText = document.createElementNS(NS,"text");
svgText = elementSetAttributes(svgText,{"x":x, "y":y, "font-family":font,
"font-size":fontSize, "text-anchor":"middle"});
if (colour) {
svgText = elementSetAttributes(svgText, {"fill":colour});
}
if (newFontSize) {
svgText = elementSetAttributes(svgText, {"font-size":newFontSize});
}
svgText.textContent = text;
if (area == "points") {
pointsarea.appendChild(svgText);
} else {
menuarea.appendChild(svgText);
}
return svgText;
}
/* Changes rect/text to bold when hovering above it */
function buttonHoverOn(e,btn,btnText) {
btnText.setAttributeNS(null, "font-size", fontSize + 2);
btn.setAttributeNS(null, "stroke-width", 3);
}
/* Changes rect/text back to normal when no longer hovering above it */
function buttonHoverOff(e,btn,btnText) {
btnText.setAttributeNS(null, "font-size", fontSize);
btn.setAttributeNS(null, "stroke-width", 2);
}
/* Checks which button was clicked and handles what should happen */
function buttonClick(e,btnType,btn,btnText) {
if (btnType == "play") {
mainMenuOn = false;
endBotGame();
startGame();
} else if (btnType == "plrAmount") {
clearArea(menuarea);
playerAmount++;
if (playerAmount > PLAYERS) {
playerAmount = 1;
playerSetButtonText = "1 Player";
} else {
playerSetButtonText = playerAmount + " Players";
}
menu(true);
} else if (btnType == "retry") {
startGame();
} else if (btnType == "rtnMenu") {
menu();
}
for (var i = 1; i <= playerAmount; i++) {
if (btnType == "plr"+i+"Set") {
setButtons(i);
}
}
}
var setButtonsHandler; // Saves event handler
/* Sets buttons for given player (asks and sets) */
function setButtons(playerNum,e,leftOrRight) {
clearArea(menuarea);
if (leftOrRight == null) {
createText(game_width/2,game_height/2,
"Press button for left button for player "+playerNum);
setButtonsHandler = function(event){setButtons(playerNum,event,"left")};
document.body.addEventListener("keydown",setButtonsHandler,true);
} else if (leftOrRight == "left") {
document.body.removeEventListener("keydown",setButtonsHandler,true);
players[playerNum-1].keyL = e.which;
createText(game_width/2,game_height/2,
"Press button for right button for player "+playerNum);
setButtonsHandler = function(event){setButtons(playerNum,event,
"right")};
document.body.addEventListener("keydown",setButtonsHandler,true);
} else if (leftOrRight == "right") {
document.body.removeEventListener("keydown",setButtonsHandler,true);
players[playerNum-1].keyR = e.which;
menu();
}
}
/* Returns readable string for key */
function getKeyFromCode(keycode) {
if ((keycode >= 48 && keycode <= 58) || (keycode >= 65 && keycode <= 90))
return String.fromCharCode(keycode); // Alfanumeric keys
if (keycode == 37) return "Left"; // Arrow keys ->
if (keycode == 38) return "Up";
if (keycode == 39) return "Right";
if (keycode == 40) return "Down";
if (keycode == 33) return "Page Up"; // Other special keys ->
if (keycode == 34) return "Page Down";
if (keycode == 35) return "End";
if (keycode == 36) return "Home";
if (keycode == 45) return "Delete";
if (keycode == 46) return "Delete";
if (keycode == 16) return "Shift";
if (keycode == 17) return "Ctrl";
if (keycode == 18) return "Alt";
if (keycode == 19) return "Pause";
if (keycode == 13) return "Enter";
if (keycode == 8) return "Backspace";
if (keycode == 188) return ",";
if (keycode == 190) return ".";
if (keycode == 109) return "-";
if (keycode == 61) return "+";
if (keycode >= 106 && keycode <= 111)
return String.fromCharCode(keycode-64);
if (keycode == 222) return "'";
if (keycode == 145) return "ScrollLock";
if (keycode >= 96 && keycode <= 106) // Numpad ->
return "Numpad "+(String.fromCharCode(keycode-48));
if (keycode == undefined) return "None"; // Just undefined
return "BadKey";
}