By the time I had a working level, I had spent about a weekend coding and I was ready to indulge myself. On Sunday evening I decided to tackle to problem of line-of-sight. Up until now I used a very, very simple algorithm to determine what you can and cannot see. Everything within 6 distance is drawn, the rest is ignored. This means you can see into and through walls, which is not very realistic. You should only see tiles that are not hidden behind other tiles. A very basic way to find out which are those tiles is to draw lines from the hero to the edge of his field of vision. Draw all tiles on that line until you reach a wall. Stop and continue with the next line. I guess this is 2D ray tracing. My first experiments did not work at all. At one point I got some kind of result, but somehow some tiles remained bright even if they were outside the field of vision.
I made some improvements to my code, but found it hard to see what was going on. So I added numbers to each floor and wall image. 5 = bright, 1= dark. I now started understanding what was going wrong. This was not trivial, and would require me to come up with some actual algorithms myself, rather than steal borrow them from the internet. It took some evenings the week thereafter to get it right. I won’t go into details, just suffice to say that lighting and line of sight is a pain.
By the way: notice that the area around the stairs is lit as well. I thought it to be a nice touch if there was light coming from above. Light to guide you back home.
I finally got it the way I wanted it. Not only did ray tracing work well with lighting, light sources in the distance would be less bright than light sources closeby. The image below shows this. Both the stairs and our hero have the same lightsource intensity, but the stairs are far away.
The distance between the hero and the stairs in the above picture is about 16. I increased the line of sight significantly to be able to spot distant light sources. The new maximum range is 19. This means a lot of ray tracing for each frame and by now rendering a frame consists of 5 ms ray tracing and less than a millisecond drawing.
After having all this in place, it was easy to add the option to drop new light sources. Our hero can drop light gems that emit a bleak red light.
Leave a comment