TwistedMind
Yes, it just avoids iterating through same pairs twice (e.g. [5,7] and [7,5]). It also avoids pairing an index with itself (e.g. [2,2]).
No, traceLine() should return all tiles that make up a direct line from point to point. We'll also use this function again later. However path_is_passable() can exit early. As soon as it finds the first non-passable tile it can break the loop and return false. Because if only one tile in the path is non-passable, it means that the whole path is non-passable.
Next you should make sure that you've set this up correctly. Maybe add some debug drawing functionality that'll draw lines from a selected red point to all of its connections, and do this by querying the AStar2D itself via get_point_connections()
Once you're sure that the setup is ok, the thing is ready for step 2. And remember, step 1 needs to be done only once for a map. Your AStar2D object should be kept safe once all the data is fed into it. You don't re-create it every time you need to calculate a path. Besides being redundant it would also be computationally too expensive.
Now, because we cleverly chose our points and connections, AStar2D can calculate the shortest path from any red point to any other red point, always going around obstacles (remember: no connection is going through an obstacle. On top of this, our setup ensures that any other position in the map "sees" at least one nearest red point. Which means we can get the shortest non-obstacled path from anywhere in the map to anywhere else.
So, step 2.
When you need to calculate a path from point start to point end:
- if the direct line from start to end doesn't go through any obstacles, you don't need A* at all. Your path is a direct connection.
Otherwise:
- find two red points, one nearest to start and other nearest to end, by calling
AStar2D::get_closest_point()
- find a path between those two red points by calling
AStar2D::get_point_path(). It will return a list of red points that form the shortest path.
- add your start and end points at the start and the end of that point list respectively.
You now have a list of directly connectable red points, that form the shortest path form start to end. Depending on the type of movement in your game, you can use this directly to animate the movement, or if you need the exact tile-to-tile path, simply call traceLine() for each two consecutive waypoints in the list, merge it all into one array, and you've got your tile path from start to end.