Advertisement
Guest User

LobbyPlayer.cs

a guest
Jun 3rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.38 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Networking;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7.  
  8. namespace Prototype.NetworkLobby
  9. {
  10.     //Player entry in the lobby. Handle selecting color/setting name & getting ready for the game
  11.     //Any LobbyHook can then grab it and pass those value to the game player prefab (see the Pong Example in the Samples Scenes)
  12.     public class LobbyPlayer : NetworkLobbyPlayer
  13.     {
  14.         static Color[] Colors = new Color[] { Color.magenta, Color.red, Color.cyan, Color.blue, Color.green, Color.yellow };
  15.         //used on server to avoid assigning the same color to two player
  16.         static List<int> _colorInUse = new List<int>();
  17.  
  18.         public Button colorButton;
  19.         public InputField nameInput;
  20.         public Dropdown character;
  21.         public Button readyButton;
  22.         public Button waitingPlayerButton;
  23.         public Button removePlayerButton;
  24.  
  25.         public GameObject localIcone;
  26.         public GameObject remoteIcone;
  27.  
  28.         //OnMyName function will be invoked on clients when server change the value of playerName
  29.         [SyncVar(hook = "OnMyName")]
  30.         public string playerName = "";
  31.         [SyncVar(hook = "OnMyColor")]
  32.         public Color playerColor = Color.white;
  33.         [SyncVar(hook = "OnMyCharacter")]
  34.         public int playerCharacter = 0;
  35.  
  36.         public Color OddRowColor = new Color(250.0f / 255.0f, 250.0f / 255.0f, 250.0f / 255.0f, 1.0f);
  37.         public Color EvenRowColor = new Color(180.0f / 255.0f, 180.0f / 255.0f, 180.0f / 255.0f, 1.0f);
  38.  
  39.         static Color JoinColor = new Color(255.0f/255.0f, 0.0f, 101.0f/255.0f,1.0f);
  40.         static Color NotReadyColor = new Color(34.0f / 255.0f, 44 / 255.0f, 55.0f / 255.0f, 1.0f);
  41.         static Color ReadyColor = new Color(0.0f, 204.0f / 255.0f, 204.0f / 255.0f, 1.0f);
  42.         static Color TransparentColor = new Color(0, 0, 0, 0);
  43.  
  44.         //static Color OddRowColor = new Color(250.0f / 255.0f, 250.0f / 255.0f, 250.0f / 255.0f, 1.0f);
  45.         //static Color EvenRowColor = new Color(180.0f / 255.0f, 180.0f / 255.0f, 180.0f / 255.0f, 1.0f);
  46.  
  47.  
  48.         public override void OnClientEnterLobby()
  49.         {
  50.             base.OnClientEnterLobby();
  51.  
  52.             if (LobbyManager.s_Singleton != null) LobbyManager.s_Singleton.OnPlayersNumberModified(1);
  53.  
  54.             LobbyPlayerList._instance.AddPlayer(this);
  55.             LobbyPlayerList._instance.DisplayDirectServerWarning(isServer && LobbyManager.s_Singleton.matchMaker == null);
  56.  
  57.             if (isLocalPlayer)
  58.             {
  59.                 SetupLocalPlayer();
  60.             }
  61.             else
  62.             {
  63.                 SetupOtherPlayer();
  64.             }
  65.  
  66.             //setup the player data on UI. The value are SyncVar so the player
  67.             //will be created with the right value currently on server
  68.             OnMyName(playerName);
  69.             OnMyColor(playerColor);
  70.             OnMyCharacter(playerCharacter);
  71.         }
  72.  
  73.         public override void OnStartAuthority()
  74.         {
  75.             base.OnStartAuthority();
  76.  
  77.             //if we return from a game, color of text can still be the one for "Ready"
  78.             readyButton.transform.GetChild(0).GetComponent<Text>().color = Color.white;
  79.  
  80.            SetupLocalPlayer();
  81.         }
  82.  
  83.         void ChangeReadyButtonColor(Color c)
  84.         {
  85.             ColorBlock b = readyButton.colors;
  86.             b.normalColor = c;
  87.             b.pressedColor = c;
  88.             b.highlightedColor = c;
  89.             b.disabledColor = c;
  90.             readyButton.colors = b;
  91.         }
  92.  
  93.         void SetupOtherPlayer()
  94.         {
  95.             nameInput.interactable = false;
  96.             removePlayerButton.interactable = NetworkServer.active;
  97.  
  98.             ChangeReadyButtonColor(NotReadyColor);
  99.  
  100.             readyButton.transform.GetChild(0).GetComponent<Text>().text = "...";
  101.             readyButton.interactable = false;
  102.  
  103.             OnClientReady(false);
  104.         }
  105.  
  106.         void SetupLocalPlayer()
  107.         {
  108.             nameInput.interactable = true;
  109.             remoteIcone.gameObject.SetActive(false);
  110.             localIcone.gameObject.SetActive(true);
  111.  
  112.             CheckRemoveButton();
  113.  
  114.             if (playerColor == Color.white)
  115.                 CmdColorChange();
  116.  
  117.             ChangeReadyButtonColor(JoinColor);
  118.  
  119.             readyButton.transform.GetChild(0).GetComponent<Text>().text = "JOIN";
  120.             readyButton.interactable = true;
  121.  
  122.             //have to use child count of player prefab already setup as "this.slot" is not set yet
  123.             if (playerName == "")
  124.                 CmdNameChanged("Player" + (LobbyPlayerList._instance.playerListContentTransform.childCount-1));
  125.  
  126.             //we switch from simple name display to name input
  127.             colorButton.interactable = true;
  128.             nameInput.interactable = true;
  129.  
  130.             nameInput.onEndEdit.RemoveAllListeners();
  131.             nameInput.onEndEdit.AddListener(OnNameChanged);
  132.  
  133.             character.onValueChanged.AddListener(OnCharacterChanged);
  134.  
  135.             colorButton.onClick.RemoveAllListeners();
  136.             colorButton.onClick.AddListener(OnColorClicked);
  137.  
  138.             readyButton.onClick.RemoveAllListeners();
  139.             readyButton.onClick.AddListener(OnReadyClicked);
  140.  
  141.             //when OnClientEnterLobby is called, the loval PlayerController is not yet created, so we need to redo that here to disable
  142.             //the add button if we reach maxLocalPlayer. We pass 0, as it was already counted on OnClientEnterLobby
  143.             if (LobbyManager.s_Singleton != null) LobbyManager.s_Singleton.OnPlayersNumberModified(0);
  144.         }
  145.  
  146.         //This enable/disable the remove button depending on if that is the only local player or not
  147.         public void CheckRemoveButton()
  148.         {
  149.             if (!isLocalPlayer)
  150.                 return;
  151.  
  152.             int localPlayerCount = 0;
  153.             foreach (UnityEngine.Networking.PlayerController p in ClientScene.localPlayers)
  154.                 localPlayerCount += (p == null || p.playerControllerId == -1) ? 0 : 1;
  155.  
  156.             removePlayerButton.interactable = localPlayerCount > 1;
  157.         }
  158.  
  159.         public override void OnClientReady(bool readyState)
  160.         {
  161.             if (readyState)
  162.             {
  163.                 ChangeReadyButtonColor(TransparentColor);
  164.  
  165.                 Text textComponent = readyButton.transform.GetChild(0).GetComponent<Text>();
  166.                 textComponent.text = "READY";
  167.                 textComponent.color = ReadyColor;
  168.                 readyButton.interactable = false;
  169.                 colorButton.interactable = false;
  170.                 nameInput.interactable = false;
  171.             }
  172.             else
  173.             {
  174.                 ChangeReadyButtonColor(isLocalPlayer ? JoinColor : NotReadyColor);
  175.  
  176.                 Text textComponent = readyButton.transform.GetChild(0).GetComponent<Text>();
  177.                 textComponent.text = isLocalPlayer ? "JOIN" : "...";
  178.                 textComponent.color = Color.white;
  179.                 readyButton.interactable = isLocalPlayer;
  180.                 colorButton.interactable = isLocalPlayer;
  181.                 nameInput.interactable = isLocalPlayer;
  182.             }
  183.         }
  184.  
  185.         public void OnPlayerListChanged(int idx)
  186.         {
  187.             GetComponent<Image>().color = (idx % 2 == 0) ? EvenRowColor : OddRowColor;
  188.         }
  189.  
  190.         ///===== callback from sync var
  191.  
  192.         public void OnMyName(string newName)
  193.         {
  194.             playerName = newName;
  195.             nameInput.text = playerName;
  196.         }
  197.  
  198.         public void OnMyColor(Color newColor)
  199.         {
  200.             playerColor = newColor;
  201.             colorButton.GetComponent<Image>().color = newColor;
  202.         }
  203.  
  204.         public void OnMyCharacter(int newOption)
  205.         {
  206.             playerCharacter = newOption;
  207.             character.value = newOption;
  208.         }
  209.  
  210.         //===== UI Handler
  211.  
  212.         //Note that those handler use Command function, as we need to change the value on the server not locally
  213.         //so that all client get the new value throught syncvar
  214.         public void OnColorClicked()
  215.         {
  216.             CmdColorChange();
  217.         }
  218.  
  219.         public void OnReadyClicked()
  220.         {
  221.             SendReadyToBeginMessage();
  222.         }
  223.  
  224.         public void OnNameChanged(string str)
  225.         {
  226.             CmdNameChanged(str);
  227.         }
  228.  
  229.         public void OnCharacterChanged(int option)
  230.         {
  231.             CmdCharacterChanged(option);
  232.         }
  233.  
  234.         public void OnRemovePlayerClick()
  235.         {
  236.             if (isLocalPlayer)
  237.             {
  238.                 RemovePlayer();
  239.             }
  240.             else if (isServer)
  241.                 LobbyManager.s_Singleton.KickPlayer(connectionToClient);
  242.                
  243.         }
  244.  
  245.         public void ToggleJoinButton(bool enabled)
  246.         {
  247.             readyButton.gameObject.SetActive(enabled);
  248.             waitingPlayerButton.gameObject.SetActive(!enabled);
  249.         }
  250.  
  251.         [ClientRpc]
  252.         public void RpcUpdateCountdown(int countdown)
  253.         {
  254.             LobbyManager.s_Singleton.countdownPanel.UIText.text = "Match Starting in " + countdown;
  255.             LobbyManager.s_Singleton.countdownPanel.gameObject.SetActive(countdown != 0);
  256.         }
  257.  
  258.         [ClientRpc]
  259.         public void RpcUpdateRemoveButton()
  260.         {
  261.             CheckRemoveButton();
  262.         }
  263.  
  264.         //====== Server Command
  265.  
  266.         [Command]
  267.         public void CmdColorChange()
  268.         {
  269.             int idx = System.Array.IndexOf(Colors, playerColor);
  270.  
  271.             int inUseIdx = _colorInUse.IndexOf(idx);
  272.  
  273.             if (idx < 0) idx = 0;
  274.  
  275.             idx = (idx + 1) % Colors.Length;
  276.  
  277.             bool alreadyInUse = false;
  278.  
  279.             do
  280.             {
  281.                 alreadyInUse = false;
  282.                 for (int i = 0; i < _colorInUse.Count; ++i)
  283.                 {
  284.                     if (_colorInUse[i] == idx)
  285.                     {//that color is already in use
  286.                         alreadyInUse = true;
  287.                         idx = (idx + 1) % Colors.Length;
  288.                     }
  289.                 }
  290.             }
  291.             while (alreadyInUse);
  292.  
  293.             if (inUseIdx >= 0)
  294.             {//if we already add an entry in the colorTabs, we change it
  295.                 _colorInUse[inUseIdx] = idx;
  296.             }
  297.             else
  298.             {//else we add it
  299.                 _colorInUse.Add(idx);
  300.             }
  301.  
  302.             playerColor = Colors[idx];
  303.         }
  304.  
  305.         [Command]
  306.         public void CmdNameChanged(string name)
  307.         {
  308.             playerName = name;
  309.         }
  310.  
  311.         [Command]
  312.         public void CmdCharacterChanged(int option)
  313.         {
  314.             playerCharacter = option;
  315.         }
  316.  
  317.         //Cleanup thing when get destroy (which happen when client kick or disconnect)
  318.         public void OnDestroy()
  319.         {
  320.             LobbyPlayerList._instance.RemovePlayer(this);
  321.             if (LobbyManager.s_Singleton != null) LobbyManager.s_Singleton.OnPlayersNumberModified(-1);
  322.  
  323.             int idx = System.Array.IndexOf(Colors, playerColor);
  324.  
  325.             if (idx < 0)
  326.                 return;
  327.  
  328.             for (int i = 0; i < _colorInUse.Count; ++i)
  329.             {
  330.                 if (_colorInUse[i] == idx)
  331.                 {//that color is already in use
  332.                     _colorInUse.RemoveAt(i);
  333.                     break;
  334.                 }
  335.             }
  336.         }
  337.     }
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement