Calculating dynamic light and shadows in tile based roguelike games – part 3: let there be Bresenham light

Are you enjoying the series about dynamic light and shadows in tile based roguelike games? In step 1 I explained Bresenham algorithm to connect two points in a tile based environment and in step 2 I showed you how to draw circles in a tile based environment.

Now it’s time to put things together and make them work properly: what about if Bresenham circles were the field of view and Bresenham lines were rays cast from player eyes to each point along the circumference of the field of view?

And what about if any obstacle hitting the ray would stop it?

This is what we would get:

Drag the player (the green tile) along the maze, not to overlap red walls, and see how dynamic light works. There is still some more to do. First, we will avoid light to pass through diagonal tiles, but it’s an improvement we’ll see next time.

Here is the source code, still uncommented because it’s not finished yet but you should start to understand where I’m going.

Next time I’ll show you how to remove light passing through diagonal walls. How would you do? Feel free to post your suggestions, meanwhile download the source code.