Defining an Entity’s Field of View
Hands-on Rust — by Herbert Wolverson (76 / 120)
👈 Chapter 10 Fields of View | TOC | Limiting Monsters’ Fields of View 👉
You need to follow a few steps to determine which tiles are visible from any point on the map. First, you have to denote which tiles are see-through and which are opaque. Second, you need to prepare a component to store the visibility data. Finally, you 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 see-through and which are opaque. Bracket-lib defines a trait entry for this known as is_opaque. Your map is simple, with tiles being either solid walls or floors. That leads to walls being opaque and floors being transparent (or not opaque). To represent this, bracket-lib requires you to add one more function to your map’s BaseMap implementation: is_opaque:
impl BaseMap for Map {
fn is_opaque(&self, idx: usize) -> bool {
self.tiles[idx as usize] != TileType::Floor
}
Opaque and Blocked Are Not the Same
TIP
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…