1
1
$ ( function ( ) {
2
-
3
- // evil
4
- Array . prototype . remove = function ( from , to ) {
5
- var rest = this . slice ( ( to || from ) + 1 || this . length ) ;
6
- this . length = from < 0 ? this . length + from : from ;
7
- return this . push . apply ( this , rest ) ;
8
- } ;
9
-
10
- function getCookie ( name ) {
11
- var r = document . cookie . match ( "\\b" + name + "=([^;]*)\\b" ) ;
12
- return r ? r [ 1 ] : undefined ;
13
- }
14
-
15
- if ( ! window . console ) window . console = { } ;
16
- if ( ! window . console . log ) window . console . log = function ( ) { } ;
17
-
2
+
18
3
var event_cursor = false ;
19
4
// polling update
20
5
var updater = {
21
6
errorSleepTime : 500 ,
22
7
cursor : null ,
23
8
24
9
poll : function ( ) {
25
- var args = { "_xsrf" : getCookie ( "_xsrf" ) } ;
10
+ var args = { "_xsrf" : game . getCookie ( "_xsrf" ) } ;
26
11
if ( event_cursor )
27
12
args [ "cursor" ] = event_cursor
28
13
if ( updater . cursor ) args . cursor = updater . cursor ;
@@ -53,8 +38,8 @@ $(function() {
53
38
}
54
39
} ;
55
40
56
- jQuery . postJSON = function ( url , args , callback ) {
57
- args . _xsrf = getCookie ( "_xsrf" ) ;
41
+ game . postJSON = function ( url , args , callback ) {
42
+ args . _xsrf = game . getCookie ( "_xsrf" ) ;
58
43
$ . ajax ( {
59
44
url : url ,
60
45
data : $ . param ( args ) ,
@@ -157,7 +142,7 @@ $(function() {
157
142
}
158
143
if ( direction ) {
159
144
this . is_loading_room = true ;
160
- $ . postJSON ( "/a/change_room" , { 'direction' :direction } , function change_room ( response ) {
145
+ game . postJSON ( "/a/change_room" , { 'direction' :direction } , function change_room ( response ) {
161
146
var others_number = other_players . length - 1 ;
162
147
while ( others_number >= 0 ) {
163
148
if ( other_players [ 0 ] ) {
@@ -251,14 +236,14 @@ $(function() {
251
236
252
237
// PLAYER
253
238
254
- function player ( init_position , key , pname ) {
239
+ function Player ( init_position , key , pname ) {
255
240
this . position = [ init_position [ 0 ] , init_position [ 1 ] ] ;
256
241
this . last_sent_position = [ init_position [ 0 ] , init_position [ 1 ] ] ;
257
242
this . target_position = [ init_position [ 0 ] , init_position [ 1 ] ] ;
258
243
this . pname = pname ;
259
244
this . key = key ;
260
245
this . walking = false ;
261
- this . speed = 3 ;
246
+ this . speed = 2 ;
262
247
this . element = $ ( '<div class="player">\
263
248
<span class="text"><span class="name">' + this . pname + '</span>\
264
249
<span class="message"></span></span>\
@@ -271,16 +256,16 @@ $(function() {
271
256
this . move ( this . position ) ;
272
257
} ;
273
258
274
- player . prototype . say = function ( message ) {
259
+ Player . prototype . say = function ( message ) {
275
260
clearTimeout ( this . message_timeout ) ;
276
261
var el = this . message_element ;
277
262
this . message_element . text ( message ) ;
278
263
this . message_element . slideDown ( "slow" ) ;
279
264
var _hide_message = function ( ) { el . slideUp ( "slow" ) ; }
280
- this . message_timeout = setTimeout ( _hide_message , 10000 ) ;
265
+ this . message_timeout = setTimeout ( _hide_message , 12000 ) ;
281
266
}
282
267
283
- player . prototype . anim = function ( ) {
268
+ Player . prototype . anim = function ( ) {
284
269
if ( ! this . walking ) {
285
270
this . cycle = 1 ;
286
271
} ;
@@ -294,20 +279,20 @@ $(function() {
294
279
this . cycle = 0 ;
295
280
} ;
296
281
297
- player . prototype . move = function ( pos ) {
298
- this . element . css ( ' left' , pos [ 0 ] + 'px' ) ;
299
- this . element . css ( ' top' , pos [ 1 ] + 'px' ) ;
282
+ Player . prototype . move = function ( pos ) {
283
+ this . element [ 0 ] . style . left = pos [ 0 ] + 'px' ;
284
+ this . element [ 0 ] . style . top = pos [ 1 ] + 'px' ;
300
285
} ;
301
286
302
- player . prototype . init_position = function ( ) {
287
+ Player . prototype . init_position = function ( ) {
303
288
this . move ( this . position ) ;
304
289
} ;
305
290
306
- player . prototype . remove = function ( ) {
291
+ Player . prototype . remove = function ( ) {
307
292
this . element . remove ( ) ;
308
293
} ;
309
294
310
- player . prototype . update_target_position = function ( ) {
295
+ Player . prototype . update_target_position = function ( ) {
311
296
var vect = keyboard_vector ( ) ;
312
297
if ( vect ) {
313
298
var next_pos = [
@@ -322,7 +307,7 @@ $(function() {
322
307
} ;
323
308
} ;
324
309
325
- player . prototype . set_start_cycle = function ( vect ) {
310
+ Player . prototype . set_start_cycle = function ( vect ) {
326
311
//var angle = Math.atan(vect[0]/vect[1]);
327
312
if ( vect [ 1 ] > 0 ) {
328
313
this . start_cycle = 24 * 9 ;
@@ -338,39 +323,46 @@ $(function() {
338
323
} ;
339
324
}
340
325
341
- player . prototype . move_to_target = function ( ) {
326
+ Player . prototype . move_to_target = function ( ) {
342
327
var vect = [
343
328
this . target_position [ 0 ] - this . position [ 0 ] ,
344
329
this . target_position [ 1 ] - this . position [ 1 ]
345
330
] ;
346
331
var norm = norm_vector ( vect ) ;
347
- if ( norm <= 2 ) {
332
+ if ( norm < 1 ) {
348
333
this . position [ 0 ] = this . target_position [ 0 ] ;
349
334
this . position [ 1 ] = this . target_position [ 1 ] ;
350
335
this . move ( this . position ) ;
351
336
this . walking = false ;
337
+ this . last_time = false ;
352
338
return ;
353
- } ;
354
- if ( norm > 2 ) {
339
+ } else {
340
+ /*var d = new Date().getTime();
341
+ if(this.last_time) {
342
+ var diff = Math.min(2, (d - this.last_time) / 16.0);
343
+ } else {
344
+ var diff = 1;
345
+ }
346
+ this.last_time = d;*/
355
347
this . walking = true
356
- vect [ 0 ] = vect [ 0 ] / norm ;
357
- vect [ 1 ] = vect [ 1 ] / norm ;
348
+ vect [ 0 ] = vect [ 0 ] / norm * this . speed ;
349
+ vect [ 1 ] = vect [ 1 ] / norm * this . speed ;
358
350
this . set_start_cycle ( vect ) ;
359
- this . position [ 0 ] += parseInt ( this . speed * vect [ 0 ] ) ;
360
- this . position [ 1 ] += parseInt ( this . speed * vect [ 1 ] ) ;
351
+ this . position [ 0 ] += parseInt ( vect [ 0 ] ) ;
352
+ this . position [ 1 ] += parseInt ( vect [ 1 ] ) ;
361
353
this . move ( this . position ) ;
362
354
return ;
363
355
}
364
356
} ;
365
357
366
- player . prototype . send_position = function ( ) {
358
+ Player . prototype . send_position = function ( ) {
367
359
if ( this . last_sent_position [ 0 ] != this . target_position [ 0 ]
368
360
|| this . last_sent_position [ 1 ] != this . target_position [ 1 ] )
369
361
{
370
362
var pos = $ . toJSON ( this . target_position )
371
363
this . last_sent_position [ 0 ] = this . target_position [ 0 ] ;
372
364
this . last_sent_position [ 1 ] = this . target_position [ 1 ] ;
373
- $ . postJSON ( "/a/player/update_position" , {
365
+ game . postJSON ( "/a/player/update_position" , {
374
366
'body' :pos
375
367
} ,
376
368
function ( response ) { }
@@ -384,7 +376,7 @@ $(function() {
384
376
e . preventDefault ( ) ;
385
377
var message = $ ( '#message' ) ;
386
378
message . focus ( ) ;
387
- $ . postJSON ( "/a/message/new" , { 'body' :message . val ( ) } , function ( response ) {
379
+ game . postJSON ( "/a/message/new" , { 'body' :message . val ( ) } , function ( response ) {
388
380
me . say ( message . val ( ) ) ;
389
381
$ ( '#message' ) . val ( '' ) ;
390
382
} ) ;
@@ -404,7 +396,7 @@ $(function() {
404
396
405
397
function bootstrap ( ) {
406
398
407
- me = new player ( window . player_position , window . personnal_key , window . player_name ) ;
399
+ me = new Player ( window . player_position , window . personnal_key , window . player_name ) ;
408
400
me . init_position ( ) ;
409
401
// start polling events
410
402
updater . poll ( ) ;
@@ -419,7 +411,7 @@ $(function() {
419
411
other_players [ i ] . move_to_target ( ) ;
420
412
} ;
421
413
} ;
422
- setInterval ( _players_move , 25 ) ;
414
+ setInterval ( _players_move , 16 ) ;
423
415
424
416
var _anim = function ( ) {
425
417
if ( ! grid1 . is_loading_room ) {
@@ -442,7 +434,7 @@ $(function() {
442
434
443
435
if ( ! window . player_position ) {
444
436
var player_name = prompt ( "Choose your hero name" ) ;
445
- $ . postJSON ( "/a/player/new" , { 'body' :player_name } , function ( response ) {
437
+ game . postJSON ( "/a/player/new" , { 'body' :player_name } , function ( response ) {
446
438
response = $ . evalJSON ( response ) ;
447
439
window . personnal_key = response [ "you" ] [ "key" ] ;
448
440
window . player_position = response [ "you" ] [ "position" ] ;
@@ -474,7 +466,7 @@ $(function() {
474
466
}
475
467
var p = get_player ( item [ 'key' ] ) ;
476
468
if ( p === false ) {
477
- p = new player ( [ pos [ 0 ] , pos [ 1 ] ] , item [ 'key' ] , item [ 'name' ] ) ;
469
+ p = new Player ( [ pos [ 0 ] , pos [ 1 ] ] , item [ 'key' ] , item [ 'name' ] ) ;
478
470
other_players . push ( p ) ;
479
471
}
480
472
p . target_position = [ pos [ 0 ] , pos [ 1 ] ] ;
@@ -573,24 +565,19 @@ $(function() {
573
565
keyboard_events . keydown ( function ( e ) {
574
566
update_keyboard ( e , 1 ) ;
575
567
//reset_cycle();
576
- player . walking = true ;
568
+ me . walking = true ;
577
569
} ) ;
578
570
keyboard_events . keyup ( function ( e ) {
579
571
update_keyboard ( e , 0 ) ;
580
572
if ( ! keyboard [ "up" ] && ! keyboard [ "left" ] && ! keyboard [ "right" ] && ! keyboard [ "down" ] )
581
- player . walking = false ;
573
+ me . walking = false ;
582
574
} ) ;
583
575
584
576
$ ( '#save-map' ) . click ( function ( ) {
585
577
var content = $ . toJSON ( grid1 . blocs ) ;
586
- $ . postJSON ( "/a/save_map" , { 'content' :content } , function change_room ( response ) {
578
+ game . postJSON ( "/a/save_map" , { 'content' :content } , function change_room ( response ) {
587
579
$ ( '#save-message' ) . show ( ) . fadeOut ( "slow" ) ;
588
580
} ) ;
589
581
} )
590
582
591
-
592
- /*keyboard_events.blur(function(e) {
593
- keyboard = {'up':0, 'left':0, 'right':0, 'down':0};
594
- keyboard_events.focus();
595
- });*/
596
583
} ) ;
0 commit comments