예제 #1
0
    //Function is used to update the UI for players
    private void UpdatePlayerListUI(string players)
    {
        //Start by getting player data
        GameObject[] playerObjects = GameObject.FindGameObjectsWithTag("Player");

        if (HostDecendant)
        {
            OverrideHostDecendantString(players);
        }

        PlayerCode localPlayer = playerObjects[0].GetComponent <PlayerCode>();

        foreach (GameObject playerObj in playerObjects)
        {
            if (playerObj.GetComponent <PlayerCode>().IsLocalPlayer())
            {
                localPlayer = playerObj.GetComponent <PlayerCode>();
                break;
            }
        }

        List <string> rpcPlayers = TurnManager.StringDeseparator(players);

        //Remove all children
        foreach (Transform child in PlayerCircleParentObject.transform)
        {
            GameObject.Destroy(child.gameObject);
        }

        //Get the rankings of players
        List <int> playerRankings = new List <int>();

        for (int i = 1; i <= rpcPlayers.Count; i++)
        {
            int           rank          = i;
            List <string> playerDetails = TurnManager.StringDeseparator(rpcPlayers[i - 1]);
            string        playerScore   = playerDetails[2];
            for (int j = i + 1; j <= rpcPlayers.Count; j++)
            {
                if (TurnManager.StringDeseparator(rpcPlayers[j - 1])[2] == playerScore)
                {
                    rank = j;
                }
                else
                {
                    j = rpcPlayers.Count + 1;
                }
            }
            playerRankings.Add(rank);
        }


        int nonPlayerFound = 0;
        int playerFound    = 0;

        for (int i = rpcPlayers.Count - 1; i >= 0; i--)
        {
            List <string> playerDetails = TurnManager.StringDeseparator(rpcPlayers[i]);
            string        playerUID     = playerDetails[0];
            if (playerUID == localPlayer.PlayerUID || ((playerFound == 0 && i <= 3) || (i <= 2))) //Only show the top 3 players + the local player
            {
                string     playerLetter                = playerDetails[1][0].ToString();
                string     playerScore                 = playerDetails[2];
                int        playerRank                  = playerRankings[i];
                GameObject createdPlayerUI             = (Instantiate(PlayerCirclePrefab, PlayerCirclePrefab.transform.position, Quaternion.Euler(0, 0, 0), PlayerCircleParentObject.transform)) as GameObject;
                LeaderboardCircleUIScript newObjScript = createdPlayerUI.GetComponent <LeaderboardCircleUIScript>();

                Color playerColor;
                if (ColorUtility.TryParseHtmlString("#" + playerDetails[3], out playerColor))
                {
                    newObjScript.SetBackgroundColor(playerColor);
                }

                newObjScript.SetFullName(playerDetails[1]);
                newObjScript.SetUUID(playerUID);


                if (playerUID == localPlayer.PlayerUID)
                {
                    newObjScript.SetOutlineColor(Color.green);
                    playerFound++;
                }
                else
                {
                    nonPlayerFound++;
                }


                newObjScript.TextName.text  = playerLetter;
                newObjScript.TextScore.text = playerScore;
                if (playerRank == 1)
                {
                    newObjScript.TextPosition.text = "1st";
                }
                else if (playerRank == 2)
                {
                    newObjScript.TextPosition.text = "2nd";
                }
                else if (playerRank == 3)
                {
                    newObjScript.TextPosition.text = "3rd";
                }
                else
                {
                    newObjScript.TextPosition.text = playerRank.ToString() + "th";
                }
                newObjScript.ChangeColorAccordingToPosition();
            }
        }
    }
예제 #2
0
    public void RpcTurnEndPlayerListUI(string players)
    {
        if (HostDecendant)
        {
            OverrideHostDecendantString(players);
        }

        List <string> UIplayerUIDs          = new List <string>();
        List <int>    UIplayerScores        = new List <int>();
        List <int>    UIplayerPositionDiffs = new List <int>();

        List <string> CMDplayerUIDs      = new List <string>();
        List <int>    CMDplayerScores    = new List <int>();
        List <string> CMDplayerPositions = new List <string>();
        List <Color>  CmdplayerColors    = new List <Color>();
        List <string> CMDplayerNames     = new List <string>();
        List <string> rpcPlayers         = TurnManager.StringDeseparator(players);
        List <int>    playerRankings     = new List <int>();

        Dictionary <GameObject, int> animationNewPos    = new Dictionary <GameObject, int>();
        Dictionary <string, int>     animationNewScores = new Dictionary <string, int>();

        //Get local player
        GameObject[] playerObjects = GameObject.FindGameObjectsWithTag("Player");
        PlayerCode   localPlayer   = playerObjects[0].GetComponent <PlayerCode>();

        foreach (GameObject playerObj in playerObjects)
        {
            if (playerObj.GetComponent <PlayerCode>().IsLocalPlayer())
            {
                localPlayer = playerObj.GetComponent <PlayerCode>();
                break;
            }
        }

        //Get data from UI icons
        foreach (Transform child in PlayerCircleParentObject.transform)
        {
            UIplayerUIDs.Add(child.GetComponent <LeaderboardCircleUIScript>().GetUUID());
            UIplayerScores.Add(int.Parse(child.GetComponent <LeaderboardCircleUIScript>().TextScore.text));
        }

        //Get data from Cmd function
        for (int i = 1; i <= rpcPlayers.Count; i++)
        {
            int           rank          = i;
            List <string> playerDetails = TurnManager.StringDeseparator(rpcPlayers[i - 1]);
            string        playerScore   = playerDetails[2];
            animationNewScores.Add(playerDetails[0], int.Parse(playerScore));
            for (int j = i + 1; j <= rpcPlayers.Count; j++)
            {
                if (TurnManager.StringDeseparator(rpcPlayers[j - 1])[2] == playerScore)
                {
                    rank = j;
                }
                else
                {
                    j = rpcPlayers.Count + 1;
                }
            }
            playerRankings.Add(rank);
        }
        int nonPlayerFound = 0;
        int playerFound    = 0;

        for (int i = rpcPlayers.Count - 1; i >= 0; i--)
        {
            List <string> playerDetails = TurnManager.StringDeseparator(rpcPlayers[i]);
            string        playerUID     = playerDetails[0];
            if (playerUID == localPlayer.PlayerUID || ((playerFound == 0 && i <= 3) || (i <= 2)))
            {
                string playerLetter = playerDetails[1][0].ToString();
                string playerScore  = playerDetails[2];
                int    playerRank   = playerRankings[i];

                Color playerColor;
                if (ColorUtility.TryParseHtmlString("#" + playerDetails[3], out playerColor))
                {
                    CmdplayerColors.Add(playerColor);
                }

                CMDplayerNames.Add(playerDetails[1]);
                CMDplayerUIDs.Add(playerUID);


                if (playerUID == localPlayer.PlayerUID)
                {
                    playerFound++;
                }
                else
                {
                    nonPlayerFound++;
                }

                CMDplayerScores.Add(int.Parse(playerScore));
                if (playerRank == 1)
                {
                    CMDplayerPositions.Add("1st");
                }
                else if (playerRank == 2)
                {
                    CMDplayerPositions.Add("2nd");
                }
                else if (playerRank == 3)
                {
                    CMDplayerPositions.Add("3rd");
                }
                else
                {
                    CMDplayerPositions.Add(playerRank.ToString() + "th");
                }
            }
        }

        //Check  existing UI positions diffs
        for (int i = 0; i < UIplayerUIDs.Count; i++)
        {
            int diff = -4;
            //Try to see if UI object exists in upcoming leaderboard
            for (int j = 0; j < CMDplayerUIDs.Count; j++)
            {
                if (UIplayerUIDs[i] == CMDplayerUIDs[j])
                {
                    //Player exists in new list, time to register his difference in position
                    diff = -1 * (i - j);//Diff is given minus because object is placed upside down
                    break;
                }
            }
            UIplayerPositionDiffs.Add(diff);
        }

        //Add all UI objects to animator dictionary
        int index = 0;

        foreach (Transform child in PlayerCircleParentObject.transform)
        {
            animationNewPos.Add(child.gameObject, UIplayerPositionDiffs[index]);
            index++;
        }

        //Check if any of the CMD objects didn't appear in the UI list
        for (int i = 0; i < CMDplayerUIDs.Count; i++)
        {
            bool found = false;
            //Try to see if UI object exists in upcoming leaderboard
            for (int j = 0; j < UIplayerUIDs.Count; j++)
            {
                if (UIplayerUIDs[j] == CMDplayerUIDs[i])
                {
                    found = true;
                    break;
                }
            }
            if (!found)//Cmd object doesn't exist in UI, time to make GameObject for it
            {
                GameObject createdPlayerUI = (Instantiate(PlayerCirclePrefab, PlayerCirclePrefab.transform.position, Quaternion.Euler(0, 0, 0), PlayerCircleParentObject.transform)) as GameObject;
                createdPlayerUI.transform.SetSiblingIndex(0);//Set as first child because order is reversed and we want it coming from bottom of leaderboard
                LeaderboardCircleUIScript newObjScript = createdPlayerUI.GetComponent <LeaderboardCircleUIScript>();
                newObjScript.SetBackgroundColor(CmdplayerColors[i]);
                newObjScript.SetFullName(CMDplayerNames[i]);
                newObjScript.SetUUID(CMDplayerUIDs[i]);
                newObjScript.TextName.text     = CMDplayerNames[i][0].ToString();
                newObjScript.TextScore.text    = CMDplayerScores[i].ToString();
                newObjScript.TextPosition.text = CMDplayerPositions[i];
                newObjScript.ChangeColorAccordingToPosition();

                //Add new player to animation dictionary
                animationNewPos.Add(createdPlayerUI, int.Parse(CMDplayerPositions[i].Replace("st", "").Replace("nd", "").Replace("rd", "").Replace("th", "")));
            }
        }

        //Update position text for all UI objects and hide it
        foreach (Transform child in PlayerCircleParentObject.transform)
        {
            LeaderboardCircleUIScript newObjScript = child.GetComponent <LeaderboardCircleUIScript>();
            for (int i = 0; i < CMDplayerUIDs.Count; i++)
            {
                if (newObjScript.GetUUID() == CMDplayerUIDs[i])
                {
                    newObjScript.TextPosition.text = CMDplayerPositions[i];
                    newObjScript.ChangeColorAccordingToPosition();
                }
            }
            newObjScript.SetPositionText(false);
        }

        //Start animation co-routine
        this.GetComponent <TurnManager>().SetupWhoVotedForWhoPanel(players);
        StartCoroutine(AnimateLeaderboardsUI(animationNewPos, animationNewScores, players));
    }