1
1
$ ( function ( ) {
2
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
+
3
10
function getCookie ( name ) {
4
11
var r = document . cookie . match ( "\\b" + name + "=([^;]*)\\b" ) ;
5
12
return r ? r [ 1 ] : undefined ;
@@ -8,7 +15,6 @@ $(function() {
8
15
if ( ! window . console ) window . console = { } ;
9
16
if ( ! window . console . log ) window . console . log = function ( ) { } ;
10
17
11
-
12
18
var event_cursor = false ;
13
19
// polling update
14
20
var updater = {
@@ -80,6 +86,7 @@ $(function() {
80
86
this . width = w ;
81
87
this . height = h ;
82
88
this . blocs = [ ] ;
89
+ this . dom_blocs = [ ] ;
83
90
this . choosen_bloc = [ 0 , 0 ] ;
84
91
this . is_loading_room = false ;
85
92
this . forbidden = [
@@ -93,32 +100,35 @@ $(function() {
93
100
var top_offest = 0 ;
94
101
var left_offset = 0 ;
95
102
for ( var i = 0 ; i < this . height ; i ++ ) {
103
+ var dom_line = [ ] ;
96
104
var line = [ ] ;
97
105
var top_offest = i * 16 ;
98
106
for ( var j = 0 ; j < this . width ; j ++ ) {
99
107
left_offset = j * 16 ;
100
- var bloc = $ ( '<div class="bloc" id="bloc-' + i
108
+ var dom_bloc = $ ( '<div class="bloc" id="bloc-' + i
101
109
+ '-' + j + '" style="top:' + top_offest
102
110
+ 'px;left:' + left_offset + 'px;"></div>' ) ;
103
- map . append ( bloc ) ;
104
- line . push ( [ bloc , false ] ) ;
111
+ map . append ( dom_bloc ) ;
112
+ dom_line . push ( dom_bloc ) ;
113
+ line . push ( false ) ;
105
114
} ;
115
+ this . dom_blocs . push ( dom_line ) ;
106
116
this . blocs . push ( line ) ;
107
117
} ;
108
118
this . load_new_map ( blocs_init ) ;
109
119
} ;
110
120
111
121
grid . prototype . get_bloc = function ( indexes ) {
112
- return this . blocs [ indexes [ 1 ] ] [ indexes [ 0 ] ] [ 1 ] ;
122
+ return this . blocs [ indexes [ 1 ] ] [ indexes [ 0 ] ] ;
113
123
} ;
114
124
115
- grid . prototype . paint_bloc = function ( bloc ) {
125
+ grid . prototype . paint_bloc = function ( dom_bloc ) {
116
126
//var bloc = $(e.target);
117
- if ( bloc . hasClass ( 'bloc' ) ) {
118
- var b_string = bloc . attr ( 'id' ) . split ( '-' ) ;
127
+ if ( dom_bloc . hasClass ( 'bloc' ) ) {
128
+ var b_string = dom_bloc . attr ( 'id' ) . split ( '-' ) ;
119
129
var tile_pos_css = ( - this . choosen_bloc [ 0 ] * 17 - 1 ) + 'px ' + ( - this . choosen_bloc [ 1 ] * 17 - 1 ) + 'px' ;
120
- this . blocs [ parseInt ( b_string [ 1 ] ) ] [ parseInt ( b_string [ 2 ] ) ] [ 1 ] = this . choosen_bloc ;
121
- bloc . css ( 'background-position' , tile_pos_css ) ;
130
+ this . blocs [ parseInt ( b_string [ 1 ] ) ] [ parseInt ( b_string [ 2 ] ) ] = this . choosen_bloc ;
131
+ dom_bloc . css ( 'background-position' , tile_pos_css ) ;
122
132
} ;
123
133
} ;
124
134
@@ -153,17 +163,18 @@ $(function() {
153
163
}
154
164
if ( direction ) {
155
165
this . is_loading_room = true ;
156
- $ . postJSON ( "/a/change_room" , { 'direction' :direction } , function ( response ) {
157
- for ( var i = 0 ; i < other_players . length ; i ++ ) {
158
- if ( other_players [ i ] ) {
159
- other_players [ i ] . remove ( ) ;
160
- delete other_players [ i ] ;
161
- }
166
+ $ . postJSON ( "/a/change_room" , { 'direction' :direction } , function change_room ( response ) {
167
+ var others_number = other_players . length - 1 ;
168
+ while ( others_number >= 0 ) {
169
+ if ( other_players [ 0 ] ) {
170
+ other_players [ 0 ] . remove ( ) ;
171
+ } ;
172
+ other_players . remove ( 0 ) ;
173
+ others_number = others_number - 1 ;
162
174
} ;
163
175
other_players = [ ] ;
164
176
json = $ . evalJSON ( response ) ;
165
177
var new_room = json [ "change_room" ] ;
166
- other_players = [ ] ;
167
178
var map_content = new_room [ "content" ] ;
168
179
if ( map_content )
169
180
map_content = $ . evalJSON ( map_content ) ;
@@ -181,7 +192,9 @@ $(function() {
181
192
for ( var i = 0 ; i < json [ "events" ] . length ; i ++ ) {
182
193
handle_event ( json [ "events" ] [ i ] ) ;
183
194
} ;
184
- $ ( '#room_data' ) . text ( new_room [ 'x' ] + ', ' + new_room [ 'y' ] ) ;
195
+ $ ( '#room_x' ) . text ( new_room [ 'x' ] ) ;
196
+ $ ( '#room_y' ) . text ( new_room [ 'y' ] ) ;
197
+ $ ( '#room_name' ) . text ( new_room [ 'name' ] ) ;
185
198
grid1 . is_loading_room = false ;
186
199
} ) ;
187
200
return ;
@@ -192,13 +205,14 @@ $(function() {
192
205
grid . prototype . load_new_map = function ( blocs_init ) {
193
206
for ( var i = 0 ; i < this . height ; i ++ ) {
194
207
for ( var j = 0 ; j < this . width ; j ++ ) {
195
- var bloc = this . blocs [ i ] [ j ] ;
208
+ var dom_bloc = this . dom_blocs [ i ] [ j ] ;
196
209
if ( blocs_init )
197
- bloc [ 1 ] = blocs_init [ i ] [ j ] ;
210
+ var bloc = blocs_init [ i ] [ j ] ;
198
211
else
199
- bloc [ 1 ] = [ 1 , 1 ] ;
200
- var tile_pos_css = ( - bloc [ 1 ] [ 0 ] * 17 - 1 ) + 'px ' + ( - bloc [ 1 ] [ 1 ] * 17 - 1 ) + 'px' ;
201
- bloc [ 0 ] . css ( 'background-position' , tile_pos_css ) ;
212
+ var bloc = [ 1 , 1 ] ;
213
+ this . blocs [ i ] [ j ] = bloc ;
214
+ var tile_pos_css = ( - bloc [ 0 ] * 17 - 1 ) + 'px ' + ( - bloc [ 1 ] * 17 - 1 ) + 'px' ;
215
+ dom_bloc . css ( 'background-position' , tile_pos_css ) ;
202
216
} ;
203
217
} ;
204
218
} ;
@@ -232,7 +246,7 @@ $(function() {
232
246
233
247
map . mouseup ( function ( e ) {
234
248
mouseDown = false ;
235
- $ ( '#grid-serialized' ) . val ( $ . toJSON ( grid ) ) ;
249
+ // $('#grid-serialized').val($.toJSON(grid));
236
250
} ) ;
237
251
238
252
map . mousemove ( function ( e ) {
@@ -307,6 +321,7 @@ $(function() {
307
321
this . target_position [ 0 ] + parseInt ( this . speed * vect [ 0 ] ) ,
308
322
this . target_position [ 1 ] + parseInt ( this . speed * vect [ 1 ] )
309
323
] ;
324
+
310
325
var bloc = grid1 . bloc_from_player_pos ( next_pos ) ;
311
326
if ( bloc && grid1 . is_bloc_walkable ( bloc ) ) {
312
327
this . target_position = next_pos ;
@@ -416,18 +431,23 @@ $(function() {
416
431
} ;
417
432
setInterval ( _players_move , 25 ) ;
418
433
419
- var _anim = function ( ) {
420
- me . anim ( ) ;
421
- for ( var i = 0 ; i < other_players . length ; i ++ ) {
422
- if ( other_players [ i ] )
423
- other_players [ i ] . anim ( ) ;
434
+ var _anim = function ( ) {
435
+ if ( ! grid1 . is_loading_room ) {
436
+ me . anim ( ) ;
437
+ for ( var i = 0 ; i < other_players . length ; i ++ ) {
438
+ if ( other_players [ i ] )
439
+ other_players [ i ] . anim ( ) ;
440
+ } ;
424
441
} ;
425
442
} ;
426
443
me . anim_interval = setInterval ( _anim , 120 ) ;
427
444
428
445
// send the new position to the server
429
- var _player_send = function ( ) { me . send_position ( ) ; }
430
- setInterval ( _player_send , 1000 ) ;
446
+ var _player_send_position = function ( ) {
447
+ if ( ! grid1 . is_loading_room )
448
+ me . send_position ( ) ;
449
+ } ;
450
+ setInterval ( _player_send_position , 1000 ) ;
431
451
432
452
$ . receive_data = function ( json ) {
433
453
for ( var i = 0 ; i < json . length ; i ++ ) {
@@ -471,7 +491,7 @@ $(function() {
471
491
for ( var i = 0 ; i < other_players . length ; i ++ ) {
472
492
if ( other_players [ i ] && other_players [ i ] . key == item [ 'key' ] ) {
473
493
other_players [ i ] . remove ( ) ;
474
- delete other_players [ i ] ;
494
+ other_players . remove ( i ) ;
475
495
} ;
476
496
} ;
477
497
var p = get_player ( item [ 'key' ] ) ;
@@ -547,22 +567,30 @@ $(function() {
547
567
} ;
548
568
549
569
var keyboard_events = $ ( '#message' )
550
- /* $('body').click(function(){
570
+ $ ( 'body' ) . click ( function ( ) {
551
571
keyboard_events . focus ( ) ;
552
- });*/
572
+ } ) ;
553
573
keyboard_events . focus ( ) ;
554
574
555
575
keyboard_events . keydown ( function ( e ) {
556
576
update_keyboard ( e , 1 ) ;
557
577
//reset_cycle();
558
578
player . walking = true ;
559
579
} ) ;
560
- keyboard_events . keyup ( function ( e ) {
580
+ keyboard_events . keyup ( function ( e ) {
561
581
update_keyboard ( e , 0 ) ;
562
- //reset_cycle();
563
582
if ( ! keyboard [ "up" ] && ! keyboard [ "left" ] && ! keyboard [ "right" ] && ! keyboard [ "down" ] )
564
583
player . walking = false ;
565
584
} ) ;
585
+
586
+ $ ( '#save-map' ) . click ( function ( ) {
587
+ var content = $ . toJSON ( grid1 . blocs ) ;
588
+ $ . postJSON ( "/a/save_map" , { 'content' :content } , function change_room ( response ) {
589
+ $ ( '#save-message' ) . show ( ) . fadeOut ( "slow" ) ;
590
+ } ) ;
591
+ } )
592
+
593
+
566
594
/*keyboard_events.blur(function(e) {
567
595
keyboard = {'up':0, 'left':0, 'right':0, 'down':0};
568
596
keyboard_events.focus();
0 commit comments