...

/

Theming Our Dungeon

Theming Our Dungeon

Learn how to implement a dungeon theme.

We'll cover the following...

Now that we know how to use traits to make replaceable components, let’s give the game a bit more visual flair by substituting different map themes at runtime. Our dungeon could become a forest or anything else we can draw. Alongside varied map design, this also helps keep players interested.

Add a new trait defining a map theme in map_builder/mod.rs:

Press + to interact
pub trait MapTheme : Sync + Send {
fn tile_to_render(&self, tile_type: TileType) -> FontCharType;
}

The required function signature should make sense: we return a character to render from the map font given a tile type. The Sync + Send syntax is new. Rust emphasizes fearless concurrency, which means that we can write multithreaded code without having fear of any side effects. Much of how Rust enforces safe concurrency is with the Sync and Send traits. The traits are defined as:

  • If an object implements Sync, we can safely access it from multiple threads.
  • If an object implements Send, we can safely share it between threads.
...