I have a standard 2D grid that i use for pathfinding. I already use A* to find the path of my units when I want to move them around.
What I need to implement now is a preview of all possible destinations of the unit when it is selected.
Here is what I want to achieve.
The yellow/orange cells represent the possible destinations.
I want to make this as optimized as possible, because it can be executed on very big maps and for multiple units.
I have something in mind but I wonder if there is better approach for this.
Here is my idea:
Get a list of all cells that are walkable in a square around my unit. The square will be from
Unit.X - Unit.Speed
toUnit.X + Unit.Speed
for X axis andUnit.Y - Unit.Speed
toUnit.Y + Unit.Speed
for Y axis.For each element in this list, execute A* search to see if it is possible to find a path.
By doing the search for only the cells that are within my unit range I should be able to do this on big maps.
Basically this is the same feature that we have in games like Heroes 3 and many other turn based games.
I was also wondering when it is a good idea to execute this pathfinding logic. When you select a unit (might result a spike) or when something moves on the map to update the list of all possible destinations of all units on the map.