@@ -50,6 +50,7 @@ package {
50
50
public var tile_spritesheet: Spritesheet = new oddball_tile();
51
51
public var spriteId: int = int (Math . random()* 272 ); // 273 sprites in oddball_char
52
52
public var playerName: String = "guest" ;
53
+ public var myCreatureId: String = "" ;
53
54
public var playerStyle: Object = char_spritesheet. makeStyle();
54
55
public var playerIconStyle: Object = char_spritesheet. makeStyle();
55
56
public var playerBitmap: Bitmap = new Bitmap (new BitmapData (2 * 2 + 8 * 3 , 2 * 2 + 8 * 3 , true , 0x00000000 ));
@@ -67,13 +68,27 @@ package {
67
68
// Map objects:
68
69
public var items: Object = {}; // {loc.toString(): {sprite: obj:}}
69
70
public var creatures: Object = {}; // {obj id: clientId: {sprite: bitmap: obj:}}
70
- public var myCreatureId : String = "" ;
71
+ private var mapBlocks : Object = {} ; // {block_id: Bitmap object}
71
72
72
73
public var colorMap: Array = [];
73
74
public var client: Client = new Client();
74
75
public var pingTime: TextField = new TextField ();
75
76
public var inputField: TextField = new TextField ();
76
77
public var outputMessages: OutputMessageBox = new OutputMessageBox(400 , 200 );
78
+
79
+ // Server message handlers
80
+ private var handlers: Object = {
81
+ 'server_identify' : handle_server_identify,
82
+ 'move_ok' : handle_move_ok,
83
+ 'map_tiles' : handle_map_tiles,
84
+ 'item_ins' : handle_item_ins,
85
+ 'item_del' : handle_item_del,
86
+ 'creature_ins' : handle_creature_ins,
87
+ 'creature_del' : handle_creature_del,
88
+ 'creature_move' : handle_creature_move,
89
+ 'messages' : handle_messages,
90
+ 'handle_pong' : handle_pong
91
+ };
77
92
78
93
public function gameclient () {
79
94
stage . scaleMode = 'noScale' ;
@@ -105,7 +120,7 @@ package {
105
120
});
106
121
107
122
client . addEventListener (ServerMessageEvent. SERVER_MESSAGE , function (e: ServerMessageEvent): void {
108
- handleMessage (e. message , e. binary);
123
+ handlers [ e . message . type ] (e. message , e. binary);
109
124
});
110
125
client . addEventListener (Event . CONNECT , function (e: Event ): void {
111
126
// TODO: remove 'Connecting' status and use activate/deactivate
@@ -397,104 +412,134 @@ package {
397
412
}
398
413
399
414
400
- private var mapBlocks : Object = {} ;
401
- public function handleMessage ( message : Object , binaryPayload : ByteArray ): void {
402
- var bitmap : Bitmap ;
415
+ private function handle_server_identify ( message :Object , binaryPayload : ByteArray ): void {
416
+ myCreatureId = message . id ;
417
+ }
403
418
404
- if (message . type == 'server_identify' ) {
405
- myCreatureId = message . id ;
406
- } else if (message . type == 'move_ok' ) {
407
- moving = false ;
408
- if (animationState) {
409
- animationState. endLocation = message . loc;
410
- } else {
411
- // If we don't have an animation state, but received
412
- // move_ok, we'll just jump to the new location.
413
- Debug. trace ("MOVE_OK with no animation in progress." );
414
- }
415
- location = message . loc;
416
-
417
- // Request map tiles corresponding to our new location. Only
418
- // request the map tiles if we don't already have that block,
419
- // or if that block is already requested.
420
- if (message . simblocks_ins != null ) {
421
- for each (var simblock_id: Object in message . simblocks_ins) {
422
- var simblock_hash: String = simblock_id. toString ();
423
- if (mapBlocks[ simblock_hash] == null ) {
424
- mapBlocks[ simblock_hash] = {}; // Pending
425
- client . sendMessage({type : 'map_tiles' , simblock_id: simblock_id});
426
- }
419
+ private function handle_move_ok (message :Object , binaryPayload :ByteArray ):void {
420
+ var simblock_id: Object , simblock_hash: String ;
421
+
422
+ moving = false ;
423
+ if (animationState) {
424
+ animationState. endLocation = message . loc;
425
+ } else {
426
+ // If we don't have an animation state, but received
427
+ // move_ok, we'll just jump to the new location.
428
+ Debug. trace ("MOVE_OK with no animation in progress." );
429
+ }
430
+ location = message . loc;
431
+
432
+ // Request map tiles corresponding to our new location. Only
433
+ // request the map tiles if we don't already have that block,
434
+ // or if that block is already requested.
435
+ if (message . simblocks_ins != null ) {
436
+ for each (simblock_id in message . simblocks_ins) {
437
+ simblock_hash = simblock_id. toString ();
438
+ if (mapBlocks[ simblock_hash] == null ) {
439
+ mapBlocks[ simblock_hash] = {}; // Pending
440
+ client . sendMessage({type : 'map_tiles' , simblock_id: simblock_id});
427
441
}
428
- }
429
- // TODO: clear map bitmap for blocks in simblocks_del
430
-
431
- // HACK: if a movement was delayed because we were already
432
- // moving, trigger the new movement
433
- if (_keyQueue ) onKeyDown(_keyQueue , true );
434
- } else if (message . type == 'map_tiles' ) {
435
- var i: int , tileId: int , x : int , y : int ;
436
- if (colorMap. length == 0 ) buildColorMap();
437
- i = 0 ;
438
- var bmp: BitmapData = new BitmapData (message . right - message . left , message . bottom - message . top , false );
439
- for (x = message . left ; x < message . right ; x ++ ) {
440
- for (y = message . top ; y < message . bottom ; y ++ ) {
441
- tileId = binaryPayload[ i++];
442
- bmp. setPixel (x - message . left , y - message . top , colorMap[ tileId] );
443
442
}
443
+ }
444
+ // TODO: clear map bitmap for blocks in simblocks_del
445
+
446
+ // HACK: if a movement was delayed because we were already
447
+ // moving, trigger the new movement
448
+ if (_keyQueue ) onKeyDown(_keyQueue , true );
449
+ }
450
+
451
+ private function handle_map_tiles (message :Object , binaryPayload :ByteArray ):void {
452
+ var i: int , tileId: int , x : int , y : int ;
453
+ var bmp: BitmapData , bitmap: Bitmap ;
454
+ var simblock_hash: String ;
455
+
456
+ if (colorMap. length == 0 ) buildColorMap();
457
+ i = 0 ;
458
+ bmp = new BitmapData (message . right - message . left , message . bottom - message . top , false );
459
+ for (x = message . left ; x < message . right ; x ++ ) {
460
+ for (y = message . top ; y < message . bottom ; y ++ ) {
461
+ tileId = binaryPayload[ i++];
462
+ bmp. setPixel (x - message . left , y - message . top , colorMap[ tileId] );
444
463
}
445
- bmp. lock ();
464
+ }
465
+ bmp. lock ();
446
466
447
- bitmap = new Bitmap (bmp);
448
- bitmap. scaleX = bitmap. scaleY = mapScale;
449
- bitmap. x = mapScale * message . left ;
450
- bitmap. y = mapScale * message . top ;
467
+ bitmap = new Bitmap (bmp);
468
+ bitmap. scaleX = bitmap. scaleY = mapScale;
469
+ bitmap. x = mapScale * message . left ;
470
+ bitmap. y = mapScale * message . top ;
451
471
452
- simblock_hash = message . simblock_id. toString ();
453
- mapBlocks[ simblock_hash] . bitmap = bitmap;
454
- terrainLayer. addChild (bitmap);
455
- } else if (message . type == 'item_ins' ) {
456
- bitmap = new Bitmap (new BitmapData (playerBitmap. width , playerBitmap. height , true , 0x00000000 ));
457
- tile_spritesheet. drawToBitmap(message . obj . sprite_id, bitmap. bitmapData , playerStyle);
458
- bitmap. x = mapScale * message . obj . loc[ 0 ] - playerStyle. padding ;
459
- bitmap. y = mapScale * message . obj . loc[ 1 ] - playerStyle. padding ;
460
- itemLayer. addChild (bitmap);
461
-
462
- var loc: String = message . obj . loc. toString ();
463
- if (items [ loc] != null ) Debug. trace ("ERROR: ins item, already exists at " , loc);
464
- items [ loc] = {sprite: bitmap, obj : message . obj };
465
- } else if (message . type == 'item_del' ) {
466
- loc = message . obj . loc. toString ();
467
- if (items [ loc] == null ) Debug. trace ("ERROR: del item, none at " , loc);
468
- itemLayer. removeChild (items [ loc] . sprite);
469
- delete items [ loc];
470
- } else if (message . type == 'creature_ins' ) {
471
- bitmap = new Bitmap (new BitmapData (playerBitmap. width , playerBitmap. height , true , 0x00000000 ));
472
- char_spritesheet. drawToBitmap(message . obj . sprite_id, bitmap. bitmapData , playerStyle);
473
- bitmap. x = mapScale * message . obj . loc[ 0 ] - playerStyle. padding ;
474
- bitmap. y = mapScale * message . obj . loc[ 1 ] - playerStyle. padding ;
475
- characterLayer. addChild (bitmap);
476
- if (message . obj . id == myCreatureId) bitmap. visible = false ; // it's me!
477
- if (creatures[ message . obj . id ] != null ) Debug. trace ("ERROR: ins creature, already exists at " , message . obj . id );
478
- creatures[ message . obj . id ] = {sprite: bitmap, obj : message . obj };
479
- } else if (message . type == 'creature_del' ) {
480
- if (creatures[ message . obj . id ] == null ) Debug. trace ("ERROR: del creature, none at " , message . obj . id );
481
- characterLayer. removeChild (creatures[ message . obj . id ] . sprite);
482
- delete creatures[ message . obj . id ];
483
- } else if (message . type == 'creature_move' ) {
484
- if (creatures[ message . obj . id ] == null ) Debug. trace ("ERROR: move creature, none at " , message . obj . id );
485
- bitmap = creatures[ message . obj . id ] . sprite;
486
- bitmap. x = mapScale * message . obj . loc[ 0 ] - playerStyle. padding ;
487
- bitmap. y = mapScale * message . obj . loc[ 1 ] - playerStyle. padding ;
488
- } else if (message . type == 'messages' ) {
489
- for each (var chat: Object in message . messages) {
490
- var iconSize: Number = 2 * playerIconStyle. padding + 8 * playerIconStyle. scale ;
491
- var icon : Bitmap = new Bitmap (new BitmapData (iconSize, iconSize, true , 0xffff00ff ));
492
- char_spritesheet. drawToBitmap(chat. sprite_id, icon . bitmapData , playerIconStyle);
493
- outputMessages. addChat(icon , chat. from, chat. systemtext, chat. usertext);
472
+ simblock_hash = message . simblock_id. toString ();
473
+ mapBlocks[ simblock_hash] . bitmap = bitmap;
474
+ terrainLayer. addChild (bitmap);
475
+ }
476
+
477
+ private function handle_item_ins (message :Object , _ :ByteArray ):void {
478
+ var bitmap: Bitmap ;
479
+ var loc: String ;
480
+
481
+ bitmap = new Bitmap (new BitmapData (playerBitmap. width , playerBitmap. height , true , 0x00000000 ));
482
+ tile_spritesheet. drawToBitmap(message . obj . sprite_id, bitmap. bitmapData , playerStyle);
483
+ bitmap. x = mapScale * message . obj . loc[ 0 ] - playerStyle. padding ;
484
+ bitmap. y = mapScale * message . obj . loc[ 1 ] - playerStyle. padding ;
485
+ itemLayer. addChild (bitmap);
486
+
487
+ loc = message . obj . loc. toString ();
488
+ if (items [ loc] != null ) Debug. trace ("ERROR: ins item, already exists at " , loc);
489
+ items [ loc] = {sprite: bitmap, obj : message . obj };
490
+ }
491
+
492
+ private function handle_item_del (message :Object , _ :ByteArray ):void {
493
+ var loc: String ;
494
+
495
+ loc = message . obj . loc. toString ();
496
+ if (items [ loc] == null ) Debug. trace ("ERROR: del item, none at " , loc);
497
+ itemLayer. removeChild (items [ loc] . sprite);
498
+ delete items [ loc];
499
+ }
500
+
501
+ private function handle_creature_ins (message :Object , _ :ByteArray ):void {
502
+ var bitmap: Bitmap ;
503
+
504
+ bitmap = new Bitmap (new BitmapData (playerBitmap. width , playerBitmap. height , true , 0x00000000 ));
505
+ char_spritesheet. drawToBitmap(message . obj . sprite_id, bitmap. bitmapData , playerStyle);
506
+ bitmap. x = mapScale * message . obj . loc[ 0 ] - playerStyle. padding ;
507
+ bitmap. y = mapScale * message . obj . loc[ 1 ] - playerStyle. padding ;
508
+ characterLayer. addChild (bitmap);
509
+ if (message . obj . id == myCreatureId) bitmap. visible = false ; // it's me!
510
+ if (creatures[ message . obj . id ] != null ) Debug. trace ("ERROR: ins creature, already exists at " , message . obj . id );
511
+ creatures[ message . obj . id ] = {sprite: bitmap, obj : message . obj };
512
+ }
513
+
514
+ private function handle_creature_del (message :Object , _ :ByteArray ):void {
515
+ if (creatures[ message . obj . id ] == null ) Debug. trace ("ERROR: del creature, none at " , message . obj . id );
516
+ characterLayer. removeChild (creatures[ message . obj . id ] . sprite);
517
+ delete creatures[ message . obj . id ];
518
+ }
519
+
520
+ private function handle_creature_move (message :Object , _ :ByteArray ):void {
521
+ var bitmap: Bitmap ;
522
+
523
+ if (creatures[ message . obj . id ] == null ) Debug. trace ("ERROR: move creature, none at " , message . obj . id );
524
+ bitmap = creatures[ message . obj . id ] . sprite;
525
+ bitmap. x = mapScale * message . obj . loc[ 0 ] - playerStyle. padding ;
526
+ bitmap. y = mapScale * message . obj . loc[ 1 ] - playerStyle. padding ;
527
+ }
528
+
529
+ private function handle_messages (message :Object , _ :ByteArray ):void {
530
+ var chat: Object , iconSize: Number , icon : Bitmap ;
531
+
532
+ for each (chat in message . messages) {
533
+ iconSize = 2 * playerIconStyle. padding + 8 * playerIconStyle. scale ;
534
+ icon = new Bitmap (new BitmapData (iconSize, iconSize, true , 0xffff00ff ));
535
+ char_spritesheet. drawToBitmap(chat. sprite_id, icon . bitmapData , playerIconStyle);
536
+ outputMessages. addChat(icon , chat. from, chat. systemtext, chat. usertext);
494
537
}
495
- } else if (message . type == 'pong' ) {
496
- pingTime. text = "ping time: " + (getTimer() - message . timestamp ) + "ms" + " recv: " + client . _bytesPerSecond + " bytes/second" ;
497
- }
538
+ }
539
+
540
+ private function handle_pong (message :Object , _ :ByteArray ):void {
541
+ pingTime. text = ("ping time: " + (getTimer() - message . timestamp ) + "ms"
542
+ + " recv: " + client . _bytesPerSecond + " bytes/second" );
498
543
}
499
544
500
545
0 commit comments