Time for action – adjacent squares

  1. Add the findAdjacentNodes() method to the Helper Methods region of the PathFinder class:
    static private List<PathNode> findAdjacentNodes(
        PathNode currentNode, 
        PathNode endNode)
    {
        List<PathNode> adjacentNodes = new List<PathNode>();
    
        int X = currentNode.GridX;
        int Y = currentNode.GridY;
    
        bool upLeft = true;
        bool upRight = true;
        bool downLeft = true;
        bool downRight = true;
    
        if ((X > 0) && (!TileMap.IsWallTile(X - 1, Y)))
        {
            adjacentNodes.Add(new PathNode(
                    currentNode,
                    endNode,
                    new Vector2(X - 1, Y),
                    CostStraight + currentNode.DirectCost));
        }
        else
        {
            upLeft = false;
            downLeft = false;
        }
     if ((X < 49) && (!TileMap.IsWallTile(X + 1, Y))) { adjacentNodes.Add(new PathNode( currentNode, endNode, new Vector2(X + 1, Y), CostStraight ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.