bool isLocationValidForConstruction()
    {
        if (current.Count <= 0)
        {
            return(false);
        }


        foreach (GameObject tile in current)
        {
            TileMaster tm = tile.GetComponent <TileMaster>();
            if (tm.isWalkable() == false)
            {
                return(false);
            }
        }

        int xLowBound  = (int)current[0].GetComponent <TileMaster>().getCoords().x;                 //(int)cursorPos.x - ((width + 0)/2);
        int xHighBound = (int)current[current.Count - 1].GetComponent <TileMaster>().getCoords().x; //(int)cursorPos.x + ((width + 0)/2);
        int yLowBound  = (int)current[0].GetComponent <TileMaster>().getCoords().y;                 //(int)cursorPos.y - ((height + 0) / 2);
        int yHighBound = (int)current[current.Count - 1].GetComponent <TileMaster>().getCoords().y; //(int)cursorPos.y + ((height + 0) / 2);

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

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

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

        return(true);
    }
Exemple #2
0
    void getPath(Vector3 startPos, Vector3 endPos, ref List <TileMaster> store)
    {
        Vector2 sPos = new Vector2((int)startPos.x, (int)startPos.y);
        Vector2 ePos = new Vector2((int)endPos.x, (int)endPos.y);

        TileMaster startNode  = MapGenerator.me.getTile((int)sPos.x, (int)sPos.y);
        TileMaster targetNode = MapGenerator.me.getTile((int)ePos.x, (int)ePos.y);

        if (startNode == null || targetNode == null || targetNode.isWalkable() == false)
        {
            Debug.Log("not walkable");
            return;
        }

        List <TileMaster> openSet   = new List <TileMaster>(); //tiles to check
        List <TileMaster> closedSet = new List <TileMaster>(); //checked

        openSet.Add(startNode);

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

            for (int i = 1; i < openSet.Count; ++i)
            {
                if (openSet[i].fCost < node.fCost || openSet[i].fCost == node.fCost)
                {
                    if (openSet[i].getH() < node.getH())
                    {
                        node = openSet[i];
                    }
                }
            }

            openSet.Remove(node);
            closedSet.Add(node);

            if (node == targetNode)
            {
                //Debug.LogError ("Finished Path " + startNode.name + " " + targetNode.name);
                RetracePath(startNode, targetNode, ref store);
                return;
            }

            foreach (TileMaster neighbour in MapGenerator.me.getTileNeighbours(node))
            {
                if (!neighbour.isWalkable() || closedSet.Contains(neighbour) || neighbour == null || node == null)
                {//not valid neighbour
                    continue;
                }

                int newCostToNeighbour = node.getG() + GetDistance(node, neighbour);//calculates gCost
                if (newCostToNeighbour < neighbour.getG() || !openSet.Contains(neighbour))
                {
                    neighbour.setG(newCostToNeighbour);
                    neighbour.setH(GetDistance(neighbour, targetNode));
                    neighbour.setParent(node);
                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
    }