Exemplo n.º 1
0
        /// <summary>
        /// Try to place a building on the tiles. Some buildings require more than one tile, so you can't place them unless all the tiles they sit on are free
        /// </summary>
        public void PlaceBuilding()
        {
            //Stop responding to navigation events ( for 4.6+ UI )
            if (useOldUI == false)
            {
                EventSystem.current.sendNavigationEvents = false;
            }

            Building currentBuilding = currentBuild.GetComponent <Building>();

            Transform[] freeTiles = new Transform[(int)currentBuilding.buildingSize.x * (int)currentBuilding.buildingSize.y];

            int tileCounter = 0;

            //Go through all the tiles that this building should sit on, and make sure they are all free
            for (int tileIndexX = 0; tileIndexX < currentBuilding.buildingSize.x; tileIndexX++)
            {
                for (int tileIndexY = 0; tileIndexY < currentBuilding.buildingSize.y; tileIndexY++)
                {
                    // Create a raycast from the camera to the mouse position and check collision with a list of allowed tiles ( The raycast checks only the layers on the allowedTiles list )
                    if (Physics.Raycast(pointer.position + new Vector3(tileIndexY, 0, tileIndexX), -Vector3.up, out hit, 100, currentBuild.GetComponent <Building>().allowedTiles))
                    {
                        Tile     hitTile  = hit.collider.GetComponent <Tile>();
                        Building building = currentBuild.GetComponent <Building>();

                        // Allow the building to be placed only if there is no building on the tile OR if this building is stackable and the tile has no stackable object on it
                        if (hitTile.building == null || (hitTile.stackable == null && building.stackable == true))
                        {
                            freeTiles[tileCounter] = hit.collider.transform;

                            tileCounter++;
                        }
                        else                         //We have an error because we tried to place the selected building on an occupied tile
                        {
                            error = true;

                            currentBuild.gameObject.SetActive(false);

                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }

            //If we passed this point, then all required tiles are free, and we can place the building

            error = false;             // There is no error

            // Place the building on the correct tile
            currentBuild.position = freeTiles[0].position;
            currentBuild.gameObject.SetActive(true);

            // If we click the Left Mouse Button, place the building on the tile and reduce from the player's money, and activate the building
            if (Input.GetButtonDown("Fire1"))
            {
                //Start responding to navigation events ( for 4.6+ UI )
                if (useOldUI == false)
                {
                    EventSystem.current.sendNavigationEvents = true;
                }

                // Assign this building to the correct slot in the tile script wether it's a building or a stackable building
                for (int tileCounterIndex = 0; tileCounterIndex < tileCounter; tileCounterIndex++)
                {
                    if (currentBuilding.stackable == true)
                    {
                        freeTiles[tileCounterIndex].GetComponent <Tile>().stackable = currentBuild;
                    }
                    else
                    {
                        freeTiles[tileCounterIndex].GetComponent <Tile>().building = currentBuild;
                    }
                }

                if (currentBuilding.moneyCosts.Length > 0 && moneyTypes.Length > 0)
                {
                    foreach (var resourceCost in currentBuilding.moneyCosts)
                    {
                        CheckResourceList(resourceCost.moneyObject.GetComponent <Money>().resourceName, resourceCost.cost, true);
                    }
                }
                else
                {
                    // Reduce from the player's money
                    money -= currentBuilding.price;
                }

                // Activate the main components of the building
                MoneyMaker moneyMaker = currentBuild.GetComponent <MoneyMaker>();

                if (moneyMaker)
                {
                    moneyMaker.enabled = true;
                }

                Weapon weapon = currentBuild.GetComponent <Weapon>();

                if (weapon)
                {
                    weapon.enabled = true;
                }

                if (currentBuild.GetComponent <Collider>())
                {
                    currentBuild.GetComponent <Collider>().enabled = true;
                }

                // Reset the cooldown counter to 0
                cooldownCount[currentIndex] = 0;

                // If the building has a build animation, play it
                if (currentBuild.GetComponent <Animation>())
                {
                    currentBuild.GetComponent <Animation>().Play();
                }

                // If the building has an audio, play it
                if (currentBuild.GetComponent <AudioSource>())
                {
                    currentBuild.GetComponent <AudioSource>().Play();
                }

                // Reset the building selection
                currentBuild = null;

                if (soundBuild)
                {
                    GetComponent <AudioSource>().PlayOneShot(soundBuild);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Picks a building from the build list, after checking if the cooldown is 0, and we have enough resources to build it
        /// </summary>
        /// <param name="buildingIndex">Building index</param>
        public void PickBuilding(int buildingIndex)
        {
            // If we have enough money, and the cooldown timer for the building is done, allow this building to be selected
            if (cooldownCount[buildingIndex] >= cooldown[buildingIndex])
            {
                if (buildList[buildingIndex].GetComponent <Building>().moneyCosts.Length > 0 && moneyTypes.Length > 0)
                {
                    // A value to check if we meet all the resource costs
                    int    moneyCostsCheck = 0;
                    Cost[] buildingCosts   = buildList[buildingIndex].GetComponent <Building>().moneyCosts;

                    foreach (Cost resourceCost in buildingCosts)
                    {
                        if (CheckResourceList(resourceCost.moneyObject.GetComponent <Money>().resourceName, resourceCost.cost, false))
                        {
                            moneyCostsCheck++;
                        }
                    }

                    if (moneyCostsCheck == buildingCosts.Length)
                    {
                        // If we already have a selected building, remove it
                        if (currentBuild)
                        {
                            Destroy(currentBuild.gameObject);
                        }

                        // Set the new building as the selected one, and put it offscreen
                        currentBuild = (Transform)Instantiate(buildList[buildingIndex], new Vector3(100, 100, 100), Quaternion.identity);

                        MoneyMaker moneyMaker = currentBuild.GetComponent <MoneyMaker>();

                        // Deactivate the main components of the building
                        if (moneyMaker)
                        {
                            moneyMaker.enabled = false;
                        }

                        Weapon weapon = currentBuild.GetComponent <Weapon>();

                        if (weapon)
                        {
                            weapon.enabled = false;
                        }

                        if (currentBuild.GetComponent <Collider>())
                        {
                            currentBuild.GetComponent <Collider>().enabled = false;
                        }

                        // Set the current building index
                        currentIndex = buildingIndex;

                        // If the building has a collider, remove it so we don't collide with objects in the game ( An enemy, or another object )
                        if (currentBuild.GetComponent <Collider>())
                        {
                            currentBuild.GetComponent <Collider>().enabled = false;
                        }

                        sell = false;

                        if (this.GetComponent <AudioSource>())
                        {
                            this.GetComponent <AudioSource>().Play();
                        }
                    }
                }
                else if (money - buildList[buildingIndex].GetComponent <Building>().price >= 0)
                {
                    // If we already have a selected building, remove it
                    if (currentBuild)
                    {
                        Destroy(currentBuild.gameObject);
                    }

                    // Set the new building as the selected one, and put it offscreen
                    currentBuild = (Transform)Instantiate(buildList[buildingIndex], new Vector3(100, 100, 100), Quaternion.identity);

                    // Deactivate the main components of the building
                    MoneyMaker moneyMaker = currentBuild.GetComponent <MoneyMaker>();

                    if (moneyMaker)
                    {
                        moneyMaker.enabled = false;
                    }

                    Weapon weapon = currentBuild.GetComponent <Weapon>();

                    if (weapon)
                    {
                        weapon.enabled = false;
                    }

                    if (currentBuild.GetComponent <Collider>())
                    {
                        currentBuild.GetComponent <Collider>().enabled = false;
                    }

                    // Set the current building index
                    currentIndex = buildingIndex;

                    // If the building has a collider, remove it so we don't collide with objects in the game ( An enemy, or another object )
                    if (currentBuild.GetComponent <Collider>())
                    {
                        currentBuild.GetComponent <Collider>().enabled = false;
                    }

                    sell = false;

                    if (this.GetComponent <AudioSource>())
                    {
                        this.GetComponent <AudioSource>().Play();
                    }
                }
            }
        }