Ejemplo n.º 1
0
    //! Gets information about other players from the server.
    public IEnumerator GetNetworkPlayers()
    {
        getNetworkPlayersCoroutineBusy = true;

        playerData = "none";
        networkReceive.GetPlayerData();
        while (playerData == "none")
        {
            yield return(null);
        }

        playerNames.Clear();
        string[] playerList = playerData.Split('[');
        for (int i = 2; i < playerList.Length; i++)
        {
            string  playerInfo     = playerList[i];
            string  playerName     = playerInfo.Split(':')[0].Split(',')[0].TrimStart('"').TrimEnd('"');
            float   x              = float.Parse(playerInfo.Split(',')[1]);
            float   y              = float.Parse(playerInfo.Split(',')[2]);
            float   z              = float.Parse(playerInfo.Split(',')[3]);
            float   fx             = float.Parse(playerInfo.Split(',')[4]);
            float   fz             = float.Parse(playerInfo.Split(',')[5]);
            float   red            = float.Parse(playerInfo.Split(',')[6]);
            float   green          = float.Parse(playerInfo.Split(',')[7]);
            float   blue           = float.Parse(playerInfo.Split(',')[8].Split(']')[0]);
            Vector3 playerPosition = new Vector3(x, y, z);
            Color   playerColor    = new Color(red, green, blue);
            if (playerName != PlayerPrefs.GetString("UserName") && playerName != playerController.stateManager.worldName)
            {
                if (!playerNames.Contains(playerName))
                {
                    playerNames.Add(playerName);
                }
                if (!networkPlayers.ContainsKey(playerName))
                {
                    CreateNetworkPlayer(playerName, playerPosition, playerColor);
                }
                else
                {
                    UpdateNetWorkPlayer(playerInfo, playerColor);
                }
            }
            yield return(null);
        }

        Dictionary <string, GameObject> allPlayers = new Dictionary <string, GameObject>(networkPlayers);

        foreach (KeyValuePair <string, GameObject> entry in allPlayers)
        {
            if (!playerNames.Contains(entry.Key))
            {
                networkPlayers.Remove(entry.Key);
                UnityEngine.Object.Destroy(entry.Value);
            }
            yield return(null);
        }

        getNetworkPlayersCoroutineBusy = false;
    }