Building a Smarter Monster

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Chapter 9 Victory and Defeat | TOC | Implementing a Game Over Screen 👉

Monsters that move randomly aren’t much of a threat, and monsters with too much intelligence can make a game unplayable. Finding a nice balance is the key to making a fun yet challenging game.

In this chapter, you’ll give the monsters perfect knowledge of the dungeon’s floor plan. You’ll also provide the monsters with the player’s location and a relentless need to pursue that player. This creates a “gauntlet” effect in which monsters constantly beset the player until the level is clear.

You’ll begin to turn this “heat-seeking” behavior into more realistic behavior in Chapter 10, Fields of View, but for now, the goal is to create an action-packed challenge.

You’ll start by tagging monsters with the new behavior. You’ll then learn about Dijkstra maps (sometimes also called flow maps) and how you can use them to efficiently help your monsters navigate toward the player.

Tagging the New Behavior

Start by adding another component to indicate that the monster is chasing the player. In components.rs, add the following component:

WinningAndLosing/gauntlet/src/components.rs

​ ​#​[​derive​(Clone, Copy, Debug, PartialEq)]
​ ​pub​ ​struct​ ChasingPlayer;

--

--

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.