void getMultipleTilesFromCoords(int width, int height)//this method modifiesthe coords passed in to account for the  ring of walkable tiles
 //we need around it and gets the coords from the grid generator
 {
     try//uses mouse position and building dimensions to calculate the tiles to get and then gets them from the grid generator
     {
         width  += 2;
         height += 2;//adding 2 to account for edge around building
         GameObject tileAtMousePoint = null;
         selectionRaycast(ref tileAtMousePoint);
         TileMasterClass tm             = tileAtMousePoint.GetComponent <TileMasterClass>();//throws an error when the mouse is not over a valid tile
         Vector2         tileGridCoords = tm.getGridCoords();
         if (isSelectionInGridRange(tileGridCoords, width, height))
         {
             Debug.Log("Enough Space");
             Vector2 startPos = new Vector2(tileGridCoords.x - (width / 2), tileGridCoords.y - (height / 2));
             Vector2 endPos   = new Vector2(tileGridCoords.x + (width / 2), tileGridCoords.y + (height / 2));
             setSelected(GridGenerator.me.getTiles(startPos, endPos), true);
         }
         else
         {
             clearSelected();
             Debug.Log("Not enough space");
         }
     }
     catch
     {
         Debug.Log("No tile is at mouse position");
     }
 }
        //get tiles multiples times to add them in to a list and checks if there is enough space or a null object(no tile) at the coords we want to place our building
        public void getMultipleTilesFromCoords(Vector3 mousePos, int width, int height)
        {
            try {
                //leave space between building for units to move around
                width  += 2;
                height += 2;
                GameObject tileAtMousePoint = null;
                tileAtMousePoint = GridGenerator.me.getTile((int)mousePos.x, (int)mousePos.y).gameObject;
                TileMasterClass tm             = tileAtMousePoint.GetComponent <TileMasterClass> ();
                var             tileGridCoords = tm.getGridCoords();

                if (isSelectionInGridRange(tileGridCoords, width, height) == true)
                {
                    var startPos = new Vector2(tileGridCoords.x - (width / 2), tileGridCoords.y - (height / 2));
                    var endPos   = new Vector2(tileGridCoords.x + (width / 2), tileGridCoords.y + (height / 2));
                    setSelected(GridGenerator.me.getTiles(startPos, endPos), true);
                }
                else
                {
                    clearSelected();
                    OnErrorOccured("Not enough space");
                }
            }
            catch {
                clearSelected();
                OnErrorOccured("No tile at mouse position");
            }
        }
示例#3
0
 //starting the movement action and setting the tile we were to walkable again
 public override void doAction()
 {
     um         = this.GetComponent <UnitMovement> ();
     targetNode = GridGenerator.me.getTile((int)um.positionWeAreAt.x, (int)um.positionWeAreAt.y);
     targetNode.setTileWalkable(true);
     targetNode.setTileStandable(true);
     um.moveToLocation(positionWeAreMovingTo);
 }
示例#4
0
    void Start()
    {
        TileMasterClass tm = new TileMasterClass();

        tm.setGridCoords(new Vector2(transform.position.x, transform.position.y));

        setTileNearMe(tm);
    }
