If you're using simple shapes to block the entity's view, there is an easy way to do this that I have implemented:
Create a VisionWave
object which can move either horizontally or vertically. You can define a VisionWave
using a source coordinate, two lines that intersect that point, and a distance from the source point.
You should have 4 waves: one going up, one down, one left, and one right, and the lines that define them should have a slope of 1 and -1 (i.e., an X). My crude drawing below shows one wave (going right) as represented by the >
character.
\ /
\ />
\ / >
@ >
/ \ >
/ \>
/ \
Make a loop that propagates each wave one pixel at a time. When you propagate the wave, you want to do the following:
- Mark every pixel that the wave is touching as visible.
- If any of the pixels that the wave touches block light, then you want to split the wave into two, and recursively propagate each one.
I implemented a system like this in my Roguelike and it is very fast. Make sure to profile your code for bottlenecks.
If you're very clever you might try circular waves instead of straight lines, but I don't know if it would be faster due to the trigonometric calculations.