Пример #1
0
    /// <summary>
    /// Used at the beginning of the game to shuffle the territories and distribute them randomly to each player active in the game
    /// </summary>
    public void DebugPopulateTerritories()
    {
        System.Random rng = new System.Random();
        int           n   = unclaimedTerritories;

        TerritoryNode[] shuffledTerritories = allTerritories;

        debugPopulateButton.SetActive(false);

        // Shuffle All the territories
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);

            TerritoryNode terry = shuffledTerritories[k];
            shuffledTerritories[k] = shuffledTerritories[n];
            shuffledTerritories[n] = terry;
        }


        // Distribute the territories in order to each active player
        int i = 0;

        n = unclaimedTerritories;
        while (i < n)
        {
            for (int j = 0; j < activePlayers; j++)
            {
                TerritoryNode terry = shuffledTerritories[i];

                terry.SetCurrentSelection(true);
                ConquerTerritory(terry, j);
                terry.SetCurrentSelection(false);
                unclaimedTerritories--;
                i++;

                if (i >= n)
                {
                    currentPlayersTurn = j;                           // set player's turn to the last player to place an army before advancing
                    break;
                }
            }
        }

        AdvanceTurn();
    }
Пример #2
0
    // Update is called once per frame
    void Update()
    {
        if (optionsSelected)       //gameplay

        {
            if (!setup)
            {
                // Game Over - Display Winner and limit interacion
                if (activePlayers < 2)
                {
                    //TODO : GAME OVER STUFF
                }

                // Main game loop - filtered by gamestate
                switch (currentPhase)
                {
                    #region RECRUIT
                case TURN_PHASE.RECRUIT:

                    // Beginning of round Initialization
                    if (!didOnce)
                    {
                        reinforceUI.SetActive(true);
                        attackUI.SetActive(false);
                        fortifyUI.SetActive(false);
                        CalculateReinforcements(currentPlayers[currentPlayersTurn]);
                        didOnce = true;
                    }

                    // Entry point for input - left mouse button to interact
                    if (Input.GetMouseButtonDown(0) && currentPlayersTurn != -1)
                    {
                        RaycastHit2D mouseCast2D = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition), 100, 1 << LayerMask.NameToLayer("Territory"));             // Raycast mouse position, return hit on territory

                        if (mouseCast2D)
                        {
                            // Deselect a territory that has been selected already
                            if (currentTerritory)
                            {
                                currentTerritory.SetCurrentSelection(false);
                                currentTerritory = null;
                            }

                            currentTerritory = mouseCast2D.rigidbody.GetComponent <TerritoryNode>();                                                            // Set the current active territory to the one that was clicked

                            if (currentPlayers[currentPlayersTurn].GetArmies() > 0 && currentTerritory.DisplayOwner() == currentPlayersTurn)
                            {
                                currentTerritory.SetCurrentSelection(true);
                                currentTerritory.AdjustSoldiers(1);
                                currentPlayers[currentPlayersTurn].AddArmies(-1);

                                if (currentPlayers[currentPlayersTurn].GetArmies() == 0)
                                {
                                    turnButton.SetActive(true);
                                }
                            }
                        }
                    }

                    break;
                    #endregion

                    #region ATTACK
                case TURN_PHASE.ATTACK:


                    // Beginning of Attack Phase Initialization
                    if (!didOnce)
                    {
                        reinforceUI.SetActive(false);
                        attackUI.SetActive(true);
                        fortifyUI.SetActive(false);
                        didOnce = true;
                    }

                    ResolveCombat();

                    if (territoryConquered)
                    {
                        attackButton.text     = "Move";
                        attackSlider.maxValue = currentTerritory.DisplaySoldiers() - 1;
                        attackDice.text       = attackSlider.value.ToString();

                        if (!attackPanel.activeInHierarchy)
                        {
                            OpenAttackPanel();
                        }


                        //Mouse Input while the attack options panel is closed
                    }
                    else if (Input.GetMouseButtonDown(0) && !attackPanel.activeInHierarchy && currentPlayersTurn != -1)
                    {
                        RaycastHit2D mouseCast2D = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition), 100, 1 << LayerMask.NameToLayer("Territory"));

                        if (mouseCast2D)
                        {
                            TerritoryNode newTerritory = mouseCast2D.rigidbody.GetComponent <TerritoryNode>();


                            // clicked territory is a territory adjacent to selected player territory.
                            if (newTerritory != currentTerritory && newTerritory.GetCurrentSelection())
                            {
                                currentTerritory.DeselectAdjacentTerritories();
                                SelectDefendingCountry(newTerritory);
                                attackSlider.minValue = 1;
                                OpenAttackPanel();

                                //clicked territory is owned by the player and is eligible to attack
                            }
                            else if (newTerritory.DisplayOwner() == currentPlayersTurn && newTerritory.DisplaySoldiers() > 1)
                            {
                                SelectActiveTerritory(newTerritory);

                                //clicked territory does not qualify as an attacking country or defending country
                            }
                            else
                            {
                                DeselectTerritory();
                            }
                        }
                        else
                        {
                            DeselectTerritory();
                        }
                    }

                    // Attack options panel/finalizing the attack.
                    if (attackPanel.activeInHierarchy && !territoryConquered)
                    {
                        attackButton.text = "Attack!";

                        if (currentTerritory.DisplaySoldiers() > 3)
                        {
                            attackSlider.maxValue = 3;
                        }
                        else
                        {
                            attackSlider.maxValue = currentTerritory.DisplaySoldiers() - 1;
                        }

                        attackDice.text = attackSlider.value.ToString();
                    }

                    break;
                    #endregion

                    #region FORTIFY
                case TURN_PHASE.FORTIFY:

                    // Beginning of phase initialization
                    if (!didOnce)
                    {
                        reinforceUI.SetActive(false);
                        attackUI.SetActive(false);
                        fortifyUI.SetActive(true);
                    }

                    //Mouse Input while the attack options panel is closed
                    if (Input.GetMouseButtonDown(0) && !attackPanel.activeInHierarchy && currentPlayersTurn != -1 && !fortified)
                    {
                        RaycastHit2D mouseCast2D = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition), 100, 1 << LayerMask.NameToLayer("Territory"));

                        if (mouseCast2D)
                        {
                            TerritoryNode newTerritory = mouseCast2D.rigidbody.GetComponent <TerritoryNode>();

                            // clicked territory is a territory adjacent to selected player territory.
                            if (newTerritory != currentTerritory && newTerritory.GetCurrentSelection())
                            {
                                currentTerritory.DeselectAdjacentTerritories();
                                SelectDefendingCountry(newTerritory);
                                OpenAttackPanel();
                            }
                            else if (newTerritory.DisplayOwner() == currentPlayersTurn && newTerritory.DisplaySoldiers() > 1)           //clicked territory is owned by the player and is eligible to fortify another territory

                            {
                                DeselectTerritory();
                                SelectActiveTerritory(newTerritory);
                            }
                            else                                                                                 //clicked territory does not qualify as an attacking country or defending country
                            {
                                DeselectTerritory();
                            }
                        }
                        else
                        {
                            DeselectTerritory();
                        }
                    }


                    if (attackPanel.activeInHierarchy)         //Fortify options panel. (repurposed attack options panel)

                    {
                        attackButton.text     = "Fortify";
                        attackSlider.minValue = 1;
                        attackSlider.maxValue = currentTerritory.DisplaySoldiers() - 1;
                        attackDice.text       = attackSlider.value.ToString();
                    }

                    if (fortified)
                    {
                        NextTurnButton();         // Advance turn when all actions have been taken
                    }
                    break;
                    #endregion


                default:
                    Debug.Log("Its just a phase....");                        break;
                }

                #region SETUP
            }
            else        //Setup Phase -- Ends when all Players are out of soldiers to place

            {
                if (unclaimedTerritories <= 0)
                {
                    allTsClaimed      = true;
                    phaseInfoTxt.text = "Reinforce claimed territories";
                }


                if (Input.GetMouseButtonDown(0) && currentPlayersTurn != -1)        //Mouse Input handling ( Click on Territory to place soldier)

                {
                    RaycastHit2D mouseCast2D = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition), 100, 1 << LayerMask.NameToLayer("Territory"));

                    if (mouseCast2D)
                    {
                        if (currentTerritory)
                        {
                            currentTerritory.SetCurrentSelection(false);
                        }

                        currentTerritory = mouseCast2D.rigidbody.GetComponent <TerritoryNode>();

                        // can only pick neutral territories until there are no more
                        if (!allTsClaimed)
                        {
                            // Can only pick neutral territories - territories with owner set to -1
                            if (currentTerritory.DisplayOwner() < 0)
                            {
                                // Hide the populate button once the first territory has been selected
                                if (debugPopulateButton.activeInHierarchy)
                                {
                                    debugPopulateButton.SetActive(false);
                                }


                                currentTerritory.SetCurrentSelection(true);
                                currentTerritory.AdjustSoldiers(1);
                                currentPlayers[currentPlayersTurn].AddArmies(-1);
                                currentPlayers[currentPlayersTurn].AddTerritory(currentTerritory);
                                currentTerritory.SetColor(currentPlayers[currentPlayersTurn].armyColour);
                                currentTerritory.SetOwner(currentPlayersTurn);
                                unclaimedTerritories -= 1;

                                if (currentTerritory.GetContinent().CheckBonus(currentPlayersTurn))
                                {
                                    currentTerritory.GetContinent().UpdateBorderColour(GetCurrentPlayer().armyColour);
                                }



                                AdvanceTurn();
                            }
                        }
                        else              //can only click on owned territories to add soldiers

                        //TODO: Add new graphic or text to show that all territories have been claimed, and the player must now only choose territories they own

                        {
                            if (currentTerritory.DisplayOwner() == currentPlayersTurn)
                            {
                                currentTerritory.SetCurrentSelection(true);
                                currentTerritory.AdjustSoldiers(1);
                                currentPlayers[currentPlayersTurn].AddArmies(-1);
                                AdvanceTurn();
                            }
                        }
                    }   //end of raycasts
                }       // end of input

                if (currentPlayers[currentPlayersTurn].GetArmies() < 1)
                {
                    setup             = false;
                    phaseInfoTxt.text = "Current Phase: ";
                }
            }      //end of setup phase
            #endregion

            //Display Info
            int uITurnInfo = currentPlayersTurn + 1;
            turnInfo.text     = "Player " + uITurnInfo + "'s Turn!";
            turnInfoCol.color = currentPlayers[currentPlayersTurn].armyColour;
            border.color      = currentPlayers[currentPlayersTurn].armyColour;
        }   //end of gameplay
    }