示例#5
0
 //set every changed tile to the previous position
 public override void cleanUp()
 {
     targetNode = GridGenerator.me.getTile((int)um.positionWeAreAt.x, (int)um.positionWeAreAt.y);
     targetNode.setTileWalkable(false);
     targetNode.setTileStandable(false);
     targetNode = GridGenerator.me.getTile((int)positionWeAreMovingTo.x, (int)positionWeAreMovingTo.y);
     targetNode.setTileStandable(true);
 }
    bool isCurrentLocAValidForConstruction()//basicly goes through the selection of tiles and checks if they are a valid place to build a building
    {
        if (currentlySelected.Count <= 0)
        {
            return(false);//there is not a tile at the mouse cursor for the script to use
        }

        //for building to be placed at a location there has to be 2 conditions met all the tiles must be walkable&
        //&the building must have a ring of walkable tiles around it.

        foreach (GameObject tile in currentlySelected)
        {
            TileMasterClass tm = tile.GetComponent <TileMasterClass>();
            if (tm.isTileWalkable() == false)
            {
                return(false);
            }
        }

        int xLowBound  = (int)currentlySelected[0].GetComponent <TileMasterClass>().getGridCoords().x;
        int xHighBound = (int)currentlySelected[currentlySelected.Count - 1].GetComponent <TileMasterClass>().getGridCoords().x;
        int yLowBound  = (int)currentlySelected[0].GetComponent <TileMasterClass>().getGridCoords().y;
        int yHighBound = (int)currentlySelected[currentlySelected.Count - 1].GetComponent <TileMasterClass>().getGridCoords().y;

        for (int x = 0; x < currentlySelected.Count - 1; x++)
        {
            GameObject      tile = currentlySelected[x];
            TileMasterClass tm   = tile.GetComponent <TileMasterClass>();

            Vector2 curTileGrid = tm.getGridCoords();
            if (curTileGrid.x == xLowBound || curTileGrid.x == xHighBound)
            {
                if (curTileGrid.y == yLowBound || curTileGrid.y == yHighBound)
                {
                    if (tm.isTileWalkable() == false)
                    {
                        return(false);//make sure that all edge tiles are walkable
                    }
                }
            }

            if (curTileGrid.y == yLowBound || curTileGrid.y == yHighBound)
            {
                if (curTileGrid.x == xLowBound || curTileGrid.x == xHighBound)
                {
                    if (tm.isTileWalkable() == false)
                    {
                        return(false);//make sure that all edge tiles are walkable
                    }
                }
            }
        }

        return(true);
    }
示例#7
0
        //gets the distance between two grid coords and returns them multiplied
        int GetDistance(TileMasterClass nodeA, TileMasterClass nodeB)
        {
            var dstX = Mathf.Abs((int)nodeA.getGridCoords().x - (int)nodeB.getGridCoords().x);
            var dstY = Mathf.Abs((int)nodeA.getGridCoords().y - (int)nodeB.getGridCoords().y);

            if (dstX > dstY)//to make sure that the final number is positive
            {
                return(14 * dstY + 10 * (dstX - dstY));
            }
            return(14 * dstX + 10 * (dstY - dstX));
        }
示例#8
0
    int GetDistance(TileMasterClass nodeA, TileMasterClass nodeB)
    //gets the distance between two grid coords and returns them multiplied
    {
        int dstX = Mathf.Abs((int)nodeA.getGridCoords().x - (int)nodeB.getGridCoords().x);
        int dstY = Mathf.Abs((int)nodeA.getGridCoords().y - (int)nodeB.getGridCoords().y);

        if (dstX > dstY)//to make that  the final number is positive
        {
            return(14 * dstY + 10 * (dstX - dstY));
        }
        return(14 * dstX + 10 * (dstY - dstX));
    }
示例#9
0
    public void setTileNearMe(TileMasterClass tm)//returns a tile that is walkable near the front of the building(nagative on y axis)
    {
        int x = (int)tm.getGridCoords().x;
        int y = (int)tm.getGridCoords().y;

        int mod = (heightInTiles / 2) + 1;

        y -= mod;

        tileNearest = GridGenerator.me.getTile(x, y);

        Debug.Log("TILE NEAREST: " + tileNearest.name);
    }
示例#10
0
        //goes through the path via the parent variable and puts it in a list
        void RetracePath(TileMasterClass startNode, TileMasterClass targetNode, ref List <TileMasterClass> store)
        {
            List <TileMasterClass> path = new List <TileMasterClass>();
            var currentNode             = targetNode;

            while (currentNode != startNode)
            {
                path.Add(currentNode);
                currentNode = currentNode.getParent();
            }
            path.Reverse();//have to reverse it because it if currently finish to start
            store = path;
        }
示例#11
0
    void RetracePath(TileMasterClass startNode, TileMasterClass targetNode, ref List <TileMasterClass> store)//goes through the path via the parent variable and puts it in a list
    {
        List <TileMasterClass> path        = new List <TileMasterClass>();
        TileMasterClass        currentNode = targetNode;

        while (currentNode != startNode)
        {
            path.Add(currentNode);
            currentNode = currentNode.getParent();
        }
        path.Reverse();//have to reverse it because it traces the path from finish to start  using the parent variable
        store = path;
    }
