Rendering With Themes
Learn how to render themes.
The MapBuilder
needs to know what theme we’re associating with our newly designed map.
Add a theme to the MapBuilder
in map_builer/mod.rs
:
Press + to interact
const NUM_ROOMS: usize = 20;pub struct MapBuilder {pub map : Map,pub rooms : Vec<Rect>,pub monster_spawns : Vec<Point>,pub player_start : Point,pub amulet_start : Point,pub theme : Box<dyn MapTheme>}
Rust requires that we initialize every field in a struct on creation because uninitialized fields are a common source of bugs in other languages. Add the following line to the initialization of each MapBuilder
(automata.rs
, drunkard.rs
, empty.rs
, and rooms.rs
):
Press + to interact
theme: super::themes::DungeonTheme::new()
Creating a new theme object is a little wasteful because we’re creating a theme and never using it. We could wrap it in an Option
and use None
and Some(x)
if we prefer. Map rendering happens so infrequently that it isn’t worth worrying about ...