Tracking Game Level
Learn how to track game level.
Transitioning between levels is a great start, but it’s helpful to know what level the player is on. We need this information to determine if we should spawn the Amulet of Yala or an exit. We’ll use the current dungeon level later to present the player with their progress through the dungeon, providing a sense of progress. The current level will also be used in the next chapter to spawn increasingly difficult monsters and better loot as the adventurer progresses.
Let’s start by adding the current dungeon level to the Player
component. The adventurer is the only entity advancing through the dungeon, so the Player
component is a good place to store the current map level. Add a map_level
field to the Player
component in components.rs
:
#[derive(Clone, Copy, Debug, PartialEq)]pub struct Player{pub map_level: u32}
We have to include every field when we create a structure. Update the spawn_player
in spawner.rs
to start the adventurer on level zero:
pub fn spawn_player(ecs : &mut World, pos : Point) {ecs.push((Player{map_level: 0},pos,Render{color: ColorPair::new(WHITE, BLACK),glyph : to_cp437('@')},Health{ current: 10, max: 10 },FieldOfView::new(8)));}
With the current level safely stored in the Player
component, we’re ready to add transitions between levels when the adventurer reaches an exit.
Level transition state
Finishing the level is a major event, similar to a victory or defeat. Such an event requires that the main game loop do something drastic, outside of the ECS systems. Add a new ...