Пример #1
0
    //Select troop function

    //Select enemy function
    void SelectEnemy()
    {
        switch (gameManager.turn)
        {
        case (Turn.Red):     //red turn
            //cast ray to mouse position
            enemyTroopRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (SelectedTroop.tag == "Priest")
            {
                //if ray hits a blue troop
                if (Physics.Raycast(enemyTroopRay, out enemyTroopHit, 100, RedTroopMask))
                {
                    //store enemy gameobject and enemy script
                    enemySelectedTroop = enemyTroopHit.transform.gameObject;
                    enemyTroopScript   = enemySelectedTroop.GetComponent <TroopScript>();

                    if (enemySelectedTroop.tag != "Priest")
                    {
                        enemyTroopScript.ResetHealth();
                        animController.FaceEnemy(SelectedTroop, enemySelectedTroop);
                        enemyIndicator = false;
                    }
                }
            }
            else
            {
                //if ray hits a blue troop
                if (Physics.Raycast(enemyTroopRay, out enemyTroopHit, 100, BlueTroopMask))
                {
                    //store enemy gameobject and enemy script
                    enemySelectedTroop = enemyTroopHit.transform.gameObject;
                    enemyTroopScript   = enemySelectedTroop.GetComponent <TroopScript>();

                    if (SelectedTroop.tag == "Mage")
                    {
                        if (SelectedTroop.transform.position.x == enemySelectedTroop.transform.position.x || SelectedTroop.transform.position.z == enemySelectedTroop.transform.position.z)
                        {
                            if (enemySelectedTroop.tag == "King")
                            {
                                //if more than 1 enemy pawn on board
                                if (gameManager.BluePawnCount > 1)
                                {
                                    //Debug.Log("cannot attack the king as the pawns are alive."); //change so message appears in UI
                                    //Undeclare the enemy gameobject and enemy script
                                    enemySelectedTroop = null;
                                    enemyTroopScript   = null;
                                }

                                else if (gameManager.BluePawnCount <= 1)
                                {
                                    tank = gameManager.SearchList(gameManager.BlueTeam, "Tank");     //search list to see if tank is alive

                                    //if tank is not null/is alive
                                    if (tank != null && enemySelectedTroop.tag != "Tank")
                                    {
                                        //play tank animation & ask player if we wants to swap
                                        UIManager.TankQuestion(enemySelectedTroop.tag.ToString());
                                        awaitingInput = true;     //waiting for players input.
                                    }

                                    //if tank is not alive
                                    else
                                    {
                                        //attack animation either here or in calculate damage
                                        CalculateDamage();     //call attack and calculate damage
                                                               //gameManager.ChangePhase();
                                    }
                                }
                            }

                            else
                            {
                                tank = gameManager.SearchList(gameManager.BlueTeam, "Tank");     //search list to see if tank is alive

                                //if tank is not null/is alive
                                if (tank != null && enemySelectedTroop.tag != "Tank")
                                {
                                    //play tank animation & ask player if we wants to swap
                                    //REMOVE Debug.Log("Do you want to swap your tanks position with target? Y/N"); //change so it appears un UI
                                    UIManager.TankQuestion(enemySelectedTroop.tag.ToString());
                                    awaitingInput = true;     //waiting for players input.
                                }

                                //if target troop is knight
                                else if (enemySelectedTroop.tag == "Knight")
                                {
                                    KnightBlock();
                                }

                                //if tank is not alive
                                else
                                {
                                    CalculateDamage();
                                }
                            }
                        }

                        else if (SelectedTroop.transform.position.x != enemySelectedTroop.transform.position.x && SelectedTroop.transform.position.z != enemySelectedTroop.transform.position.z)
                        {
                            //Undeclare the enemy gameobject and enemy script
                            enemySelectedTroop = null;
                            enemyTroopScript   = null;
                        }
                    }

                    else
                    {
                        //if enemy is the king
                        if (enemySelectedTroop.tag == "King")     //REMEMBER IF KING DIES THEN GAME OVER
                        {
                            //if more than 1 enemy pawn on board
                            if (gameManager.BluePawnCount > 1)
                            {
                                //Debug.Log("cannot attack the king as the pawns are alive."); //change so message appears in UI
                                //Undeclare the enemy gameobject and enemy script
                                enemySelectedTroop = null;
                                enemyTroopScript   = null;
                            }

                            //if 1 or less enemy pawn on the board
                            else if (gameManager.BluePawnCount <= 1)
                            {
                                bool inRange = CheckRange();     //check is enemy troop is in attack range; return bool

                                //if return true
                                if (inRange == true)
                                {
                                    tank = gameManager.SearchList(gameManager.BlueTeam, "Tank");     //search list to see if tank is alive

                                    //if tank is not null/is alive
                                    if (tank != null && enemySelectedTroop.tag != "Tank")
                                    {
                                        //play tank animation & ask player if we wants to swap
                                        UIManager.TankQuestion(enemySelectedTroop.tag.ToString());
                                        awaitingInput = true;     //waiting for players input.
                                    }

                                    //if tank is not alive
                                    else
                                    {
                                        //if attacking troop is archer
                                        if (SelectedTroop.tag == "Archer")
                                        {
                                            //attack animation either here or in calculate damage
                                            CalculateArcherDamage();     //call archer unique attack and calculate damage
                                                                         //gameManager.ChangePhase();
                                        }
                                        else
                                        {
                                            //attack animation either here or in calculate damage
                                            CalculateDamage();     //call attack and calculate damage
                                                                   //gameManager.ChangePhase();
                                        }
                                    }
                                }

                                //if not in range
                                else if (inRange == false)
                                {
                                    //Undeclare the enemy gameobject and enemy script
                                    enemySelectedTroop = null;
                                    enemyTroopScript   = null;
                                }
                            }
                        }

                        //if king is not the target
                        else
                        {
                            Debug.Log("Test");
                            bool inRange = CheckRange();     //check is enemy troop is in attack range; return bool

                            //if return true
                            if (inRange == true)
                            {
                                tank = gameManager.SearchList(gameManager.BlueTeam, "Tank");     //search list to see if tank is alive

                                //if tank is not null/is alive
                                if (tank != null && enemySelectedTroop.tag != "Tank")
                                {
                                    //play tank animation & ask player if we wants to swap
                                    //REMOVE Debug.Log("Do you want to swap your tanks position with target? Y/N"); //change so it appears un UI
                                    UIManager.TankQuestion(enemySelectedTroop.tag.ToString());
                                    awaitingInput = true;     //waiting for players input.
                                }

                                //if tank is not alive
                                else
                                {
                                    //if target troop is knight
                                    if (enemySelectedTroop.tag == "Knight")
                                    {
                                        KnightBlock();
                                    }
                                    else
                                    {
                                        //if attacking troop is archer
                                        if (SelectedTroop.tag == "Archer")
                                        {
                                            //attack animation either here or in calculate damage
                                            CalculateArcherDamage();     //call archer unique attack and calculate damage
                                                                         //gameManager.ChangePhase();
                                        }
                                        else
                                        {
                                            //attack animation either here or in calculate damage
                                            CalculateDamage();     //call attack and calculate damage
                                                                   //gameManager.ChangePhase();
                                        }
                                    }
                                }
                            }

                            //if not in range
                            else if (inRange == false)
                            {
                                //Undeclare the enemy gameobject and enemy script
                                enemySelectedTroop = null;
                                enemyTroopScript   = null;
                            }
                        }
                    }
                }
            }
            break;

        case (Turn.Blue):
            //cast ray to mouse position
            enemyTroopRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (SelectedTroop.tag == "Priest")
            {
                //if ray hits a blue troop
                if (Physics.Raycast(enemyTroopRay, out enemyTroopHit, 100, BlueTroopMask))
                {
                    //store enemy gameobject and enemy script
                    enemySelectedTroop = enemyTroopHit.transform.gameObject;
                    enemyTroopScript   = enemySelectedTroop.GetComponent <TroopScript>();

                    if (enemySelectedTroop.tag != "Priest")
                    {
                        enemyTroopScript.ResetHealth();
                        animController.FaceEnemy(SelectedTroop, enemySelectedTroop);
                        enemyIndicator = false;
                    }
                }
            }

            else
            {
                //if ray hits a blue troop
                if (Physics.Raycast(enemyTroopRay, out enemyTroopHit, 100, RedTroopMask))
                {
                    //store enemy gameobject and enemy script
                    enemySelectedTroop = enemyTroopHit.transform.gameObject;
                    enemyTroopScript   = enemySelectedTroop.GetComponent <TroopScript>();

                    if (SelectedTroop.tag == "Mage")
                    {
                        if (SelectedTroop.transform.position.x == enemySelectedTroop.transform.position.x || SelectedTroop.transform.position.z == enemySelectedTroop.transform.position.z)
                        {
                            if (enemySelectedTroop.tag == "King")
                            {
                                Debug.Log("4.1 enemy selected troop: " + enemySelectedTroop);
                                Debug.Log("4.1 enemy troop script: " + enemyTroopScript);
                                //if more than 1 enemy pawn on board
                                if (gameManager.RedPawnCount > 1)
                                {
                                    //Debug.Log("cannot attack the king as the pawns are alive."); //change so message appears in UI
                                    //Undeclare the enemy gameobject and enemy script
                                    enemySelectedTroop = null;
                                    enemyTroopScript   = null;
                                    Debug.Log("calling");
                                }

                                else if (gameManager.RedPawnCount <= 1)
                                {
                                    tank = gameManager.SearchList(gameManager.RedTeam, "Tank");     //search list to see if tank is alive

                                    //if tank is not null/is alive
                                    if (tank != null && enemySelectedTroop.tag != "Tank")
                                    {
                                        //play tank animation & ask player if we wants to swap
                                        //REMOVE Debug.Log("Do you want to swap your tanks position with target? Y/N"); //change so it appears un UI
                                        UIManager.TankQuestion(enemySelectedTroop.tag.ToString());
                                        awaitingInput = true;     //waiting for players input.
                                    }

                                    //if tank is not alive
                                    else
                                    {
                                        //attack animation either here or in calculate damage
                                        CalculateDamage();     //call attack and calculate damage
                                                               //gameManager.ChangePhase();
                                    }
                                }
                            }

                            else
                            {
                                tank = gameManager.SearchList(gameManager.RedTeam, "Tank");     //search list to see if tank is alive

                                //if tank is not null/is alive
                                if (tank != null && enemySelectedTroop.tag != "Tank")
                                {
                                    //play tank animation & ask player if we wants to swap
                                    //REMOVE Debug.Log("Do you want to swap your tanks position with target? Y/N"); //change so it appears un UI
                                    UIManager.TankQuestion(enemySelectedTroop.tag.ToString());
                                    awaitingInput = true;     //waiting for players input.
                                }

                                //if target troop is knight
                                else if (enemySelectedTroop.tag == "Knight")
                                {
                                    KnightBlock();
                                }

                                else
                                {
                                    CalculateDamage();
                                }
                            }
                        }

                        else if (SelectedTroop.transform.position.x != enemySelectedTroop.transform.position.x && SelectedTroop.transform.position.z != enemySelectedTroop.transform.position.z)
                        {
                            //Undeclare the enemy gameobject and enemy script
                            enemySelectedTroop = null;
                            enemyTroopScript   = null;
                            Debug.Log("calling");
                        }
                    }
                    else
                    {
                        //if enemy is the king
                        if (enemySelectedTroop.tag == "King")     //REMEMBER IS KING DIES THEN GAME OVER
                        {
                            //if more than 1 enemy pawn on board
                            if (gameManager.RedPawnCount > 1)
                            {
                                Debug.Log("cannot attack the king as the pawns are alive.");     //change so message appears in UI
                                                                                                 //Undeclare the enemy gameobject and enemy script
                                enemySelectedTroop = null;
                                enemyTroopScript   = null;
                                Debug.Log("calling");
                            }

                            //if 1 or less enemy pawn on the board
                            else if (gameManager.RedPawnCount <= 1)
                            {
                                bool inRange = CheckRange();     //check is enemy troop if in attack range; return bool

                                //if return true
                                if (inRange == true)
                                {
                                    tank = gameManager.SearchList(gameManager.RedTeam, "Tank");     //search list to see if tank is alive

                                    //if tank is not null/is alive
                                    if (tank != null && enemySelectedTroop.tag != "Tank")
                                    {
                                        //play tank animation & ask player if we wants to swap
                                        //REMOVE Debug.Log("Do you want to swap your tanks position with target? Y/N"); //change so it appears un UI
                                        UIManager.TankQuestion(enemySelectedTroop.tag.ToString());
                                        awaitingInput = true;     //waiting for players input.
                                    }

                                    //if tank is not alive
                                    else
                                    {
                                        //if attacking troop is archer
                                        if (SelectedTroop.tag == "Archer")
                                        {
                                            //attack animation either here or in calculate damage
                                            CalculateArcherDamage();     //call archer unique attack and calculate damage
                                                                         //gameManager.ChangePhase();
                                        }
                                        else
                                        {
                                            //attack animation either here or in calculate damage
                                            CalculateDamage();     //call attack and calculate damage
                                                                   //gameManager.ChangePhase();
                                        }
                                    }
                                }

                                //if not in range
                                else if (inRange == false)
                                {
                                    //Undeclare the enemy gameobject and enemy script
                                    enemySelectedTroop = null;
                                    enemyTroopScript   = null;
                                    Debug.Log("calling");
                                }
                            }
                        }

                        //if king is not the target
                        else
                        {
                            Debug.Log("Test");
                            bool inRange = CheckRange();     //check if enemy troop is in attack range; return bool

                            //if return true
                            if (inRange == true)
                            {
                                tank = gameManager.SearchList(gameManager.RedTeam, "Tank");     //search list to see if tank is alive

                                //if tank is not null/is alive
                                if (tank != null && enemySelectedTroop.tag != "Tank")
                                {
                                    //play tank animation & ask player if we wants to swap
                                    //REMOVE Debug.Log("Do you want to swap your tanks position with target? Y/N"); //change so it appears un UI
                                    UIManager.TankQuestion(enemySelectedTroop.tag.ToString());
                                    awaitingInput = true;     //waiting for players input.
                                }

                                //if tank is not alive
                                else
                                {
                                    //if target troop is knight
                                    if (enemySelectedTroop.tag == "Knight")
                                    {
                                        KnightBlock();
                                    }
                                    else
                                    {
                                        //if attacking troop is archer
                                        if (SelectedTroop.tag == "Archer")
                                        {
                                            CalculateArcherDamage();     //call archer unique attack and calculate damage
                                        }
                                        else
                                        {
                                            Debug.Log(SelectedTroop);
                                            Debug.Log(enemySelectedTroop);
                                            CalculateDamage();     //call attack and calculate damage
                                        }
                                    }
                                }
                            }

                            //if not in range
                            else if (inRange == false)
                            {
                                //Undeclare the enemy gameobject and enemy script
                                enemySelectedTroop = null;
                                enemyTroopScript   = null;
                                Debug.Log("calling");
                            }
                        }
                    }
                }
            }
            break;

        default:
            break;
        }
    }