Tracking Game Level

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Adding Stairs to the Map | TOC | Displaying the Current Level on the HUD 👉

Transitioning between levels is a great start — but it’s helpful to know what level the player is on. You need this information to determine if you should spawn the Amulet of Yala or an exit. You’ll use the current dungeon level to update the player with their progress through the dungeon. The current level will also be used in Chapter 15, Combat Systems and Loot, to spawn increasingly more challenging monsters and nicer loot as the adventurer progresses.

Let’s start by adding the current dungeon level to the Player component. The player is the only entity advancing through the dungeon, so the Player component is a good place to store the current map level. Open components.rs, and add a map_level field to the Player component:

DeeperDungeons/more_levels/src/components.rs

​ ​#​[​derive​(Clone, Copy, Debug, PartialEq)]
​ ​pub​ ​struct​ Player{
​ ​pub​ map_level: u32
​ }

Your text editor may have flagged spawner.rs as an error because you need to include every field when you create a structure. Open spawner.rs and update spawn_player to start the player on level zero:

DeeperDungeons/more_levels/src/spawner.rs

​ ​pub​ ​fn​ ​spawn_player​(ecs : &​mut​ World, pos : Point) {
​ ecs​.push​(
» (Player{map_level: 0},
​ pos,
​…

--

--

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.