示例#12
0
 //check if the movement action is completed and if it is made the tile we are on to unwalkable to prevent soldiers stacking on top of each other and prevent placing buildings on top of soldiers
 public override bool getIsActionComplete()
 {
     if (Vector3.Distance(positionWeAreMovingTo, this.transform.position) < 2.0f)
     {
         TileMasterClass targetNode = GridGenerator.me.getTile((int)positionWeAreMovingTo.x, (int)positionWeAreMovingTo.y);
         targetNode.setTileWalkable(false);
         um = this.GetComponent <UnitMovement> ();
         um.positionWeAreAt = positionWeAreMovingTo;
         return(true);
     }
     else
     {
         return(false);
     }
 }
    void createBuildingAtLocation(Vector3 cursorPos, int width, int height)//creates a building from the one currently selected at the mouse position
    {
        //gets the lowets and highest of the each axis, works cause the selected tiles are in increasing order
        int xLowBound  = (int)currentlySelected[0].GetComponent <TileMasterClass>().getGridCoords().x;
        int xHighBound = (int)currentlySelected[currentlySelected.Count - 1].GetComponent <TileMasterClass>().getGridCoords().x;
        int yLowBound  = (int)currentlySelected[0].GetComponent <TileMasterClass>().getGridCoords().y;
        int yHighBound = (int)currentlySelected[currentlySelected.Count - 1].GetComponent <TileMasterClass>().getGridCoords().y;

        for (int x = 0; x < currentlySelected.Count - 1; x++)//goes through all the tiles and makes the ones not on the outside(where the actual building will be placed unwalkable)
        {
            GameObject      tile = currentlySelected[x];
            TileMasterClass tm   = tile.GetComponent <TileMasterClass>();

            Vector2 curTileGrid = tm.getGridCoords();
            if (curTileGrid.x == xLowBound || curTileGrid.x == xHighBound)
            {
                if (curTileGrid.y == yLowBound || curTileGrid.y == yHighBound)
                {
                    Debug.Log("Keeping" + tile.name + " Walkable");
                }
            }
            else if (curTileGrid.y == yLowBound || curTileGrid.y == yHighBound)
            {
                if (curTileGrid.x == xLowBound || curTileGrid.x == xHighBound)
                {
                    Debug.Log("Keeping" + tile.name + " Walkable");
                }
            }
            else
            {
                tm.setTileWalkable(false);
            }
        }


        //this code creates the building
        Vector3        spawnPos = currentlySelected[(currentlySelected.Count - 1) / 2].transform.position;
        GameObject     built    = (GameObject)Instantiate(AssignMethodToInventoryItems.me.getToBuild(), spawnPos, Quaternion.Euler(0, 0, 0));
        SpriteRenderer sr       = built.AddComponent <SpriteRenderer>();

        sr.sprite       = built.GetComponent <Building>().buildingSprite;
        sr.sortingOrder = 10;
        built.AddComponent <BoxCollider2D>();
        built.SetActive(true);
        clearSelected();
    }
示例#14
0
        public override void initaliseLocation(Vector3 position)
        {
            //checking if the position is on the grid
            edgeChecker(ref position);
            //if the chosen walkable tile got surrounded by unwalkable tiles with no way in that gives an argument out of range error!!!
            targetNode = GridGenerator.me.getTile((int)position.x, (int)position.y);

            while (!targetNode.isTileWalkable() || !targetNode.hasNoUnitOnIt())
            {
                //adjust the tile to a new close one
                position.x += Random.Range(-1, 2);
                position.y += Random.Range(-1, 2);
                edgeChecker(ref position);
                targetNode = GridGenerator.me.getTile((int)position.x, (int)position.y);
            }
            positionWeAreMovingTo = position;
            targetNode.setTileStandable(false);
        }
