-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
431 lines (335 loc) · 12.3 KB
/
main.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
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
function openFullscreen(elem) {
if (window.innerHeight == screen.height && window.innerWidth == screen.width) {
closeFullscreen();
return;
}
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) { /* Firefox */
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) { /* IE/Edge */
elem.msRequestFullscreen();
}
}
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) { /* Firefox */
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) { /* Chrome, Safari and Opera */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) { /* IE/Edge */
document.msExitFullscreen();
}
}
//listen for when the screen changes to full screen
document.addEventListener("fullscreenchange", function () {
setTimeout(function () { adjust_to_fullscreen(); }, 250);
});
document.addEventListener("mozfullscreenchange", function () {
setTimeout(function () { adjust_to_fullscreen(); }, 250);
});
document.addEventListener("webkitfullscreenchange", function () {
setTimeout(function () { adjust_to_fullscreen(); }, 250);
});
document.addEventListener("msfullscreenchange", function () {
setTimeout(function () { adjust_to_fullscreen(); }, 250);
});
function adjust_to_fullscreen() {
gamecanvas.width = window.innerWidth;
gamecanvas.height = window.innerHeight;
ui_canvas.width = window.innerWidth;
ui_canvas.height = window.innerHeight;
//if it is in full screen mode
if (document.fullscreen || window.innerHeight == screen.availHeight) {
ui_canvas.width = screen.availWidth;
ui_canvas.height = screen.availHeight;
}
//change the values of the configurations
config();
//adjust the positions of the players and the ball
for (let i = 0; i < players.length; i++) {
players[i].x *= gamecanvas.width / screen_dimensions.width;
players[i].y *= gamecanvas.height / screen_dimensions.height;
}
if (ball != undefined) {
ball.x *= window.innerWidth / screen_dimensions.width;
ball.y *= window.innerHeight / screen_dimensions.height;
}
screen_dimensions = {
width: window.innerWidth,
height: window.innerHeight
};
}
function create_canvas() {
gamecanvas = document.createElement("canvas");
gamecanvas.id = "gamecanvas";
gamecanvas.style.border = "1px solid #000000";
gamecanvas.width = window.innerWidth;
gamecanvas.height = window.innerHeight;
ctx = gamecanvas.getContext("2d");
document.getElementById("game_div").appendChild(gamecanvas);
swipe_listener = new Swipe_Listener(gamecanvas);
}
let degree = 0;
let photo = 0;
//draws the elements in the canvas
function circle_drawer() {
ctx.clearRect(0, 0, gamecanvas.width, gamecanvas.height);
//draw the grass
if (window.innerHeight > window.innerWidth) {
//smartphone/portrait
//the top and the bottom of the screen are taken as the right and left sides respectively
//the grass
ctx.drawImage(document.getElementById("pitch"), 0, outside_pitch.side, gamecanvas.width - outside_pitch.top, gamecanvas.height - (outside_pitch.side * 2));
} else {
//landscape
ctx.drawImage(document.getElementById("pitch_flipped"), outside_pitch.side, outside_pitch.top, gamecanvas.width - (outside_pitch.side * 2), gamecanvas.height - outside_pitch.top);
}
//the back, pause and play buttons
ctx.drawImage(back_button.image, back_button.x, back_button.y, back_button.width, back_button.height);
ctx.drawImage(fullscreen_button.image, fullscreen_button.x, fullscreen_button.y, fullscreen_button.width, fullscreen_button.height);
if (gamesession) {
ctx.drawImage(pause_button.image, pause_button.x, pause_button.y, pause_button.width, pause_button.height);
} else {
ctx.drawImage(play_button.image, play_button.x, play_button.y, play_button.width, play_button.height);
}
//draw the players
for (let i = 0; i < players.length; i++) {
ctx.beginPath();
ctx.arc(players[i].x, players[i].y, players[i].radius, 0, 2 * Math.PI);
ctx.stroke();
ctx.fillStyle = players[i].color;
ctx.fill();
//draw a white circle on the currently active team
if (players[i].team == turn) {
ctx.beginPath();
ctx.arc(players[i].x, players[i].y, players[i].radius / 2, 0, 2 * Math.PI);
ctx.stroke();
ctx.fillStyle = 'white';
ctx.fill();
}
}
//ball spinning mechanism, DISABLED
if (degree % 5 == 0 && (ball.unit_x > 0.25 || ball.unit_y > 0.25)) {
photo++;
if (photo > 3) {
photo = 0;
}
}
degree++;
//draw the ball
ctx.drawImage(document.getElementById(ball.id[0]), ball.x, ball.y, ball.dimensions, ball.dimensions);
//draw the nets
if (window.innerWidth > window.innerHeight) {
ctx.drawImage(document.getElementById("net1"), goal_post.x1, goal_post.y1, goal_post.width, goal_post.height);
ctx.drawImage(document.getElementById("net2"), goal_post.x2, goal_post.y2, goal_post.width, goal_post.height);
} else {
ctx.drawImage(document.getElementById("net1_potrait"), goal_post.x1, goal_post.y1, goal_post.width, goal_post.height);
ctx.drawImage(document.getElementById("net2_potrait"), goal_post.x2, goal_post.y2, goal_post.width, goal_post.height);
}
//draw the turn indicator
ctx.beginPath();
ctx.arc(turn_indicator[`team_${turn}`].x, turn_indicator[`team_${turn}`].y, turn_indicator[`team_${turn}`].radius, 0, 2 * Math.PI);
ctx.stroke();
ctx.fillStyle = turn_indicator[`team_${turn}`].color;
ctx.fill();
}
function start_game() {
change_display("game_div");
create_canvas();
create_ball();
create_players(players_number);
home_score = 0;
away_score = 0;
circle_drawer();
gamesession = true;
turn_taking(); //defined in player.js
game_time_counter = 0;
if (game_mode == 'time') {
time_keeper();
}
//play Cheering audio
CurrentAudio = CheeringNormal;
CurrentAudio.play();
requestAnimationFrame(gameplay);
}
var test = true;
let game_time_counter;;
function gameplay() {
player_movement();
time_displayer();
if (gamesession) {
if (CurrentAudio.paused) CurrentAudio.play();
//increase the value of the counter
game_time_counter++;
//try adjusting the screen after every several frames just incase it was changed to/from fullscreen
if (game_time_counter % 50 == 0) {
if (gamecanvas.height != window.innerHeight || gamecanvas.width != window.innerWidth) {
adjust_to_fullscreen();
}
}
requestAnimationFrame(gameplay);
} else {
//If game is paused, pause the audio
CurrentAudio.pause();
}
score_keeper();
}
function change_display(div_to_display) {
let divs = ["main_menu", "game_div"];
divs.forEach(div => {
if (div == div_to_display) {
document.getElementById(div_to_display).style.display = "block";
} else {
document.getElementById(div).style.display = "none";
}
});
document.getElementById('main_menu').style.display == "none" ? in_main_menu.home = false : in_main_menu.home = true;
if (in_main_menu.home) {
draw_ui();
}
}
//a goal has been scored
function goal() {
//Fans celebrating
CurrentAudio.pause();
CurrentAudio = CheeringGoal;
CurrentAudio.currentTime = 0;
CurrentAudio.play();
//take the ball back to the center
ball.x = ball_initial_position.x;
ball.y = ball_initial_position.y;
ball.unit_x = 0;
ball.unit_y = 0;
//reposition the players
initial_players_positioning();
players.forEach(player => {
player.unit_x = 0;
player.unit_y = 0;
});
setTimeout(() => {
CurrentAudio.pause();
CurrentAudio = CheeringNormal;
CurrentAudio.play();
}, 16000);
}
//keeps track of the score in the game
let home_score = 0;
let away_score = 0;
function score_keeper(scorer) {
//check who has scored
if (scorer == 'home') {
home_score++;
turn_taking('home score')
} else if (scorer == 'away') {
away_score++;
turn_taking('away score');
}
//display the scores
ctx.font = scores_display.font;
ctx.fillStyle = scores_display.color;
ctx.textAlign = "center";
if (window.innerWidth > window.innerHeight) {
//on laptops
ctx.fillText(`${home_score} : ${away_score}`, scores_display.x, scores_display.y);
} else {
//on smartphone (potrait displays)
ctx.save();
ctx.rotate(90 * Math.PI / 180);
ctx.fillText(`${home_score} - ${away_score}`, scores_display.x, scores_display.y);
ctx.restore();
}
if (game_mode == 'goals' && gamesession) {
if (home_score == game_duration || away_score == game_duration) {
gamesession = false;
setTimeout(function () {
document.getElementById("game_div").removeChild(gamecanvas);
change_display("main_menu");
}, 1000);
}
}
}
//keeps track of time in the game
let remaining_time, timer;
let time_keeper = () => {
remaining_time = game_duration
timer = setInterval(() => {
if (remaining_time == 0) {
//if time's up
clearInterval(timer);
} else {
if (gamesession) {
//if there is still time remaining
remaining_time--;
}
}
}, 1000);
}
function time_displayer() {
let color;
if (game_mode == 'time') {
//change color according to the time remaining
remaining_time < 10 ? color = time_display.time_up_color : color = time_display.color;
//display the scores
ctx.font = time_display.font;
ctx.fillStyle = color;
ctx.textAlign = "center";
if (remaining_time == 1) FinalWhistle.play(); //blow the final whistle
if (remaining_time == 0) {
//if time's up
time_up();
} else {
//if there is still time remaining
let minutes = Math.floor(remaining_time / 60).toFixed(0);
let seconds = remaining_time % 60;
//display time as two digits
seconds < 10 ? seconds = '0' + seconds : seconds = String(seconds);
if (window.innerWidth > window.innerHeight) {
//on laptops
ctx.fillText(`${minutes} : ${seconds}`, time_display.x, time_display.y);
} else {
//on smartphone (potrait displays)
ctx.save();
ctx.rotate(90 * Math.PI / 180);
ctx.fillText(`${minutes} : ${seconds}`, time_display.x, time_display.y);
ctx.restore();
}
}
} else if (game_mode == 'goals') {
ctx.font = time_display.font;
ctx.fillStyle = color;
ctx.textAlign = "center";
if (window.innerWidth > window.innerHeight) {
//on laptops
ctx.fillText(`First to ${game_duration} goals`, time_display.x, time_display.y);
} else {
//on smartphone (potrait displays)
ctx.save();
ctx.rotate(90 * Math.PI / 180);
ctx.fillText(`First to ${game_duration} goals`, time_display.x, time_display.y);
ctx.restore();
}
}
}
//gets called when the given time for a match has elapsed
function time_up() {
if (window.innerWidth > window.innerHeight) {
//on laptops
ctx.fillText(`Time's Up!`, time_display.x, time_display.y);
} else {
//on smartphone (potrait displays)
ctx.save();
ctx.rotate(90 * Math.PI / 180);
ctx.fillText(`Time's Up!`, time_display.x, time_display.y);
ctx.restore();
}
//stop the game
gamesession = false;
setTimeout(function () {
document.getElementById("game_div").removeChild(gamecanvas);
change_display("main_menu");
}, 2000);
}