From aee89fade73afa8cc874f969de62e1c8f0894dc8 Mon Sep 17 00:00:00 2001 From: Tyrel Souza <923113+tyrelsouza@users.noreply.github.com> Date: Tue, 31 Aug 2021 13:52:56 -0400 Subject: [PATCH] map themes --- src/main.rs | 2 ++ src/map_builder/automata.rs | 7 ++++--- src/map_builder/drunkard.rs | 1 + src/map_builder/empty.rs | 1 + src/map_builder/mod.rs | 16 ++++++++++++++-- src/map_builder/rooms.rs | 1 + src/map_builder/themes.rs | 33 +++++++++++++++++++++++++++++++++ src/systems/map_render.rs | 17 ++++++++--------- 8 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 src/map_builder/themes.rs diff --git a/src/main.rs b/src/main.rs index 54b1e51..6fc17fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,7 @@ impl State { resources.insert(Camera::new(map_builder.player_start)); resources.insert(TurnState::AwaitingInput); + resources.insert(map_builder.theme); State { ecs, resources, @@ -98,6 +99,7 @@ impl State { self.resources.insert(map_builder.map); self.resources.insert(Camera::new(map_builder.player_start)); self.resources.insert(TurnState::AwaitingInput); + self.resources.insert(map_builder.theme); } fn victory(&mut self, ctx: &mut BTerm) { diff --git a/src/map_builder/automata.rs b/src/map_builder/automata.rs index 317aa9d..8ab8b22 100644 --- a/src/map_builder/automata.rs +++ b/src/map_builder/automata.rs @@ -1,9 +1,9 @@ use super::MapArchitect; use crate::prelude::*; -pub struct AutomataArchitect {} +pub struct CellularAutomataArchitect {} -impl MapArchitect for AutomataArchitect { +impl MapArchitect for CellularAutomataArchitect { fn new(&mut self, rng: &mut RandomNumberGenerator) -> MapBuilder { let mut mb = MapBuilder { map: Map::new(), @@ -11,6 +11,7 @@ impl MapArchitect for AutomataArchitect { monster_spawns: Vec::new(), player_start: Point::zero(), amulet_start: Point::zero(), + theme: super::themes::DungeonTheme::new(), }; self.random_noise_map(rng, &mut mb.map); for _ in 0..10 { @@ -25,7 +26,7 @@ impl MapArchitect for AutomataArchitect { } } -impl AutomataArchitect { +impl CellularAutomataArchitect { fn random_noise_map(&mut self, rng: &mut RandomNumberGenerator, map: &mut Map) { map.tiles.iter_mut().for_each(|t| { let roll = rng.range(0, 100); diff --git a/src/map_builder/drunkard.rs b/src/map_builder/drunkard.rs index 3a4a03a..d9c1ba0 100644 --- a/src/map_builder/drunkard.rs +++ b/src/map_builder/drunkard.rs @@ -14,6 +14,7 @@ impl MapArchitect for DrunkardsWalkArchitect { monster_spawns: Vec::new(), player_start: Point::zero(), amulet_start: Point::zero(), + theme: super::themes::DungeonTheme::new(), }; mb.fill(TileType::Wall); let center = Point::new(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); diff --git a/src/map_builder/empty.rs b/src/map_builder/empty.rs index dc6102f..d987356 100644 --- a/src/map_builder/empty.rs +++ b/src/map_builder/empty.rs @@ -11,6 +11,7 @@ impl MapArchitect for EmptyArchitect { monster_spawns: Vec::new(), player_start: Point::zero(), amulet_start: Point::zero(), + theme: super::themes::DungeonTheme::new(), }; mb.fill(TileType::Floor); mb.player_start = Point::new(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); diff --git a/src/map_builder/mod.rs b/src/map_builder/mod.rs index fd3a693..e9932a7 100644 --- a/src/map_builder/mod.rs +++ b/src/map_builder/mod.rs @@ -3,11 +3,13 @@ mod drunkard; mod empty; mod prefab; mod rooms; +mod themes; -use automata::AutomataArchitect; +use automata::CellularAutomataArchitect; use drunkard::DrunkardsWalkArchitect; use empty::EmptyArchitect; use rooms::RoomsArchitect; +use themes::*; use crate::map_builder::prefab::apply_prefab; use crate::prelude::*; @@ -23,6 +25,11 @@ pub struct MapBuilder { pub monster_spawns: Vec, pub player_start: Point, pub amulet_start: Point, + pub theme: Box, +} + +pub trait MapTheme: Sync + Send { + fn tile_to_render(&self, tile_type: TileType) -> FontCharType; } impl MapBuilder { @@ -30,12 +37,17 @@ impl MapBuilder { let mut architect: Box = match rng.range(0, 3) { 0 => Box::new(DrunkardsWalkArchitect {}), 1 => Box::new(RoomsArchitect {}), - _ => Box::new(AutomataArchitect {}), + _ => Box::new(CellularAutomataArchitect {}), }; let mut mb = architect.new(rng); apply_prefab(&mut mb, rng); + mb.theme = match rng.range(0, 2) { + 0 => DungeonTheme::new(), + _ => ForestTheme::new(), + }; + mb } diff --git a/src/map_builder/rooms.rs b/src/map_builder/rooms.rs index 1d9de39..08f9413 100644 --- a/src/map_builder/rooms.rs +++ b/src/map_builder/rooms.rs @@ -11,6 +11,7 @@ impl MapArchitect for RoomsArchitect { monster_spawns: Vec::new(), player_start: Point::zero(), amulet_start: Point::zero(), + theme: super::themes::DungeonTheme::new(), }; mb.fill(TileType::Wall); mb.build_random_rooms(rng); diff --git a/src/map_builder/themes.rs b/src/map_builder/themes.rs new file mode 100644 index 0000000..47f2f95 --- /dev/null +++ b/src/map_builder/themes.rs @@ -0,0 +1,33 @@ +use crate::prelude::*; +pub struct DungeonTheme {} +pub struct ForestTheme {} + +impl DungeonTheme { + pub fn new() -> Box { + Box::new(Self {}) + } +} + +impl MapTheme for DungeonTheme { + fn tile_to_render(&self, tile_type: TileType) -> FontCharType { + match tile_type { + TileType::Floor => to_cp437('.'), + TileType::Wall => to_cp437('#'), + } + } +} + +impl ForestTheme { + pub fn new() -> Box { + Box::new(Self {}) + } +} + +impl MapTheme for ForestTheme { + fn tile_to_render(&self, tile_type: TileType) -> FontCharType { + match tile_type { + TileType::Floor => to_cp437(';'), + TileType::Wall => to_cp437('"'), + } + } +} diff --git a/src/systems/map_render.rs b/src/systems/map_render.rs index 4d0e84f..e9dafe8 100644 --- a/src/systems/map_render.rs +++ b/src/systems/map_render.rs @@ -3,7 +3,12 @@ use crate::prelude::*; #[system] #[read_component(Player)] #[read_component(FieldOfView)] -pub fn map_render(ecs: &SubWorld, #[resource] map: &Map, #[resource] camera: &Camera) { +pub fn map_render( + ecs: &SubWorld, + #[resource] map: &Map, + #[resource] camera: &Camera, + #[resource] theme: &Box, +) { let mut fov = <&FieldOfView>::query().filter(component::()); let player_fov = fov.iter(ecs).nth(0).unwrap(); let mut draw_batch = DrawBatch::new(); @@ -21,14 +26,8 @@ pub fn map_render(ecs: &SubWorld, #[resource] map: &Map, #[resource] camera: &Ca } else { DARK_GRAY }; - match map.tiles[idx] { - TileType::Floor => { - draw_batch.set(pt - offset, ColorPair::new(tint, BLACK), to_cp437('.')); - } - TileType::Wall => { - draw_batch.set(pt - offset, ColorPair::new(tint, BLACK), to_cp437('#')); - } - } + let glyph = theme.tile_to_render(map.tiles[idx]); + draw_batch.set(pt - offset, ColorPair::new(tint, BLACK), glyph); } } }