Theming Your Dungeon
Hands-on Rust — by Herbert Wolverson (87 / 120)
👈 Chapter 12 Map Themes | TOC | Rendering with Themes 👉
Now that you 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. Your dungeon could become a forest — or anything else you can draw. Alongside varied map design, this also helps to keep players interested.
Open map_builder/mod.rs, and add a new trait defining a map theme:
MapTheming/themed/src/map_builder/mod.rs
pub trait MapTheme : Sync + Send {
fn tile_to_render(&self, tile_type: TileType) -> FontCharType;
}
The required function signature should make sense: given a tile type, you return a character to render from the map font. Sync+Send is new.[61] Rust emphasizes “fearless concurrency,” which means you can write multi-threaded code without fearing that it will do something terrible. A large part of how Rust enforces safe concurrency is with the Sync and Send traits. The traits are defined to mean:
- If an object implements Sync, you can safely access it from multiple threads.
- If an object implements Send, you can safely share it between threads.
Most of the time, you don’t need to implement Sync and Send yourself. Rust can figure out that some types of code are safe to send between threads. For example, pure functions…