-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.js
363 lines (325 loc) · 8.94 KB
/
client.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
"use strict";
var World = require('./world');
var Ball = require('./ball');
var Vector = require('./vector');
var Color = require('./color');
$(function() {
var colorId = Math.random();
var color = Color.niceColor(colorId);
$('.box').css({
borderTopColor: color.toString(),
borderBottomColor: color.toString()
});
$('a').css({
borderBottomColor: color.toString()
});
$('#login input, h2, a').css({
color: color.toString()
});
$('#about .box').click(function(e) {
e.stopPropagation();
});
if(location.search == "?music") (function(){
//Do music!
var clientId = "dd250c3d9ef318565e6f22e871b87fb8";
var musicUrl = "http://soundcloud.com/alex-nicholls/snakes-theme";
$.getJSON(
'http://api.soundcloud.com/resolve.json?callback=?', {
url: musicUrl,
client_id: clientId
}, function(data) {
if(data.streamable && data.stream_url) {
var music = new Audio();
music.src = data.stream_url+'?client_id='+clientId;
music.loop = true;
music.play();
}
}
);
})();
//Show and hide the about box
$('#about-link').click(function() {
$('#login')
.fadeOut()
.queue(function(next) { $('#about').fadeIn(next); })
.queue(function(next) { $('#about').one('click', next); })
.queue(function(next) { $('#about').fadeOut(next); })
.fadeIn();
return false;
});
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || window.oRequestAnimationFrame || function(callback) {
window.setTimeout(function() {
callback(Date.now());
}, 1000 / 60.0);
};
//Get canvas stuff
var canvas = $('#canvas').get(0);
var width = canvas.width, height = canvas.height;
$(window).resize(function(){
width = canvas.width = $(canvas).width();
height = canvas.height = $(canvas).height();
}).resize();
var ctx = canvas.getContext('2d');
var keycodes = {
up: 87,
down: 83,
left: 65,
right: 68
};
var universe = new World(2000, 2000);
var socket = io.connect(':'+port);
//var socket = io.connect('/');
var name;
var head;
var heads;
var viewOrigin = Vector.zero;
function nextViewOrigin() {
var x, y;
if(!head) {
x = (universe.width - width) / 2;
y = (universe.height - height) / 2;
} else {
var border = 20;
x = head.position.x - width / 2;
y = head.position.y - height / 2;
if(x < -border)
x = -border;
else if(x > border + universe.width - width)
x = border + universe.width - width;
if(universe.width + 2 * border < width)
x = (universe.width - width) / 2;
if(y < -border)
y = -border;
else if(y > border + universe.height - height)
y = border + universe.height - height;
if(universe.height + 2 * border < height)
y = (universe.height - height) / 2;
}
return new Vector(x, y);
}
var nameinput = $('#name');
if(localStorage['name'])
nameinput.val(localStorage['name']);
var isTrying = false;
var playing = false;
$('#login').hide();
socket.on('connect', function() {
socket.emit('room', gameName, function(user) {
//It knows who we are
if(user) {
name = user.name;
playing = true;
requestAnimationFrame(draw);
}
else $('#login').show();
});
});
$('#join').submit(function() {
//Stop things happening while the response is being waited for
if(!isTrying) {
isTrying = true;
var n = nameinput.val();
socket.emit('join', {
name: n,
color: colorId,
game: gameName
}, function(data) {
if(data === true) {
localStorage['name'] = name = n;
requestAnimationFrame(draw);
$('#login').fadeOut(function() {
isTrying = false;
playing = true;
});
} else if(data instanceof Object && data.error) {
alert(data.error);
isTrying = false;
}
});
}
return false;
});
socket.on('servermessage', function (str) {
$('#message').html(str);
});
socket.on('serverstop', function () {
playing = false;
});
socket.on('ping', function() {
socket.emit('ping');
});
socket.on('scores', function (scores) {
//Get the DOM elements
var scoreList = $('#scores').empty();
var scoreBar = $('.score-bar').empty();
if(scores) {
//Expand to an objects - data sent as array to save space
scores = scores.map(function(score) {
return { name: score[0], value: score[1], color: score[2] };
});
//Calculate the spacing of the bars
var unclaimed = 1000 - scores.pluck('value').reduce(function(a, b) { return a + b; }, 0);
var gap = unclaimed / (scores.length - 1);
//Keep track of leftmost bar
var left = 0;
//draw bars
scores.forEach(function(score) {
//left
$('<div />').css({
backgroundColor: score.color,
width: (score.value / 10) + '%',
left: (left / 10) + '%'
}).appendTo(scoreBar);
left += score.value + gap;
});
scores.sort(function(a, b) { return a.score - b.score; });
//draw numbers and names
scores.forEach(function(score) {
$('<li />').append(
$('<span class="name"/>').text(score.name), " ",
$('<span />').text(score.value)
).css('color', score.color).appendTo(scoreList);
});
}
});
socket.on('entityupdates', function (data) {
Object.forEach(data.e, function(edata, id) {
var p = Vector.ify(edata.p);
var c = Color.ify(edata.c);
var r = edata.r;
if(+id in universe.entities) {
var e = universe.entities[+id];
e.position = p;
e.color = c;
e.radius = r;
} else {
var b = new Ball(p, r, c);
b._id = +id;
universe.entities[+id] = b;
}
});
for(var id in universe.entities)
if(!(id in data.e))
delete universe.entities[id];
var newHeads = {};
Object.forEach(data.s, function(id, name) {
newHeads[name] = universe.entities[id];
});
head = newHeads[name];
heads = newHeads;
if(playing) socket.emit('playercontrol', target.plus(viewOrigin));
});
var target = new Vector();
if(navigator.msPointerEnabled)
$(document).on('MSPointerMove', function(e) {
// e = e.originalEvent
// for(var i in e)
// console.log("pointer", i);
var offset = $(canvas).offset();
target.set(e.pageX - offset.left, e.pageY - offset.top);
return false;
});
else
$(document).on('mousemove', function(e) {
// for(var i in e)
// console.log("pointer", i);
var offset = $(canvas).offset();
target.set(e.pageX - offset.left, e.pageY - offset.top);
return false;
}).on('touchmove', function(e) {
// console.log("touch", JSON.stringify(e));
var touch = e.touches[0];
var offset = $(canvas).offset();
target.set(touch.pageX - offset.left, touch.pageY - offset.top);
return false;
});
$(document).keydown(function() {
if(playing) $('#chat input').focus();
});
$('#chat').submit(function() {
if(playing) {
var input = $(this).find('input');
var msg = input.val();
input.val('');
socket.emit('chat', msg);
}
return false;
});
socket.on('chat', function (data) {
var name = data.n;
var color = Color.ify(data.c);
var message = data.m;
var history = $('#chat .history');
//alert(name + ":" + message);
$('<li />').text(' '+message).prepend(
$('<span />').css('color', color+"").text(name)
).appendTo('#chat .history');
while(history.children().size() > 10)
history.children().first().remove();
});
function drawArrow(x, y, angle) {
ctx.save();
ctx.translate(x, y);
ctx.rotate(angle);
ctx.beginPath();
ctx.moveTo(5, 0);
ctx.lineTo(20,-7.5);
ctx.lineTo(20, 7.5);
ctx.restore();
}
var lastt = Date.now();
function draw(t) {
//Calculate frame time
var dt = (t - lastt) / 1000.0;
ctx.clearRect(0, 0, width, height);
ctx.save();
viewOrigin = nextViewOrigin();
ctx.translate(-viewOrigin.x, -viewOrigin.y);
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "black";
ctx.fillRect(0, 0, 2000, 2000);
ctx.globalCompositeOperation = "lighter";
universe.entities.forEach(function(e) {
e.drawTo(ctx);
});
Object.forEach(heads, function(h) {
var off = {
left: h.position.x + h.radius < viewOrigin.x,
right: h.position.x - h.radius > viewOrigin.x + width,
top: h.position.y + h.radius < viewOrigin.y,
bottom: h.position.y - h.radius > viewOrigin.y + height
};
off.any = off.right || off.left || off.top || off.bottom;
if(!off.any) {
ctx.beginPath();
ctx.arc(h.position.x, h.position.y, h.radius / 2, 0, Math.PI * 2, false);
ctx.fillStyle = "white";
ctx.fill();
}
else {
if(off.left && off.top)
drawArrow(viewOrigin.x, viewOrigin.y, Math.PI / 4);
else if(off.right && off.top)
drawArrow(viewOrigin.x + width, viewOrigin.y, 3*Math.PI / 4);
else if(off.left && off.bottom)
drawArrow(viewOrigin.x, viewOrigin.y + height, -Math.PI / 4);
else if(off.right && off.bottom)
drawArrow(viewOrigin.x + width, viewOrigin.y + height, -3*Math.PI / 4);
else if(off.left)
drawArrow(viewOrigin.x, h.position.y, 0);
else if(off.right)
drawArrow(viewOrigin.x + width, h.position.y, Math.PI);
else if(off.top)
drawArrow(h.position.x, viewOrigin.y, Math.PI / 2);
else if(off.bottom)
drawArrow(h.position.x, viewOrigin.y + height, -Math.PI / 2);
ctx.fillStyle = h.color+"";
ctx.fill();
}
});
//prepare for next frame
lastt = t;
ctx.restore();
requestAnimationFrame(draw);
}
});