Example #1
0
    /// <summary>
    /// Initializes the PlayersManager Component.
    /// </summary>
    public void InitializePlayersManager()
    {
        // Gets the GameManager reference.
        gameManager = GetComponent <GameManager>();

        // Initializes the AIPlayers List.
        AIPlayers = new List <PlayerAI>();

        // Clears the content of the PlayersAIGroup GameObject.
        foreach (Transform child in PlayersAIGroup.transform)
        {
            Destroy(child.gameObject);
        }

        // For each data in DataManager, the system creates an AI Player with the correct settings. The new player is added to PlayersAIGroup.
        foreach (DataManager.PlayerAIInitData data in gameManager.DataManager.GetPlayersToInit())
        {
            GameObject currentPlayer   = Instantiate(playerAIPrefab, PlayersAIGroup.transform);
            PlayerAI   currentPlayerAI = currentPlayer.GetComponent <PlayerAI>();

            currentPlayerAI.playerName                 = data.Name;
            currentPlayerAI.percentageOfRisk           = data.RiskPercentage;
            currentPlayerAI.minValueForRiskCalculation = data.RiskCalcMinValue;

            // Visual update.
            currentPlayerAI.nameText.text = currentPlayerAI.playerName;

            AIPlayers.Add(currentPlayerAI);
        }

        // The name of the dealer is updated with the name set in DataManager.
        playerDealer.playerName = gameManager.DataManager.DealerName;
        // Visual update.
        playerDealer.nameText.text = playerDealer.playerName;

        AIPlayersCount = AIPlayers.Count;

        // Setting up the real angle between players.
        float realPlayersDistanceAngle = playersDistanceAngle;

        if (AIPlayersCount == 1)
        {
            realPlayersDistanceAngle = maxArrangementAngle / 2;
        }
        else if (realPlayersDistanceAngle * AIPlayersCount > maxArrangementAngle)
        {
            realPlayersDistanceAngle = maxArrangementAngle / (AIPlayersCount - 1);
        }

        // Every player is located in its proper position.
        // For each player, the system initializes its OnFinish delegate value.
        for (int i = 0; i < AIPlayersCount; i++)
        {
            PlayerAI playerAI = AIPlayers[i];
            playerAI.OnFinish += MoveToNextPlayer;

            playerAI.transform.rotation = Quaternion.Euler(
                0f,
                (realPlayersDistanceAngle * i) - (realPlayersDistanceAngle * (AIPlayersCount - 1) / 2),
                0f
                );

            playerAI.SetInfoCanvasRotation();
        }
        // Setting the delegate of the Dealer.
        playerDealer.OnFinish += CheckWinners;

        // Every single AI Player's score and visual infos are resetted.
        StartCoroutine(ResetPlayers());
    }