devomage

MatchMaking Lobby Demo

Jun 15th, 2016
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.74 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using TNet;
  5.  
  6. public class lobbydemo : TNBehaviour
  7. {
  8.     public Canvas canvas;
  9.  
  10.     private Button button_play;
  11.     private Button button_connect;
  12.     private Button button_disconnect;
  13.    
  14.     private void Awake()
  15.     {
  16.         if (canvas == null)
  17.         {
  18.             Debug.LogError("set canvas");
  19.             Debug.Break();
  20.         }
  21.  
  22.         button_play = canvas.transform.FindChild("play").GetComponent<Button>();
  23.         button_play.onClick.AddListener(button_play_onClick);
  24.  
  25.         button_connect = canvas.transform.FindChild("connect").GetComponent<Button>();
  26.         button_connect.onClick.AddListener(button_connect_onClick);
  27.  
  28.         button_disconnect = canvas.transform.FindChild("disconnect").GetComponent<Button>();
  29.         button_disconnect.onClick.AddListener(button_disconnect_onClick);
  30.        
  31.         //when joining a channel the scene is reloaded (if using one scene)
  32.         if (TNManager.isConnected)
  33.         {
  34.             button_connect.interactable = false;
  35.             button_disconnect.interactable = true;
  36.  
  37.             //play button is only available if in the lobby channel
  38.             if (TNManager.IsInChannel(1000)) button_play.interactable = true;
  39.             else button_play.interactable = false;
  40.         }
  41.         else
  42.         {
  43.             button_play.interactable = false;
  44.             button_connect.interactable = true;
  45.             button_disconnect.interactable = false;
  46.         }
  47.     }
  48.    
  49.     new void OnEnable()
  50.     {
  51.         TNManager.onConnect += network_onConnect;
  52.         TNManager.onDisconnect += network_onDisconnect;
  53.  
  54.         TNManager.onJoinChannel += network_onJoinChannel;
  55.         TNManager.onPlayerJoin += network_onPlayerJoin;
  56.     }
  57.  
  58.     void OnDisable()
  59.     {
  60.         TNManager.onConnect -= network_onConnect;
  61.         TNManager.onDisconnect -= network_onDisconnect;
  62.  
  63.         TNManager.onJoinChannel -= network_onJoinChannel;
  64.         TNManager.onPlayerJoin -= network_onPlayerJoin;
  65.     }
  66.  
  67.     private void button_connect_onClick()
  68.     {
  69.         button_connect.interactable = false;
  70.        
  71.         TNManager.Connect("192.168.1.105", 10420);
  72.     }
  73.  
  74.     private void button_disconnect_onClick()
  75.     {
  76.         button_disconnect.interactable = false;
  77.  
  78.         TNManager.Disconnect();
  79.     }
  80.  
  81.     private void button_play_onClick()
  82.     {
  83.         //button_play.interactable = false;
  84.  
  85.         Debug.Log(TNManager.player.Get<bool>("isQue"));
  86.  
  87.         TNManager.SetPlayerData("isQue", true);
  88.  
  89.         tno.Send("RFC_QueGame", Target.Host, TNManager.playerID, TNManager.playerName);
  90.     }
  91.  
  92.     #region RFC's
  93.    
  94.     [RFC]
  95.     void RFC_QueGame(int playerid, string playername)
  96.     {
  97.         //this RFC is only sent to the channel host
  98.         //the 'isHosting' player would get Que priority
  99.  
  100.         //client player
  101.         Debug.Log(TNManager.player.Get<bool>("isQue"));
  102.  
  103.         //network players
  104.         foreach (Player p in TNManager.players)
  105.         {
  106.             Debug.Log(p.Get<bool>("isQue"));
  107.  
  108.             //if the 'isHosting' player is not in the Que - take the first
  109.             //2 players from the list
  110.             //or respond to the player that there is no other players available
  111.  
  112.            
  113.         }
  114.  
  115.         int newchannelid = 1;
  116.  
  117.         tno.Send("RFC_EnterGame", player1.id, newchannelid);
  118.         tno.Send("RFC_EnterGame", player2.id, newchannelid);
  119.     }
  120.  
  121.     [RFC]
  122.     void RFC_EnterGame(int channelid)
  123.     {
  124.         //set Que to false here
  125.         //or set when entering any new channel via the callback
  126.  
  127.         TNManager.SetPlayerData("isQue", false);
  128.        
  129.         TNManager.JoinChannel(channelid, "game", false, 2, null, true);
  130.     }
  131.  
  132.     #endregion
  133.  
  134.     #region TNET callbacks
  135.  
  136.     private void network_onConnect(bool success, string message)
  137.     {
  138.         if (success)
  139.         {
  140.             button_disconnect.interactable = true;
  141.  
  142.             //everyone joins main catch-all lobby channel
  143.             TNManager.JoinChannel(1000, "lobby", true, 500, null, true);
  144.         }
  145.         else Debug.Log(string.Format("network_onConnect: success={0} message={1}", success, message));
  146.     }
  147.  
  148.     private void network_onDisconnect()
  149.     {
  150.         button_connect.interactable = true;
  151.     }
  152.  
  153.     private void network_onJoinChannel(int channelID, bool success, string message)
  154.     {
  155.         Debug.Log(string.Format("network_onJoinChannel: channelID={0} success={1} message={2}", channelID, success, message));
  156.  
  157.         TNManager.SetPlayerData("isQue", false);
  158.     }
  159.  
  160.     private void network_onPlayerJoin(int channelID, Player p)
  161.     {
  162.         Debug.Log(string.Format("network_onPlayerJoin: channelID={0} p.name={1}", channelID, p.name));
  163.     }
  164.  
  165.     #endregion
  166. }
Advertisement
Add Comment
Please, Sign In to add comment