Skip to content

Find path to direct neighbour #7

@codevogel

Description

@codevogel

It could be that you left this out for optimisation reasons, seeing as you could easily implement a way to check if one of the neighbours is your target tile before even calling the regular algorithm,

But the algorithms implementation (at least after episode 5) cannot find the path to it's direct neighbour.

Here's a proposed fix:

// in Pathfinding.cs

// in
    Vector3[] SimplifyPath(List<Tile> path)
    {
        List<Vector3> waypoints = new List<Vector3>();
        Vector2 directionOld = Vector2.zero;

        // ***************
        // Add this to make sure we do not just ignore the case where path.Count == 1
        // before even checking the rest
        //  **************
        if (path.Count == 1)
        {
            waypoints.Add(path[0].worldPos);
            return waypoints.ToArray();
        }

        for (int i = 1; i < path.Count; i++)
        {
            Vector2 directionNew = new Vector2(path[i - 1].gridCoords.x - path[i].gridCoords.x,
                                               path[i - 1].gridCoords.y - path[i].gridCoords.y);
            if (directionNew != directionOld)
            {
                // Also of importance: 
                // We start at i = 1, but we need to check all the way to the origin! so [i-1]
                waypoints.Add(path[i-1].worldPos);
            }
            directionOld = directionNew;
        }
        return waypoints.ToArray();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions