Beispiel #1
0
    private int getTileClearCost(EnvironmentTile tile)
    {
        //Get the cost to remove the tile or the sell price of the turret if tile is a turret

        //Decrement turret cost of the tile to get the price that was paid for it
        incrementTurretCost(tile, -1);
        Turret turret = tile.GetComponentInChildren <Turret>();
        int    cost;

        if (turret is MachineGun)
        {
            cost = (int)(-(machineGunCost + tile.costToRemove) * sellMoneyBackPercent);
        }
        else if (turret is Mortar)
        {
            cost = (int)(-(mortarCost + tile.costToRemove) * sellMoneyBackPercent);
        }
        else
        {
            cost = tile.costToRemove;
        }
        //Increment cost again to ensure turret cost stays the same
        incrementTurretCost(tile, 1);
        return(cost);
    }
Beispiel #2
0
    private void useDestroyTool()
    {
        //Remove a turret or obstacle when player clicks on it

        //Enable destroy icon
        Vector3 pos = MainCamera.ScreenToViewportPoint(Input.mousePosition);

        pos.x = (pos.x - 0.5f) * hudCanvas.sizeDelta.x;
        pos.y = (pos.y - 0.5f) * hudCanvas.sizeDelta.y;
        destroyIcon.localPosition = pos;
        destroyIcon.gameObject.SetActive(true);

        //Check if highlighted tile can be cleared
        if (currentTile.canBeDestroyed && !currentTile.IsAccessible)
        {
            //Get cost to remove tile and colour it red
            int cost = getTileClearCost(currentTile);
            setBuyText(cost);
            currentTile.GetComponent <ColorSwapper>().swapColour(Color.red);
            //If left click pressed
            if (Input.GetMouseButtonDown(0))
            {
                //Check if player can afford it
                if (cost <= money)
                {
                    //If player can afford it then update player money and clear the tile
                    buyItem(cost);
                    mMap.clearTile(currentTile);

                    incrementTurretCost(currentTile, -1);

                    Turret turret = currentTile.GetComponentInChildren <Turret>();
                    if (turret != null)
                    {
                        turrets.Remove(turret);
                    }

                    //Re calculate enemy route
                    foreach (Spawner spawner in spawners)
                    {
                        spawner.route = mMap.Solve(spawner.spawnExitPoint, mMap.houseEntrance);
                    }
                }
                else
                {
                    sendMessage("Can't afford");
                }
            }
        }
    }
Beispiel #3
0
    private int getTurretCost(EnvironmentTile tile)
    {
        //get the cost to build a turret

        Turret turret = tile.GetComponentInChildren <Turret>();

        if (turret is MachineGun)
        {
            return(machineGunCost);
        }
        else if (turret is Mortar)
        {
            return(mortarCost);
        }

        throw new System.Exception("No turret on selected tile");
    }
Beispiel #4
0
    private void incrementTurretCost(EnvironmentTile tile, int increment)
    {
        //Increase or decrease cost of turrets by increment

        Turret turret = tile.GetComponentInChildren <Turret>();

        if (turret != null)
        {
            if (turret is MachineGun)
            {
                numberOfMachineGuns    += increment;
                machineGunCost          = (int)((float)machineGunBaseCost * Mathf.Pow(machineGunIncrement, numberOfMachineGuns));
                machineGunCostText.text = "$" + machineGunCost.ToString();
            }
            else if (turret is Mortar)
            {
                numberOfMortars    += increment;
                mortarCost          = (int)((float)mortarBaseCost * Mathf.Pow(mortarIncrement, numberOfMortars));
                mortarCostText.text = "$" + mortarCost.ToString();
            }
        }
    }
