Inspired by the article Field of Vision (FOV) using recursive shadowcasting – improved on the splendid website RougeBasin, I implemented the excellent algorithm in C Sharp 3.5. Below is a picture of the application:
The black squares represent solid objects, the blue are what the player can see and white is the player.
The source code can be downloaded here.
What’s Going On?
The code is straight forwards enough.
The class game.cs contains the FOV algorithm. When instantiated the direction the player intends to move is passed to the method movePlayer and if the player has been able to move the event playerMoved is fired. The publicly exposed properties mapXLimit, mapYLimit, playerX, playerY and Sight when used in conjunction with the method getMapPoint can be used to draw the map and what the player can see.
There really isn’t that much to say about it as the code speaks for itself, but if you’re stuck with anything please add a comment and I’ll get back to you.
Using the application
The keys q,w,e,a,s,d,f,z,x and c control the player, and you can create solid objects with the left mouse button and remove objects with the right.