示例#15
0
        //get the neighbors of the tile passed in as a list
        public List <TileMasterClass> getTileNeighbors(TileMasterClass Tile)
        {
            List <TileMasterClass> retVal = new List <TileMasterClass>();
            TileMasterClass        t      = Tile;

            var pos = t.getGridCoords();

            //passed in tile is at left edge
            if (pos.x == 0)
            {
                if (pos.y == 0)
                {
                    //passed in tile is at bottom left
                    retVal.Add(gridOfTiles[(int)pos.x + 1, (int)pos.y]);
                    retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y + 1]);
                }
                else if (pos.y == gridDimensions.y - 1)
                {
                    //passed in tile is at top left
                    retVal.Add(gridOfTiles[(int)pos.x + 1, (int)pos.y]);
                    retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y - 1]);
                }
                else
                {
                    retVal.Add(gridOfTiles[(int)pos.x + 1, (int)pos.y]);
                    retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y + 1]);
                    retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y - 1]);
                }

                //passed in tile is at right edge
            }
            else if (pos.x == gridDimensions.x - 1)
            {
                if (pos.y == 0)
                {
                    //passed in tile is at  bottom right y
                    retVal.Add(gridOfTiles[(int)pos.x - 1, (int)pos.y]);
                    retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y + 1]);
                }
                else if (pos.y == gridDimensions.y - 1)
                {
                    //passed in tile is at top right y
                    retVal.Add(gridOfTiles[(int)pos.x - 1, (int)pos.y]);
                    retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y - 1]);
                }
                else
                {
                    retVal.Add(gridOfTiles[(int)pos.x - 1, (int)pos.y]);
                    retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y + 1]);
                    retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y - 1]);
                }
            }
            else
            {
                //tile position x is not on the edge
                if (pos.y == 0)
                {
                    //passed in tile is at edge left y
                    retVal.Add(gridOfTiles [(int)pos.x - 1, (int)pos.y]);
                    retVal.Add(gridOfTiles [(int)pos.x, (int)pos.y + 1]);
                    retVal.Add(gridOfTiles [(int)pos.x + 1, (int)pos.y]);
                }
                else if (pos.y == gridDimensions.y - 1)
                {
                    //passed in tile is at edge right y
                    retVal.Add(gridOfTiles [(int)pos.x - 1, (int)pos.y]);
                    retVal.Add(gridOfTiles [(int)pos.x, (int)pos.y - 1]);
                    retVal.Add(gridOfTiles [(int)pos.x + 1, (int)pos.y]);
                }
                else
                {
                    //both x and y coordinates are not on the edge
                    retVal.Add(gridOfTiles [(int)pos.x - 1, (int)pos.y]);
                    retVal.Add(gridOfTiles [(int)pos.x, (int)pos.y - 1]);
                    retVal.Add(gridOfTiles [(int)pos.x + 1, (int)pos.y]);
                    retVal.Add(gridOfTiles [(int)pos.x, (int)pos.y + 1]);
                }
            }

            return(retVal);
        }
示例#16
0
 public void setParent(TileMasterClass val)
 {
     parent = val;
 }
