This is a copy of the ShadowCasting code from the MSDN Developer Blog of Eric Lippert.
Read more about it in his series of posts on blogs.msdn.microsoft.com:
- ShadowCasting in C# part One
- ShadowCasting in C# part Two
- ShadowCasting in C# part Three
- ShadowCasting in C# part Four
- ShadowCasting in C# part Five
- ShadowCasting in C# part Six
Availabe via nuget:
Install-Package ShadowCaster
Usage Example:
var cellsInFieldOfView = new List<Cell>(); // will hold our Cell which ARE visible in the field of view
ShadowCaster.ComputeFieldOfViewWithShadowCasting(
5, // x position of the object doing the viewing
6, // x position of the object doing the viewing
4, // The sight radius, represents how far the position can see
(x,y) => { return myMap.getCell(x,y).IsWall(); }, // Func that returns whether the cell can be seen through
(x,y) => { cellsInFieldOfView.Add(myMap.getCell(x,y)); } // Action that specifies what to do with each item that IS in the field of view
);
// Note: myMap and Cell objects are just examples of the types of classes you might use