Grid.GetNeighbours C# (CSharp) Method

GetNeighbours() public method

public GetNeighbours ( Node node ) : List
node Node
return List
	public List<Node> GetNeighbours(Node node) 
	{
		List<Node> neighbours = new List<Node>();

		bool rightBlocked = true;
		bool topBlocked = true;
		bool leftBlocked = true;
		bool bottomBlocked = true;

		//add adjacent neighbors
		for (int x = -1; x <= 1; x++) 
		{
			for (int y = -1; y <= 1; y++) 
			{
				if (x == 0 && y == 0)
					continue;
				
				if (x == 0 || y == 0)
				{
					int checkX = node.gridX + x;
					int checkY = node.gridY + y;

					if (checkX >= 0 && checkX < gridSizeX && checkY >= 0 && checkY < gridSizeY) 
					{
						// If directly adjacent, always add as neighbor
						neighbours.Add(grid[checkX,checkY]);

						// Test for blocked nodes
						if (x == 1)
							rightBlocked = !grid[checkX,checkY].walkable;
						else if (y == 1)
							topBlocked = !grid[checkX,checkY].walkable;
						else if (x == -1)
							leftBlocked = !grid[checkX,checkY].walkable;
						else if (y == -1)
							bottomBlocked = !grid[checkX,checkY].walkable;
					}
				}
			}
		}

		// Diagonal bools are derived from adjacent
		bool topRightBlocked = rightBlocked || topBlocked;
		bool topLeftBlocked = topBlocked || leftBlocked;
		bool bottomLeftBlocked = leftBlocked || bottomBlocked;
		bool bottomRightBlocked = bottomBlocked || rightBlocked;

		//add diagonal neighbors
		for (int x = -1; x <= 1; x++) 
		{
			for (int y = -1; y <= 1; y++) 
			{
				if (x == 0 && y == 0)
					continue;
				
				if (x == 0 || y == 0)
					continue;


				int checkX = node.gridX + x;
				int checkY = node.gridY + y;

				if (checkX >= 0 && checkX < gridSizeX && checkY >= 0 && checkY < gridSizeY) 
				{
					// if not a diagonal or is a non-blocked diagonal, add to neighbor list
					if ( (x == 1 && y == 1 && !topRightBlocked) 
						|| (x == -1 && y == 1 && !topLeftBlocked)
						|| (x == -1 && y == -1 && !bottomLeftBlocked)
						|| (x == 1 && y == -1 && !bottomRightBlocked) )
					{
						neighbours.Add(grid[checkX,checkY]);
					}
				}
			}
		}

		return neighbours;
	}

Usage Example

Example #1
0
    public List <Node> FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);

        List <Node>    openSet   = new List <Node> ();
        HashSet <Node> closedSet = new HashSet <Node> ();

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            Node currentNode = openSet [0];
            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet [i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
                {
                    currentNode = openSet [i];
                }
            }
            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            if (currentNode == targetNode)
            {
                return(RetracePath(startNode, targetNode));
                //return;
            }

            foreach (Node neighbour in grid.GetNeighbours(currentNode))
            {
                if (!neighbour.walkable || closedSet.Contains(neighbour))
                {
                    continue;
                }

                int penality = 0;
                if (IsNearToUnwalkable(neighbour))
                {
                    penality = 1000;
                }

//				int newMovementCostToNeighbour = currentNode.gCost + GetDistance (currentNode, neighbour);
                int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + penality;
//				Debug.Log (newMovementCostToNeighbour + "gCost "+neighbour.gCost);
                if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost  = newMovementCostToNeighbour;
                    neighbour.hCost  = GetDistance(neighbour, targetNode);
                    neighbour.parent = currentNode;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
        return(null);
    }
All Usage Examples Of Grid::GetNeighbours