-
Notifications
You must be signed in to change notification settings - Fork 0
/
balls.html
411 lines (346 loc) · 12.8 KB
/
balls.html
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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Ahhh Balls!</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<style>
#content {
margin: 0 auto;
width: 960px;
text-align: center;
}
#theCanvas {
border: 5px solid black;
}
</style>
</head>
<body>
<div id="content">
<h1>Ahhh balls!</h1>
<h3>Use arrow keys to control your ball. Space to fire and Enter to respawn</h3>
<canvas id="theCanvas" width="800" height="600">Uh oh, your browser doesn't support HTML5.</canvas>
</div>
<script type="text/javascript">
function spawnBall(){
return {
x: centreX,
y: centreY,
a: 0,
v: 0,
r: Math.floor((Math.random()*15)+5),
fill: getRandomColor(),
};
}
function drawBall(ball) {
if(ball){
context.beginPath();
context.arc(ball.x, ball.y, ball.r, 0, 2 * Math.PI);
context.lineWidth = 2;
context.strokeStyle = 'black';
context.fillStyle = ball.fill;
context.stroke();
context.fill();
context.closePath();
context.beginPath();
context.moveTo(ball.x, ball.y);
var lx = ball.x + ball.r * Math.cos(ball.a);
var ly = ball.y + ball.r * Math.sin(ball.a);
context.lineTo(lx, ly);
context.stroke();
context.closePath();
}
}
function drawShot(shot){
if(shot){
context.beginPath();
context.lineWidth = 5;
context.strokeStyle = shot.colour;
context.moveTo(shot.x1, shot.y1);
context.lineTo(shot.x2, shot.y2);
context.stroke();
context.closePath();
}
}
function drawTrace() {
var traceMsg = myBall ? "X:" + Math.round(myBall.x) + " Y: " + Math.round(myBall.y) : "Respawn!";
context.beginPath();
context.fillStyle = myBall ? myBall.fill : 'red';
context.font = '14px serif';
context.fillText(traceMsg, 10, 14);
context.closePath();
}
function draw() {
// Clear the canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// Draw my ball
drawBall(myBall);
$.each(enemyBalls, function(k, v){
if(k != socketId)
drawBall(v);
});
// Draw my shots
$.each(myShots, function(k, v){
if(k != socketId)
drawShot(v);
});
// Draw enemy shots
$.each(enemyShots, function(k, shots){
if(k != socketId){
$.each(shots, function(k, shot){
drawShot(shot);
});
}
});
// Draw trace output
drawTrace();
}
function update() {
if (keysPressed.enter)
myBall = spawnBall();
if(myBall) {
var oldX = myBall.x;
var oldY = myBall.y;
var oldA = myBall.a;
if (keysPressed.up)
myBall.v += constants.acceleration;
if (keysPressed.down)
myBall.v -= constants.acceleration;
if (keysPressed.left)
myBall.a -= constants.turnRate;
if (keysPressed.right)
myBall.a += constants.turnRate;
if (keysPressed.space && frameCount > 15) {
// Create a shot
var newShot = {};
newShot.x1 = myBall.x;
newShot.y1 = myBall.y;
newShot.x2 = myBall.x + constants.shotLength * Math.cos(myBall.a);
newShot.y2 = myBall.y + constants.shotLength * Math.sin(myBall.a);
newShot.a = myBall.a;
newShot.colour = myBall.fill;
myShots.push(newShot);
frameCount = 0;
}
// Rudimentary wall collision detection
if (((myBall.x - myBall.r) <= 0) || ((myBall.x + myBall.r) >= canvas.width)) {
myBall.a = Math.PI - myBall.a;
}
if(((myBall.y - myBall.r) <= 0) || ((myBall.y + myBall.r) >= canvas.height)){
myBall.a = -myBall.a;
}
// Test for collision with other players
/*$.each(enemyBalls, function(k, ball){
if(k != socketId && ball){
if(intersects(myBall, ball)){
if(myBall.r < ball.r)
myBall = undefined;
else
myBall.r += ball.r;
}
}
});*/
// Hack to ensure we don't infinitely reduce myBall.v
if(Math.abs(myBall.v) > 0.1)
myBall.v -= constants.friction * myBall.v;
else
myBall.v = 0;
var dx = myBall.v * Math.cos(myBall.a);
var dy = myBall.v * Math.sin(myBall.a);
myBall.x += dx;
myBall.y += dy;
$.each(myShots, function(k, myShot){
if(myShot){
if(myShot.x2 < 0 || myShot.x2 > canvas.width || myShot.y2 < 0 || myShot.y2 > canvas.height){
myShots.splice(k, 1);
}
else{
var dx = constants.shotSpeed * Math.cos(myShot.a);
var dy = constants.shotSpeed * Math.sin(myShot.a);
myShot.x1 += dx;
myShot.x2 += dx;
myShot.y1 += dy;
myShot.y2 += dy;
}
}
});
socket.emit('updateshots', myShots);
if(oldX != myBall.x || oldY != myBall.y || oldA != myBall.a){
// Only send new position if it has changed
socket.emit('updateball', myBall);
}
// Check for shots hitting
$.each(enemyShots, function(k, shots){
if(k != socketId && shots){
$.each(shots, function(k, shot){
if(shotHit(shot, myBall)){
console.log('Shot hit!');
myBall.r -= constants.shotCost * myBall.r;
if(myBall.r < 5){
myBall = undefined;
}
socket.emit('updateball', myBall);
}
});
}
});
}
}
function shotHit(shot, ball){
var a = shot.x2 - shot.x1;
var b = shot.y2 - shot.y1;
var c = ball.x - shot.x1;
var d = ball.y - shot.y1;
var r = ball.r;
var startInside = false;
var endInside = false;
var middleInside = false;
if ((d*a - c*b)*(d*a - c*b) <= r*r*(a*a + b*b)) {
// Collision is possible
if (c*c + d*d <= r*r) {
// Line segment start point is inside the circle
return true;
}
if ((a-c)*(a-c) + (b-d)*(b-d) <= r*r) {
// Line segment end point is inside the circle
return true;
}
if (!startInside && !endInside && c*a + d*b >= 0 && c*a + d*b <= a*a + b*b) {
// Middle section only
return true;
}
}
return false;
}
function intersects(ball1, ball2){
var distance = Math.sqrt( (ball1.y - ball2.y)^2 + (ball1.x - ball2.x)^2 );
console.log('Distance: ' + distance);
return distance < (ball1.r + ball2.r) || Math.abs(ball1.r - ball2.r) > distance;
}
function dot(x1,y1,x2,y2){
return (x1 * x2) + (y1 * y2);
}
function animate() {
frameCount++;
requestAnimFrame(animate);
update();
draw();
}
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.round(Math.random() * 15)];
}
return color;
}
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
</script>
<script type="text/javascript">
var socket = io.connect();
var socketId;
var canvas = document.getElementById('theCanvas');
var context = canvas.getContext('2d');
var centreX = canvas.width / 2;
var centreY = canvas.height / 2;
var keysPressed = {
space: false,
up: false,
down: false,
left: false,
right: false
};
var constants = {
turnRate: 0.1,
acceleration: 2,
friction: 0.05,
shotSpeed: 10,
shotLength: 15,
shotCost: 0.1
};
var myBall = spawnBall();
var myShots = [];
var enemyBalls = {};
var enemyShots = {};
var frameCount = 0;
// Kick off the animation and game loop
animate();
socket.on('connected', function(id){
socketId = id;
socket.emit('updateball', myBall);
});
socket.on('onupdateballs', function(balls){
enemyBalls = balls;
});
socket.on('onupdateshots', function(shots){
enemyShots = shots;
});
$(document).ready(function () {
$(document.body).keydown(function (e) {
switch (e.keyCode) {
// ENTER
case 13:
keysPressed.enter = true;
break;
// SPACEBAR
case 32:
keysPressed.space = true;
break;
// LEFT ARROW
case 37:
keysPressed.left = true;
break;
// RIGHT ARROW
case 39:
keysPressed.right = true;
break;
// UP ARROW
case 38:
keysPressed.up = true;
break;
// DOWN ARROW
case 40:
keysPressed.down = true;
break;
}
});
$(document.body).keyup(function (e) {
switch (e.keyCode) {
// ENTER
case 13:
keysPressed.enter = false;
break;
// SPACEBAR
case 32:
keysPressed.space = false;
break;
// LEFT ARROW
case 37:
keysPressed.left = false;
break;
// RIGHT ARROW
case 39:
keysPressed.right = false;
break;
// UP ARROW
case 38:
keysPressed.up = false;
break;
// DOWN ARROW
case 40:
keysPressed.down = false;
break;
}
});
});
</script>
</body>
</html>