Limiting Monsters’ Fields-of-View

Learn how to improve gameplay by making the monsters only chase the player when they see them.

Limiting monsters’ field of view introduces new tactics. If the player can escape a monster’s line of sight, it won’t follow them. It removes the certainty that if you wait around long enough, you’ll have defeated the level’s denizens. It also adds some uncertainty—the player isn’t given many clues as to what may reside beyond the next corner.

Fortunately, we’ve already done most of the hard work. Monsters have a FieldOfView component just like the player. By implementing the system in a generic fashion, the game mechanic applies to any entity that has a FieldOfView component. We’ve also already learned how to use contains to see if a tile is visible for a given set.

The first query in systems/chasing.rs needs to be updated to include the entity’s field of view:

Get hands-on with 1200+ tech skills courses.