예제 #1
0
    private void chekNaborsViablity(Ground_Tile lookTile, int[] lookLocation)
    {
        int newG = moveCost;

        //cheking to see if the tile is pathable or if it is in the closed list
        if (lookTile.pathable == true && !closedList.ContainsKey(lookLocation[0] * Board.board.GetLength(1) + lookLocation[1]))
        {
            //Debug.Log("neighbor "+ i +" "+j +" is pathable and not in closed list");
            //setting its G equal to the cost of its position from the curent point added to the curent points G
            newG += curentTileObj.G;
            //cheking if the new tile is in the oppen list
            if (oppenList.contains(lookTile))
            {
                //Debug.Log("" + lookTile.tileY + " " + lookTile.tileX + " is in open list");
                //check to see if its g is smaller or greater then the curent one
                if (lookTile.G < oppenList.Peek(lookTile).G)
                {
                    int heapIndexOfLook = oppenList.PeekL(lookTile);
                    //change the G value and Parent
                    oppenList.changeValuesAt(heapIndexOfLook, newG, curentTileObj);
                }
            }
            else
            {
                //Debug.Log(i + " " + j + " is new");
                //setting the tiles values and puting it in the oppen list
                lookTile.updateValues(newG);
                lookTile.parent = curentTileObj;
                oppenList.Insert(lookTile);
            }
        }
    }
    private void createNeighbors()
    {
        int startI = curentTile[0] - 1;
        int maxI   = curentTile[0] + 1;
        int startJ = curentTile[1] - 1;
        int maxJ   = curentTile[1] + 1;

        //cheking to see if you are at the edge of the board array
        if (curentTile[0] == 0)
        {
            startI += 1;
        }
        else if (curentTile[0] == Board.board.GetLength(1))
        {
            maxI -= 1;
        }
        if (curentTile[1] == 0)
        {
            startJ += 1;
        }
        else if (curentTile[1] == Board.board.GetLength(0))
        {
            maxJ -= 1;
        }

        //Debug.Log("Creating Neibors for tile " + curentTile[0] + " " + curentTile[1]);
        //looping thru adjacent tiles
        for (int i = startI; i <= maxI; i++)
        {
            //Debug.Log("maxI = " + maxI);
            //Debug.Log("curent i = " + i);
            for (int j = startJ; j <= maxJ; j++)
            {
                //skipping over curent tile
                if (i == curentTile[0] && j == curentTile[1])
                {
                    j++;
                }
                //Debug.Log("maxJ = " + maxJ);
                //Debug.Log("curent j = " + j);
                int[] lookLocation = new int[2];
                lookLocation[0] = i;
                lookLocation[1] = j;
                int         newG     = strMoveCost;
                Ground_Tile lookTile = Board.board[i, j];
                //setting the move increace to diagonal or streate values
                if (i != curentTile[0] && j != curentTile[1])
                {
                    newG = diagMoveCost;
                }

                //Debug.Log("neighbor " + i + " " + j + " is pathable ="+ lookTile.pathable);
                //Debug.Log("neighbor " + i + " " + j + " is in closed list =" + closedList.ContainsKey(lookLocation[0] + "" + lookLocation[1]));
                //cheking to see if the tile is pathable or if it is in the closed list
                if (lookTile.pathable == true && !closedList.ContainsKey(lookLocation[0] * Board.board.GetLength(1) + lookLocation[1]))
                {
                    //Debug.Log("neighbor "+ i +" "+j +" is pathable and not in closed list");
                    //setting its G equal to the cost of its position from the curent point added to the curent points G
                    newG += curentTileObj.G;
                    //cheking if the new tile is in the oppen list
                    if (oppenList.contains(lookTile))
                    {
                        //Debug.Log(i + " " + j + " is in oppen list");
                        //check to see if its g is smaller or greater then the curent one
                        if (lookTile.G < oppenList.Peek(lookTile).G)
                        {
                            int heapIndexOfLook = oppenList.PeekL(lookTile);
                            //change the G value and Parent
                            oppenList.changeValuesAt(heapIndexOfLook, newG, curentTileObj);
                        }
                    }
                    else
                    {
                        //Debug.Log(i + " " + j + " is new");
                        //setting the tiles values and puting it in the oppen list
                        lookTile.updateValues(newG);
                        lookTile.parent = curentTileObj;
                        oppenList.Insert(lookTile);
                    }
                }
            }
        }
    }