Crate adam_fov_rs

Source
Expand description

License: MIT Crates.io docs

An implementation of Adam Millazo’s FOV algorithm

This crate provides a single function, compute_fov, which computes a field of view into a 2d grid from existing map data.

§Example

use adam_fov_rs::*;

#[derive(Clone)]
enum Tile {
    Floor,
    Wall,
}
let width = 50;
let height = 50;
let index = |p: IVec2| p.y as usize * width + p.x as usize;

let mut game_map = vec![Tile::Floor; width * height];
// Add a vision blocker
game_map[index(IVec2::new(15, 16))] = Tile::Wall;

let mut vision = vec![false; width * height];

let is_opaque = |p| matches!(game_map[index(p)], Tile::Wall);
let mark_visible = |p| {
    let i = index(p);
    vision[i] = true;
};

compute_fov([15, 15], 5, [width, height], is_opaque, mark_visible);

let is_visible = |p| vision[index(p)];
assert!(!is_visible(IVec2::new(15, 17)));
assert!(is_visible(IVec2::new(17, 15)));

Taken from the terminal example

Structs§

IVec2
A 2-dimensional vector.

Traits§

GridPoint
A trait for types representing an integer point on a 2d grid.
GridSize
A trait for types representing the size of a rectangular 2d grid.

Functions§

compute_fov
Compute a field of view into a 2d grid from existing map data.