I need to calculate the line-of-sight distance between two objects (I think).
I'm currently hacking up the RTS template to learn more about events with C2.
I currently have Enemy tanks only returning fire once fired upon by a friendly tank. I now want to add some extra Enemy AI which will allow an Enemy tank to detect any Friendly within it's shooting range, who is also shooting, and then engage.
See image below;
In this example there are two Enemy (red) tanks within range (for simplicity I've highlighted the range of the Friendly tank - but in reality this is Enemy centric). What I want to accomplish is these two tanks detecting that the Friendly in their range is shooting, and to begin engaging it.
My thought for achieving this is;
For each enemy unit that isn't firing
- find closest friendly unit
- Calc distance to unit (line-of-sight)
- if distance <= range AND friendly is firing then ATTACK!!
This only gets me the one closest Friendly to the enemy - there could be more friendly in range attacking, but since i can only attack one at a time, this is good enough.
How do I get that distance figure, and how do I ensure it is line-of-sight and not including a random path route distance?
Any thought appreciated.