lineOfSight
lineOfSight
> Functions
Checks the line of sight between the cell start and the cell end, ignoring the entities in the array entityToIgnore.
Example: if (lineOfSight(getCell(), getCell(enemy)))
The algorithm works as follows:
- Draw a segment between the centers of the two tested cells.
- List the cells crossed by this segment. A cell does not count as crossed if the segment only grazes its edge, or if it is ignored.
- If even one of these crossed cells is an obstacle or contains an entity, the line of sight is blocked; otherwise it is clear.
Parameters
- start: Starting cell.
- end: Target cell.
- entityToIgnore (optional): Entity to ignore, or array of entities to ignore. By default, your entity is ignored. With a single entity, only that one is ignored (yours no longer is); with an array, your entity is ignored in addition to those in the array.
Return
- los: (boolean)
null if start or end is not a cell of the map;
true if the line of sight is clear;
false otherwise.
Demonstration
Click on a cell to display all the cells that are in line of sight with it.
Mathematical implementation
lineOfSight uses an algorithm inspired by Bresenham's line drawing algorithm (Wikipedia).