Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package States
  2. {
  3.     import flash.text.TextField;
  4.     import flash.text.TextFormat;
  5.     import flash.ui.Mouse;
  6.     import org.flixel.*;
  7.     import playerio.*;
  8.     import Utility.Registry;
  9.  
  10.     public class MenuState extends FlxState
  11.     {
  12.         private var bumb2:FlxText;
  13.         private var bumb:TextField = new TextField();
  14.         private var time:Number = 0;
  15.         private var t:FlxText;
  16.         private var readyButton:FlxButton;
  17.         private var isInRoom:Boolean = false;
  18.         private var roomName:String = "";
  19.         override public function create():void
  20.         {  
  21.             FlxG.framerate = 60;
  22.             FlxG.flashFramerate = 60;
  23.             bumb.width = 200;
  24.             bumb.embedFonts = true;
  25.             bumb.text = "Type a Room Name"
  26.             bumb.selectable = true;
  27.             bumb.type = 'input';
  28.             bumb.textColor = 0xFFFFFFFF;
  29.             bumb.setTextFormat(new TextFormat("system", 10, 0xffffff, null, null, null, null, null, "center"));
  30.             bumb.backgroundColor = 0x000000;
  31.             bumb.border = true;
  32.             bumb.antiAliasType = 'none';
  33.             bumb.borderColor = 0xFFFFFF
  34.             bumb.height = 15;
  35.             bumb.x = FlxG.width / 2 - 100;
  36.             bumb.y = 250;
  37.             FlxG.stage.addChild(bumb);
  38.            
  39.             bumb2 = new FlxText(FlxG.width / 2 - 100, 30, 200, "Room Listing");
  40.             bumb2.color = 0xFFFFFFFF
  41.             bumb2.size = 12;
  42.             bumb2.alignment = 'center';
  43.             bumb2.height = 200;
  44.             add(bumb2);
  45.            
  46.             FlxG.bgColor = 0xFF333333;
  47.            
  48.             //t = new FlxText(0,FlxG.height/2-40,FlxG.width,"Breakout");
  49.             //t.size = 32;
  50.             //t.alignment = "center";
  51.             //add(t);
  52.             t = new FlxText(FlxG.width/2-200,FlxG.height-30,400,"Connecting to Server...");
  53.             t.size = 12;
  54.             t.alignment = "center";
  55.             add(t);
  56.            
  57.             readyButton = new FlxButton(310, 250, "Ready", readyUp);
  58.             readyButton.visible = false;
  59.             add(readyButton);
  60.            
  61.             //The Flixel mouse cursor looks awfully laggy with the framerate
  62.             //so low, so we'll hide it away.
  63.             FlxG.mouse.show();
  64.            
  65.             PlayerIO.connect(
  66.                 FlxG.stage,                             //Referance to stage
  67.                 "breakout-zwe2bywuqelkoperhwzg",    //Game id (Get your own at playerio.com)
  68.                 "public",                           //Connection id, default is public
  69.                 "GuestUser",                        //Username
  70.                 "",                                 //User auth. Can be left blank if authentication is disabled on connection
  71.                 null,                               //Current PartnerPay partner.
  72.                 handleConnect,                      //Function executed on successful connect
  73.                 handleError                         //Function executed if we recive an error
  74.             );
  75.         }
  76.        
  77.         private function handleConnect(client:Client):void{
  78.             FlxG.log("Sucessfully connected to player.io");
  79.             t.text = "Join an existing game, or make a new one";
  80.            
  81.             //Set developmentsever (Comment out to connect to your server online)
  82.             client.multiplayer.developmentServer = "24.107.232.63:8184";
  83.            
  84.             Registry.client = client;
  85.             //Create pr join the room test
  86.             //client.multiplayer.createJoinRoom(
  87.                 //"test",                               //Room id. If set to null a random roomid is used
  88.                 //"MyCode",                         //The game type started on the server
  89.                 //true,                             //Should the room be visible in the lobby?
  90.                 //{},                                   //Room data. This data is returned to lobby list. Variabels can be modifed on the server
  91.                 //{},                                   //User join data
  92.                 //handleJoin,                           //Function executed on successful joining of the room
  93.                 //handleError                           //Function executed if we got a join error
  94.             //);
  95.             client.multiplayer.listRooms("MyCode", null, 10, 0, roomListing, null);
  96.         }
  97.         private function handleJoin(connection:Connection):void{
  98.             FlxG.log("Sucessfully connected to the multiplayer server");
  99.             t.text = "Connected, waiting on both players to be ready";
  100.             readyButton.visible = true;
  101.             Registry.connection = connection;
  102.             connection.addMessageHandler("State Setup", quickLaunch);
  103.             connection.addMessageHandler("begin", begin);
  104.             connection.addMessageHandler("wait", wait);
  105.         }
  106.         private function quickLaunch(m:Message, userid:uint):void {
  107.             Registry.playerIndex = m.getInt(0);
  108.         }
  109.         private function begin(m:Message, userid:uint):void {
  110.             Registry.connection.removeMessageHandler("State Setup", quickLaunch);
  111.             Registry.connection.removeMessageHandler("begin", begin);
  112.             Registry.connection.removeMessageHandler("wait", wait);
  113.             FlxG.stage.removeChild(bumb);
  114.             FlxG.switchState(new PlayState());
  115.         }
  116.         private function wait(m:Message):void {
  117.             if(m.getString(0) == "W1"){
  118.                 t.text = "Waiting on another player to join";
  119.             }else {
  120.                 t.text = "Waiting on the other player to Ready";
  121.             }
  122.         }
  123.        
  124.         private function readyUp():void {
  125.             Registry.connection.send("ready");
  126.         }
  127.        
  128.         override public function update():void
  129.         {
  130.             time += FlxG.elapsed;
  131.             if (time > 10) {
  132.                 Registry.client.multiplayer.listRooms("MyCode", null, 10, 0, roomListing, null);
  133.                 time = 0;
  134.             }
  135.             super.update();
  136.             if (FlxG.keys.justPressed("ENTER")) {
  137.                 if(!isInRoom){
  138.                     Registry.client.multiplayer.createJoinRoom(
  139.                     bumb.text,                          //Room id. If set to null a random roomid is used
  140.                     "MyCode",                           //The game type started on the server
  141.                     true,                               //Should the room be visible in the lobby?
  142.                     {},                                 //Room data. This data is returned to lobby list. Variabels can be modifed on the server
  143.                     {},                                 //User join data
  144.                     handleJoin,                         //Function executed on successful joining of the room
  145.                     handleError                         //Function executed if we got a join error
  146.                     );
  147.                     roomName = bumb.text;
  148.                     isInRoom = true;
  149.                 }else {
  150.                     if (bumb.text != roomName) {
  151.                         Registry.connection.disconnect();
  152.                         Registry.client.multiplayer.createJoinRoom(
  153.                         bumb.text,                          //Room id. If set to null a random roomid is used
  154.                         "MyCode",                           //The game type started on the server
  155.                         true,                               //Should the room be visible in the lobby?
  156.                         {},                                 //Room data. This data is returned to lobby list. Variabels can be modifed on the server
  157.                         {},                                 //User join data
  158.                         handleJoin,                         //Function executed on successful joining of the room
  159.                         handleError                         //Function executed if we got a join error
  160.                         );
  161.                         roomName = bumb.text;
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.        
  167.         private function roomListing(rooms:Array):void {
  168.             if(FlxG.state is MenuState){
  169.                 bumb2.text = "Room Listing\n";
  170.                 for each(var a:RoomInfo in rooms) {
  171.                     bumb2.text += a.id + "\n";
  172.                 }
  173.                 FlxG.log("Listing Rooms");
  174.             }
  175.         }
  176.        
  177.         private function handleDisconnect():void{
  178.             FlxG.log("Disconnected from server")
  179.         }
  180.         private function handleError(error:PlayerIOError):void{
  181.             FlxG.log("got" + error)
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement