示例#1
0
    private IEnumerator AnimateLeaderboardsUI(Dictionary <GameObject, int> animationNewPos, Dictionary <string, int> animationNewScores, string players, float timerNewScore = 4f, float timerChangePos = 1f)
    {
        PlayerCircleParentObject.GetComponent <RectTransform>().offsetMax = new Vector2((PlayerCircleParentObject.GetComponent <RectTransform>().offsetMax.x + (Screen.width - 200) / 4 * (animationNewPos.Count - 4)), PlayerCircleParentObject.GetComponent <RectTransform>().offsetMax.y);
        bool doFade = animationNewPos.Count > 4;

        yield return(new WaitForSeconds(Time.deltaTime));//Wait a second so UI could update and ann new objects

        //Prepare variables
        List <float> CircleUIxByPosition = new List <float>();

        foreach (GameObject obj in animationNewPos.Keys)
        {
            CircleUIxByPosition.Add(obj.transform.position.x);
            Debug.Log("Circle by position x value: " + obj.transform.position.x);
        }
        CircleUIxByPosition.Sort();
        //CircleUIxByPosition.Reverse();
        float posDiff = Mathf.Abs(CircleUIxByPosition[0] - CircleUIxByPosition[1]);

        List <Vector3> originalPositons = new List <Vector3>();
        List <Vector3> targetPositions  = new List <Vector3>();
        int            index            = 0;

        foreach (GameObject obj in animationNewPos.Keys)
        {
            originalPositons.Add(obj.transform.position);
            if (index < 4)
            {
                targetPositions.Add(new Vector3(obj.transform.position.x - animationNewPos[obj] * posDiff, obj.transform.position.y, obj.transform.position.z));
            }
            else
            {
                targetPositions.Add(new Vector3(CircleUIxByPosition[animationNewPos[obj] - 1], obj.transform.position.y, obj.transform.position.z));
                Debug.Log("First place X: " + CircleUIxByPosition[0] + "; Target X: " + targetPositions[index].x + "; Diff: " + posDiff + "; animationNewPos[obj] - 1: " + (animationNewPos[obj] - 1) + ";");
            }
            index++;
        }
        float timer = 0f;

        PlayerCircleParentObject.GetComponent <HorizontalLayoutGroup>().enabled = false;

        //Do score up animation
        while (timer < timerNewScore)
        {
            timer += Time.deltaTime;
            if (doFade)
            {
                foreach (GameObject obj in animationNewPos.Keys)
                {
                    obj.GetComponent <FadeInScript>().SetFade((CircleUIxByPosition[4] - obj.transform.position.x) / posDiff);
                }
            }
            yield return(new WaitForSeconds(Time.deltaTime));
        }
        foreach (GameObject obj in animationNewPos.Keys)
        {
            LeaderboardCircleUIScript objScript = obj.GetComponent <LeaderboardCircleUIScript>();
            objScript.TextScore.text = animationNewScores[objScript.GetUUID()].ToString();
        }

        //Do position change animation
        timer = 0f;
        while (timer < timerChangePos)
        {
            index = 0;
            foreach (GameObject obj in animationNewPos.Keys)
            {
                obj.transform.position = Vector3.Lerp(originalPositons[index], targetPositions[index], timer / timerChangePos);
                if (doFade)
                {
                    obj.GetComponent <FadeInScript>().SetFade((CircleUIxByPosition[4] - obj.transform.position.x) / posDiff);
                }
                index++;
            }
            timer += Time.deltaTime;
            yield return(new WaitForSeconds(Time.deltaTime));
        }

        //Set children correctly
        PlayerCircleParentObject.GetComponent <RectTransform>().offsetMax = new Vector2(-200, PlayerCircleParentObject.GetComponent <RectTransform>().offsetMax.y);
        UpdatePlayerListUI(players);

        //Lock grid
        PlayerCircleParentObject.GetComponent <HorizontalLayoutGroup>().enabled = true;
    }
示例#2
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();
            }
        }
    }
示例#3
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));
    }