private Node GetValidNodeNeighbour(int neighboutNodeXPosition, int neighbourNodeYPosition) { // If neighbour node position is beyond grid then return null if (neighboutNodeXPosition >= gridWidth || neighboutNodeXPosition < 0 || neighbourNodeYPosition >= gridHeight || neighbourNodeYPosition < 0) { return(null); } // if neighbour is an obstacle or neighbour is in the closed list then skip Node neighbourNode = gridNodes.GetGridNode(neighboutNodeXPosition, neighbourNodeYPosition); if (neighbourNode.isObstacle || closedNodeList.Contains(neighbourNode)) { return(null); } else { return(neighbourNode); } }
/// <summary> /// This method returns the neighborNode from currentNode at the given neighbor node x/y position. If that node location is invalid (out of the grid, is an obstacle, /// or is already in the closed node list), this returns null so we won't have to check it /// </summary> /// <param name="neighborNodeXPosition"></param> /// <param name="neighborNodeYPosition"></param> /// <returns> Returns the neighbor Node at the specified position if it's valid, else returns null </returns> private Node GetValidNeighborNode(int neighborNodeXPosition, int neighborNodeYPosition) { // If the neighbor node's position is beyond the grid, then return null - they aren't valid! if (neighborNodeXPosition >= gridWidth || neighborNodeXPosition < 0 || neighborNodeYPosition >= gridHeight || neighborNodeYPosition < 0) { return(null); } // Get the neighbor node from the GridNodes Node array at the neighbor grid position Node neighborNode = gridNodes.GetGridNode(neighborNodeXPosition, neighborNodeYPosition); // If the neighbor node is an obstacle, or if the neighbor node is in the closed list already, then skip it! It's not a valid neighbor node if (neighborNode.isObstacle || closedNodeList.Contains(neighborNode)) { return(null); } // Finally, if it's valid return the valid neighbor node! else { return(neighborNode); } }