Beispiel #5
0
    private void usePlaceTool()
    {
        //Place a turret down on the selected tile if possible

        //Show rotate icon
        Vector3 pos = MainCamera.ScreenToViewportPoint(Input.mousePosition);

        pos.x = (pos.x - 0.5f) * hudCanvas.sizeDelta.x;
        pos.y = (pos.y - 0.5f) * hudCanvas.sizeDelta.y;
        rotateIcon.localPosition = pos;
        rotateIcon.gameObject.SetActive(true);

        //Enable the turret preview object
        objectToPlace.SetActive(true);
        objectToPlace.transform.position = currentTile.Position;
        //Check if the tile is accessible
        if (currentTile.IsAccessible && currentTile.canBeDestroyed)
        {
            //set preview object to green if it is valid
            objectToPlace.GetComponent <ColorSwapper>().swapColour(Color.green);
            Turret turret     = tilePrefab.GetComponentInChildren <Turret>();
            int    turretCost = 0;
            //Set the cost of the turret
            if (turret != null)
            {
                turretCost = getTurretCost(tilePrefab);
                setBuyText(turretCost);
            }

            //If left click pressed
            if (Input.GetMouseButtonDown(0))
            {
                //If player can afford it
                if (turretCost <= money)
                {
                    //Swap tile for turret
                    currentTile = mMap.swapTile(currentTile, tilePrefab, true, false);
                    currentTile.costToRemove = 0;
                    currentTile.transform.GetChild(0).transform.Rotate(new Vector3(0, 1, 0), 90 * tileRotation);

                    //Check if tile position is valid
                    if (mMap.checkIfHouseAccesible())
                    {
                        //If position is valid then set the turret up and recalculate enemy routes
                        turret = currentTile.GetComponentInChildren <Turret>();

                        if (turret != null)
                        {
                            turret.setSpawners(spawners);
                            turret.setGame(this);

                            int cost = getTurretCost(currentTile);
                            buyItem(cost);
                            incrementTurretCost(currentTile, 1);

                            turrets.Add(turret);
                            showRange(turret, false);
                        }
                        foreach (Spawner spawner in spawners)
                        {
                            spawner.route = mMap.Solve(spawner.spawnExitPoint, mMap.houseEntrance);
                        }
                    }
                    else
                    {
                        //If position isn't valid then reset the tile
                        sendMessage("Can't block path");
                        mMap.clearTile(currentTile);
                    }
                }
                else
                {
                    //If player can't afford then send error message
                    sendMessage("Can't afford");
                }
            }
        }
        else
        {
            //Set preview object to red if not a valid position
            objectToPlace.GetComponent <ColorSwapper>().swapColour(Color.red);
        }
    }
    // Make a specific tile inaccessible (at point x,y and of specific type)
    // Types (more for possible expansion):
    // 0 = rocks
    // 1 = stone
    // 2 = trees
    // 3 = logs
    // 4 = water
    // 5 = player base
    // 6 = enemy base
    public void MakeTileInaccessible(int x, int y, int type)
    {
        // Get transform pos of where to make inaccessible
        finalPosition = mMap[x][y].transform.position;

        // Create new tile initially to the first inaccessible tile type in list
        EnvironmentTile prefabNa = InaccessibleTiles[0];

        // Define what type of inaccessible tile this will be
        switch (type)
        {
        case 0: prefabNa = InaccessibleTiles[Random.Range(4, 10)]; break;       // Rocks

        case 1: prefabNa = InaccessibleTiles[Random.Range(10, 16)]; break;      // Stone

        case 2: prefabNa = InaccessibleTiles[Random.Range(2, 4)]; break;        // Trees

        case 3: prefabNa = InaccessibleTiles[Random.Range(0, 2)]; break;        // Logs

        case 4: prefabNa = InaccessibleTiles[16]; break;                        // Water

        case 5: prefabNa = InaccessibleTiles[17]; break;                        // Player Base

        case 6: prefabNa = InaccessibleTiles[18]; break;                        // Enemy Base
        }

        // Get reference to tile on map
        var t = mMap[x][y];

        // Change mesh of tile to be the type of inaccessible tile
        t.GetComponent <MeshFilter>().sharedMesh  = prefabNa.GetComponent <MeshFilter>().sharedMesh;
        t.GetComponent <MeshRenderer>().materials = prefabNa.GetComponent <MeshRenderer>().sharedMaterials;

        // Using rocks and player/enemy base objects, requires instantiation of extra object (e.g the actual rock)
        if (type == 0 || type == 5 || type == 6)
        {
            Instantiate(prefabNa.GetComponentInChildren <MeshFilter>(), finalPosition, mMap[x][y].transform.rotation, t.transform);
        }

        // Make this tile not accessible
        t.IsAccessible = false;

        // Define tile type string and health (if appropriate)
        switch (type)
        {
        case 0: t.Type = "rock";
            t.Health   = 100; break;

        case 1: t.Type = "stone";
            t.Health   = 100; break;

        case 2: t.Type = "tree";
            t.Health   = 100; break;

        case 3: t.Type = "logs";
            t.Health   = 100; break;

        case 4: t.Type = "water"; break;

        case 5: t.Type = "player base"; break;

        case 6: t.Type = "enemy base"; break;

        default: t.Type = "error"; break;
        }

        // Replace tile in the map tile list
        if (mAll.FindIndex(ind => ind.Equals(mMap[x][y])) != -1)
        {
            mAll[mAll.FindIndex(ind => ind.Equals(mMap[x][y]))] = t;
        }

        // Change this tile to be the new inaccessible tile
        mMap[x][y] = t;
    }