Limiting Monsters’ Fields of View

Hands-on Rust — by Herbert Wolverson (77 / 120)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Defining an Entity’s Field of View | TOC | Adding Spatial Memory 👉

You can significantly improve game play by making the monsters chase the player only when they can see them. Doing so introduces new tactics — if you can break line of sight to the monster, it won’t follow you. It removes the certainty that if you “camp” long enough, you will defeat 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, you’ve already done most of the hard work. Monsters have a FieldOfView component just like the player. By generically implementing the system, the game mechanic applies to any entity that has a FieldOfView component. You’ve also already learned how to use contains to see if a tile is visible for a given set.

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

WhatCanISee/eyesight/src/systems/chasing.rs

​ ​#​[system]
​ #[read_component(Point)]
​ #[read_component(ChasingPlayer)]
»#[read_component(FieldOfView)]
​ #[read_component(Health)]
​ #[read_component(Player)]
​ ​pub​ ​fn​ ​chasing​(
​ #[resource] map: &Map,
​ ecs: &SubWorld,
​ commands: &​mut​ CommandBuffer
​ ) {
» ​let​ ​mut​ movers= <(Entity, &Point, &ChasingPlayer, &FieldOfView)>::​query​();
​ ​let​ ​mut​ positions =…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.