-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.js
310 lines (261 loc) · 14 KB
/
player.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
function Player(x_position, y_position, circle_radius, players_team, identity, color) {
this.x = x_position;
this.y = y_position;
this.radius = circle_radius;
this.team = players_team;
this.mass = 1;
this.id = identity;
this.color = color;
this.unit_x = 0; //distance player moves horizontally
this.unit_y = 0; //distance player moves vertically
this.friction = player_friction;
}
function Ball(x_position, y_position, dimensions, identity) {
this.x = x_position;
this.y = y_position;
this.dimensions = dimensions;
this.mass = 0.5;
this.id = identity;
this.unit_x = 0; //distance ball moves horizontally
this.unit_y = 0; //distance ball moves vertically
this.friction = ball_friction;
}
//creates the ball
function create_ball() {
ball = new Ball(ball_initial_position.x, ball_initial_position.y, ball_dimensions, ball_id);
}
//creates the pieces for playing
function create_players(number) {
players = [];
//create the players for both teams and populate them into the array
for (let i = 0; i < number; i++) {
if (i < number / 2) {
players.push(new Player(0, 0, player_radius, 1, i, team_1_color));
} else {
players.push(new Player(0, 0, player_radius, 2, i, team_2_color));
}
}
//position the players properly in the field
initial_players_positioning();
}
function player_movement() {
//loop over the players, acting on each
for (let i = 0; i < players_number; i++) {
//apply friction
players[i].unit_x *= players[i].friction;
players[i].unit_y *= players[i].friction;
ball.unit_x *= ball.friction;
ball.unit_y *= ball.friction;
let repeat = speed;
do {
if (window.innerHeight > window.innerWidth) {
//SMARTPHONE form factor
//if a player is in the goal
if (players[i].y - players[i].radius < goal_post.y1 + goal_post.height - 10 || players[i].y + players[i].radius > goal_post.y2 + 10) {
//contact with goal post
if (players[i].x + players[i].radius >= goal_post.x1 + goal_post.width - 10 && players[i].unit_x > 0) {
players[i].unit_x = -players[i].unit_x;
} else if (players[i].x - players[i].radius <= goal_post.x1 + 10 && players[i].unit_x < 0) {
players[i].unit_x = -players[i].unit_x;
}
}
//if a player is headed for the goal
if (players[i].y - players[i].radius <= goal_post.y1 + goal_post.height && players[i].unit_y < 0 &&
players[i].x - players[i].radius - 5 > goal_post.x1 && players[i].x + players[i].radius + 5 < goal_post.x1 + goal_post.width) {
} else if (players[i].y + players[i].radius >= goal_post.y2 && players[i].unit_y > 0 &&
players[i].x - players[i].radius - 5 > goal_post.x1 && players[i].x + players[i].radius + 5 < goal_post.x1 + goal_post.width) {
}
//check for player contact with field boundary
else if (players[i].x + players[i].radius >= gamecanvas.width - outside_pitch.top - 2 && players[i].unit_x > 0) {
players[i].unit_x = -players[i].unit_x;
} else if (players[i].x - players[i].radius <= 2 && players[i].unit_x < 0) {
players[i].unit_x = -players[i].unit_x;
} else if (players[i].y + players[i].radius >= gamecanvas.height - outside_pitch.side - 2 && players[i].unit_y > 0) {
players[i].unit_y = -players[i].unit_y;
} else if (players[i].y - players[i].radius <= outside_pitch.side + 2 && players[i].unit_y < 0) {
players[i].unit_y = -players[i].unit_y;
}
//check for ball contact with field boundary
//if the ball is in the goal
if (ball.y < goal_post.y1 + goal_post.height - 5 || ball.y + ball.dimensions > goal_post.y2 + 5) {
//GOOAAAAAAL!!!!
if (ball.y + ball.dimensions < goal_post.y1 + goal_post.height) {
goal();
score_keeper('away');
//contact with goal post
if (ball.x + ball.dimensions >= goal_post.x1 + goal_post.width - 10 && ball.unit_x > 0) {
ball.unit_x = -ball.unit_x;
} else if (ball.x <= goal_post.x1 + 10 && ball.unit_x < 0) {
ball.unit_x = -ball.unit_x;
}
} else if (ball.y > goal_post.y2) {
goal();
score_keeper('home');
//contact with goal post
if (ball.x + ball.dimensions >= goal_post.x1 + goal_post.width - 10 && ball.unit_x > 0) {
ball.unit_x = -ball.unit_x;
} else if (ball.x <= goal_post.x1 + 10 && ball.unit_x < 0) {
ball.unit_x = -ball.unit_x;
}
}
}
//if the ball is headed for the goal
if (ball.y <= outside_pitch.side + 2 && ball.unit_y < 0 &&
ball.x - 5 > goal_post.x1 && ball.x + ball.dimensions + 5 < goal_post.x1 + goal_post.width) {
} else if (ball.y + ball.dimensions >= outside_pitch.side + play_area.height - 2 && ball.unit_y > 0 &&
ball.x - 5 > goal_post.x1 && ball.x + ball.dimensions + 5 < goal_post.x1 + goal_post.width) {
}
//check for ball contact with field boundary
else if (ball.x + ball.dimensions >= gamecanvas.width - outside_pitch.top - 2 && ball.unit_x > 0) {
ball.unit_x = -ball.unit_x;
} else if (ball.x <= 2 && ball.unit_x < 0) {
ball.unit_x = -ball.unit_x;
} else if (ball.y + ball.dimensions >= gamecanvas.height - outside_pitch.side - 2 && ball.unit_y > 0) {
ball.unit_y = -ball.unit_y;
} else if (ball.y <= outside_pitch.side + 2 && ball.unit_y < 0) {
ball.unit_y = -ball.unit_y;
}
} else {
//LAPTOP
//if a player is in the goal
if (players[i].x - players[i].radius < outside_pitch.side || players[i].x + players[i].radius > outside_pitch.side + play_area.width) {
//contact with goal post
if (players[i].y + players[i].radius >= goal_post.y1 + goal_post.height - 10 && players[i].unit_y > 0) {
players[i].unit_y = -players[i].unit_y;
} else if (players[i].y - players[i].radius <= goal_post.y1 + 10 && players[i].unit_y < 0) {
players[i].unit_y = -players[i].unit_y;
}
}
//if a player is headed for the goal
if (players[i].x - players[i].radius <= outside_pitch.side + 2 && players[i].unit_x < 0 &&
players[i].y - players[i].radius - 5 > goal_post.y1 && players[i].y + players[i].radius + 5 < goal_post.y1 + goal_post.height) {
} else if (players[i].x + players[i].radius >= outside_pitch.side + play_area.width - 2 && players[i].unit_x > 0 &&
players[i].y - players[i].radius - 5 > goal_post.y1 && players[i].y + players[i].radius + 5 < goal_post.y1 + goal_post.height) {
}
//check for player contact with field boundary
else if (players[i].x + players[i].radius >= gamecanvas.width - outside_pitch.side - 2 && players[i].unit_x > 0) {
players[i].unit_x = -players[i].unit_x;
} else if (players[i].x - players[i].radius <= outside_pitch.side + 2 && players[i].unit_x < 0) {
players[i].unit_x = -players[i].unit_x;
} else if (players[i].y + players[i].radius >= gamecanvas.height - 2 && players[i].unit_y > 0) {
players[i].unit_y = -players[i].unit_y;
} else if (players[i].y - players[i].radius <= outside_pitch.top + 2 && players[i].unit_y < 0) {
players[i].unit_y = -players[i].unit_y;
}
//check for ball contact with field boundary
//if the ball is in the goal
if (ball.x < outside_pitch.side - 5 || ball.x + ball.dimensions > outside_pitch.side + play_area.width) {
//GOOAAAAAAL!!!!
if (ball.x + ball.dimensions < outside_pitch.side) {
goal();
score_keeper('away');
} else if (ball.x > outside_pitch.side + play_area.width) {
goal();
score_keeper('home');
}
//contact with goal post
if (ball.y + ball.dimensions >= goal_post.y1 + goal_post.height - 10 && ball.unit_y > 0) {
ball.unit_y = -ball.unit_y;
} else if (ball.y <= goal_post.y1 + 10 && ball.unit_y < 0) {
ball.unit_y = -ball.unit_y;
}
}
//if the ball is headed for the goal
if (ball.x <= outside_pitch.side + 2 && ball.unit_x < 0 &&
ball.y - 5 > goal_post.y1 && ball.y + ball.dimensions + 5 < goal_post.y1 + goal_post.height) {
} else if (ball.x + ball.dimensions >= outside_pitch.side + play_area.width - 2 && ball.unit_x > 0 &&
ball.y - 5 > goal_post.y1 && ball.y + ball.dimensions + 5 < goal_post.y1 + goal_post.height) {
}
//check for ball contact with field boundary
else if (ball.x + ball.dimensions >= gamecanvas.width - outside_pitch.side - 2 && ball.unit_x > 0) {
ball.unit_x = -ball.unit_x;
} else if (ball.x <= outside_pitch.side && ball.unit_x < 0) {
ball.unit_x = -ball.unit_x;
} else if (ball.y + ball.dimensions >= gamecanvas.height - 2 && ball.unit_y > 0) {
ball.unit_y = -ball.unit_y;
} else if (ball.y <= outside_pitch.top && ball.unit_y < 0) {
ball.unit_y = -ball.unit_y;
}
}
//check for player contact with wall
if (players[i].x + players[i].radius >= gamecanvas.width - 2 && players[i].unit_x > 0) {
players[i].unit_x = -players[i].unit_x;
} else if (players[i].x - players[i].radius <= 7 && players[i].unit_x < 0) {
players[i].unit_x = -players[i].unit_x;
} else if (players[i].y + players[i].radius >= gamecanvas.height - 2 && players[i].unit_y > 0) {
players[i].unit_y = -players[i].unit_y;
} else if (players[i].y - players[i].radius <= 2 && players[i].unit_y < 0) {
players[i].unit_y = -players[i].unit_y;
}
//check for ball contact with wall
if (ball.x + ball.dimensions >= gamecanvas.width - 2 && ball.unit_x > 0) {
ball.unit_x = -ball.unit_x;
} else if (ball.x <= 2 && ball.unit_x < 0) {
ball.unit_x = -ball.unit_x;
} else if (ball.y + ball.dimensions >= gamecanvas.height - 2 && ball.unit_y > 0) {
ball.unit_y = -ball.unit_y;
} else if (ball.y <= 2 && ball.unit_y < 0) {
ball.unit_y = -ball.unit_y;
}
//check for contact with other player
for (let i = 0; i < players_number; i++) {
for (let j = i + 1; j < players_number; j++) {
if (Math.sqrt(Math.pow(players[i].x - players[j].x, 2) + Math.pow(players[i].y - players[j].y, 2)) <= players[i].radius + players[j].radius + 1) {
player_collision(players[i], players[j]);
break;
}
}
}
//check for player contact with ball
for (let i = 0; i < players_number; i++) {
if (Math.sqrt(Math.pow(players[i].x - (ball.x + ball.dimensions / 2), 2) + Math.pow(players[i].y - (ball.y + ball.dimensions / 2), 2)) < players[i].radius + (ball.dimensions / 2)) {
//create a holder because the ball's x is not exactly the midpoint
let temp = ball;
temp.x += temp.dimensions / 2;
temp.y += temp.dimensions / 2;
player_collision(players[i], temp);
temp.x -= temp.dimensions / 2;
temp.y -= temp.dimensions / 2;
ball = temp;
break;
}
}
//move the player
players[i].x += players[i].unit_x;
players[i].y += players[i].unit_y;
ball.x += ball.unit_x / ball_grease;
ball.y += ball.unit_y / ball_grease;
repeat--;
} while (repeat > 0);
}
circle_drawer();
}
//switches the turn to play between the two teams every after a set duration
let turn_taking_timeout;
function turn_taking(event) {
//if the game is being played
if (gamesession) {
if (event == 'played') {
clearTimeout(turn_taking_timeout);
if (turn == 1) {
turn = 2;
} else {
turn = 1;
}
} else if (event == 'home score') {
clearTimeout(turn_taking_timeout);
turn = 2;
} else if (event == 'away score') {
clearTimeout(turn_taking_timeout);
turn = 1;
}
turn_taking_timeout = setTimeout(function () {
if (turn == 1) {
turn = 2;
} else {
turn = 1;
}
turn_taking('timeout');
}, turn_duration);
}
}