Another way to write "GetNeigbours" method:

Another way to write “GetNeigbours” method:

using static GridPosition;
/*
... other code
*/
private List<PathNode> GetNeigbours(PathNode node)
{
    List<PathNode> neigbours = new();

    GridPosition pos = node.GridPosition;
    GridPosition[] neigboursPositions =
    {
        pos + UP,
        pos + UP + RIGHT,
        pos + RIGHT,
        pos + RIGHT + DOWN,
        pos + DOWN,
        pos + DOWN + LEFT,
        pos + LEFT,
        pos + LEFT + UP
    };
    foreach (GridPosition p in neigboursPositions)
        if (_gridSystem.IsValidGridPosition(p))
            neigbours.Add(GetNode(p));

    return neigbours;
 }

And also we must add consts to “GridPosition” class

public static readonly GridPosition UP = new(0, 1);
public static readonly GridPosition DOWN = new(0, -1);
public static readonly GridPosition RIGHT = new(1, 0);
public static readonly GridPosition LEFT = new(-1, 0);
5 Likes

That’s an interesting way to do it, good job!

This helps me understand it better, thank you!

Privacy & Terms