Beispiel #1
0
    //create active selection / floating tower on pressing a tower button
    public void InstantiateTower(int clickedButton)
    {
        //store the TowerManager tower index passed in as parameter
        index = clickedButton;

        //we clicked one of these tower buttons
        //if we already have a floating tower, destroy it and free selections
        if (SV.selection)
        {
            currentGrid  = null;
            currentTower = null;
            Destroy(SV.selection);
        }

        //check if there are free grids left
        //no free grid left (list count is equal to grid count)
        if (gridScript.GridList.Count == gridScript.transform.childCount)
        {
            //print a warning message
            StartCoroutine("DisplayError", "No free grids left for placing a new tower!");
            Debug.Log("No free grids left for placing a new tower!");
            return;
        }

        //initialize price array with total count of resources
        float[] price = new float[GameHandler.resources.Length];
        //cache selected upgrade options for further processment
        UpgOptions opt = towerScript.towerUpgrade[index].options[0];

        //loop through resources
        //get needed resources (buy price) of this tower from upgrade list
        for (int i = 0; i < price.Length; i++)
        {
            price[i] = opt.cost[i];
        }

        //check in case we have not enough resources left, abort purchase
        for (int i = 0; i < price.Length; i++)
        {
            if (GameHandler.resources[i] < price[i])
            {
                StartCoroutine("DisplayError", "Not enough resources for buying this tower!");
                Debug.Log("Not enough resources for buying this tower!");
                //destroy selection. this is a bit hacky: CancelSelection() destroys all selections,
                //but we do want to keep our grid selection on BuildMode Grid, thus we cache it right
                //before calling this method and restore it afterwards
                if (SV.gridSelection)
                {
                    GameObject grid = SV.gridSelection;
                    CancelSelection(true);
                    SV.gridSelection      = grid;
                    grid.renderer.enabled = true;
                }
                else
                {
                    CancelSelection(true);
                }
                return;
            }
        }

        //all checks went through, we are able to purchase this tower
        //instantiate selected tower from TowerManager prefab list and thus create a floating tower
        SV.selection      = (GameObject)Instantiate(towerScript.towerPrefabs[index], SV.outOfView, Quaternion.identity);
        SV.selection.name = towerScript.towerNames[index];
        //get new base properties of this tower instance
        towerBase = SV.selection.GetComponentInChildren <TowerBase>();
        //change name of the gameobject holding this component to the defined one in TowerManager names list
        towerBase.gameObject.name = towerScript.towerNames[index];
        //parent tower to the container gameobject
        SV.selection.transform.parent = towerContainer;
        //get new upgrade properties of this tower instance
        upgrade           = SV.selection.GetComponentInChildren <Upgrade>();
        towerBase.upgrade = upgrade;
        //disable its base properties, so while moving/placing the tower around, it can not attack
        towerBase.enabled = false;
        //show all grid renderers to see where we could (or could not) place the tower
        //but only highlight all of them on BuildMode Tower (where gridSelection is always empty)
        if (!SV.gridSelection)
        {
            gridScript.ToggleVisibility(true);
        }
        //apply passive powerups to this tower
        PowerUpManager.ApplyToSingleTower(towerBase, upgrade);
    }
Beispiel #2
0
    //swaps the tower model to the one defined in the corresponding upgrade option
    public void TowerChange()
    {
        //instantiate new tower model at the same position and reparent it to the Tower Manager object
        GameObject newTower = (GameObject)Instantiate(options[curLvl].swapModel, transform.position, transform.rotation);

        newTower.transform.parent = GameObject.Find("Tower Manager").transform;
        //find the TowerBase component, we have to do some adjustments to it
        TowerBase newTowerBase = newTower.GetComponentInChildren <TowerBase>();
        //get the gameobject which holds the TowerBase script,
        //and rename it to keep the old name
        GameObject subTower = newTowerBase.gameObject;

        newTower.name = newTowerBase.gameObject.name = transform.name;

        //attach and store an Upgrade script to the actual tower object,
        //copy values from the old tower
        Upgrade newUpgrade = subTower.AddComponent <Upgrade>();

        newUpgrade.curLvl    = this.curLvl;
        newUpgrade.options   = this.options;
        newTowerBase.upgrade = newUpgrade;

        //apply passive powerups to new tower
        PowerUpManager.ApplyToSingleTower(newTowerBase, newUpgrade);

        //switch TowerBase component to active state - attacks enemies
        newTowerBase.enabled = true;
        newTowerBase.rangeInd.renderer.enabled = true;

        //abort shot automatism of the new tower,
        //in order to take the last shot time of the old tower into account
        newTowerBase.CancelInvoke("CheckRange");
        float lastShot    = towerScript.lastShot;
        float invokeInSec = options[curLvl].shootDelay + lastShot - Time.time;

        if (invokeInSec < 0)
        {
            invokeInSec = 0f;
        }
        newTowerBase.StartInvoke(invokeInSec);

        //find GUI component and assign new upgrade script,
        //we do this in order to update the upgrade gui tooltip for the new tower
        GUILogic gui = GameObject.Find("GUI").GetComponent <GUILogic>();

        gui.upgrade = newUpgrade;

        //signalize enemies that the old tower was despawned,
        //here we loop through all enemies in range and remove the old tower from them.
        //they detect the new tower automatically, since enemies are colliding with it.
        for (int i = 0; i < towerScript.inRange.Count; i++)
        {
            PoolManager.Props[towerScript.inRange[i].name].RemoveTower(towerScript);
        }

        //re-rotate turret to match the old turret, if there existed one,
        //else rotate turret to match the grid rotation
        if (newTowerBase.turret)
        {
            if (towerScript.turret)
            {
                newTowerBase.turret.rotation = towerScript.turret.rotation;
            }
            else
            {
                //define ray with down direction to get the grid beneath
                Ray        ray = new Ray(transform.position + new Vector3(0, 0.5f, 0), -transform.up);
                RaycastHit hit;

                //raycast downwards of the tower against our grid mask to get the grid
                if (Physics.Raycast(ray, out hit, 20, SV.gridMask))
                {
                    Transform grid = hit.transform;
                    newTowerBase.turret.rotation = grid.rotation;
                }
            }
            //don't let the tower/turret rotation affect shot position rotation
            newTowerBase.shotPos.localRotation = Quaternion.identity;

            //enable turret rotation mechanism
            newTowerBase.turret.GetComponent <TowerRotation>().enabled = true;
        }

        //destroy (this) old tower
        Destroy(transform.parent.gameObject);
    }