示例#17
0
 public virtual void initialiseTile(TileMasterClass tm)
 {
 }
    public List <TileMasterClass> getTileNeighbors(TileMasterClass Tile)//gets passed in a tile and based on the coords in the grid it will get the neighboring tiles
    //and return them as a list
    {
        List <TileMasterClass> retVal = new List <TileMasterClass>();
        TileMasterClass        t      = Tile;

        Vector2 pos = t.getGridCoords();


        if (pos.x == 0)
        {
            if (pos.y == 0)//bottom left
            {
                retVal.Add(gridOfTiles[(int)pos.x + 1, (int)pos.y]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y + 1]);
            }
            else if (pos.y == gridDimensions.y - 1)//top left
            {
                retVal.Add(gridOfTiles[(int)pos.x + 1, (int)pos.y]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y - 1]);
            }
            else
            {
                retVal.Add(gridOfTiles[(int)pos.x + 1, (int)pos.y]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y + 1]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y - 1]);
            }
        }
        else if (pos.x == gridDimensions.x - 1)
        {
            if (pos.y == 0)//bottom right
            {
                retVal.Add(gridOfTiles[(int)pos.x - 1, (int)pos.y]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y + 1]);
            }
            else if (pos.y == gridDimensions.y - 1)//top right
            {
                retVal.Add(gridOfTiles[(int)pos.x - 1, (int)pos.y]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y - 1]);
            }
            else
            {
                retVal.Add(gridOfTiles[(int)pos.x - 1, (int)pos.y]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y + 1]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y - 1]);
            }
        }
        else
        {
            //grid not horizontal x
            if (pos.y == 0)//bottom right
            {
                retVal.Add(gridOfTiles[(int)pos.x - 1, (int)pos.y]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y + 1]);
                retVal.Add(gridOfTiles[(int)pos.x + 1, (int)pos.y]);
            }
            else if (pos.y == gridDimensions.y - 1)//top right
            {
                retVal.Add(gridOfTiles[(int)pos.x - 1, (int)pos.y]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y - 1]);
                retVal.Add(gridOfTiles[(int)pos.x + 1, (int)pos.y]);
            }
            else
            {
                retVal.Add(gridOfTiles[(int)pos.x - 1, (int)pos.y]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y - 1]);
                retVal.Add(gridOfTiles[(int)pos.x + 1, (int)pos.y]);
                retVal.Add(gridOfTiles[(int)pos.x, (int)pos.y + 1]);
            }
        }

        return(retVal);
    }
示例#19
0
    void getPath(Vector3 startPos, Vector3 endPos, ref List <TileMasterClass> store)
    {
        Vector2 sPos = new Vector2((int)startPos.x, (int)startPos.y);
        Vector2 ePos = new Vector2((int)endPos.x, (int)endPos.y);                        //converting positions to ints in order to get the rough tile coords from them

        TileMasterClass startNode  = GridGenerator.me.getTile((int)sPos.x, (int)sPos.y); //gets the two tiles as TileMasterClass
        TileMasterClass targetNode = GridGenerator.me.getTile((int)ePos.x, (int)ePos.y);

        if (startNode == null || targetNode == null || targetNode.isTileWalkable() == false)//if the destination is not a walkable tile then it breaks
        {
            Debug.Log("One of the tiles is not walkable");
            return;
        }

        List <TileMasterClass> openSet   = new List <TileMasterClass>(); //open set is tiles we want to check
        List <TileMasterClass> closedSet = new List <TileMasterClass>(); //closed set is tiles we have checked and dont want to include in the path

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            TileMasterClass node = openSet[0];

            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].fCost < node.fCost || openSet[i].fCost == node.fCost)//check to see if we can find a tile with lower or equal f cost
                //(approximation of distance to target from start including this tile)
                {
                    if (openSet[i].getH() < node.getH()) //if the tile has a lower distance to the target tile than the current one in "node"
                    {
                        node = openSet[i];               //sets node to be this closer tile
                    }
                }
            }


            openSet.Remove(node);
            closedSet.Add(node);
            if (node == targetNode)//checks to see if the node is the tile we want to go to, if so we return the path after is has been retraced
            {
                Debug.Log("finish the path");
                RetracePath(startNode, targetNode, ref store);
                return;
            }

            foreach (TileMasterClass neighbour in GridGenerator.me.getTileNeighbors(node))//goes through each of the neighbors of the node
            {
                if (!neighbour.isTileWalkable() || closedSet.Contains(neighbour) || neighbour == null || node == null)
                //if the neighbor is not accessable or the node is null, go onto the next neighbour
                {
                    continue;
                }

                int newCostToNeighbour = node.getG() + GetDistance(node, neighbour);  //calculates the cost of going to the neighbour from the start os the path
                if (newCostToNeighbour < node.getG() || !openSet.Contains(neighbour)) //if the cost is shorter and the open set doesnt contain the nieghbour
                {
                    neighbour.setG(newCostToNeighbour);
                    neighbour.setH(GetDistance(neighbour, targetNode));//sets the g and h values of the neighbour
                    neighbour.setParent(node);

                    if (!openSet.Contains(neighbour))//adds the neighbour to the openset if not already there
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
    }
示例#20
0
 public virtual void doAction(GameObject me, TileMasterClass tile)
 {
 }