...

/

Defining an Entity’s Field of View

Defining an Entity’s Field of View

Learn how to define the entity's field of view.

We need to follow a few steps to determine which tiles are visible from any point on the map.

  • We have to denote which tiles are transparent and which are opaque.
  • We need to prepare a component in which to store the visibility data.
  • We need a system that runs a field of view algorithm and stores the results in the new component type.

Tile opacity

The first step in implementing visibility is deciding which tiles are transparent and which are opaque. bracket-lib defines a trait entry for this known as is_opaque. Our map is simple, with tiles being either solid walls or floors. That leads to walls being opaque and floors being transparent. To represent this, bracket-lib requires us to add one more function to our map’s BaseMap implementation: is_opaque.

Press + to interact
impl BaseMap for Map {
fn is_opaque(&self, idx: usize) -> bool {
self.tiles[idx as usize] != TileType::Floor
}

Both is_blocked and is_opaque give similar results. In more complicated games, they aren’t the same thing. A closed window is opaque but blocks an entity from traveling through it. Likewise, an arrow slit prevents travel but is a great way to peek outside your castle.

Once the trait function is implemented, ...