diff --git a/src/map_builders/prefab_builder.rs b/src/map_builders/prefab_builder/mod.rs similarity index 55% rename from src/map_builders/prefab_builder.rs rename to src/map_builders/prefab_builder/mod.rs index b491042..4257d5b 100644 --- a/src/map_builders/prefab_builder.rs +++ b/src/map_builders/prefab_builder/mod.rs @@ -3,11 +3,13 @@ use super::{ }; use rltk::RandomNumberGenerator; use specs::prelude::*; +mod prefab_levels; #[allow(dead_code)] #[derive(PartialEq, Clone)] pub enum PrefabMode { RexLevel { template: &'static str }, + Constant { level: prefab_levels::PrefabLevel }, } pub struct PrefabBuilder { @@ -51,13 +53,14 @@ impl MapBuilder for PrefabBuilder { } impl PrefabBuilder { + #[allow(dead_code)] pub fn new(new_depth: i32) -> PrefabBuilder { PrefabBuilder { map: Map::new(new_depth), starting_position: Position { x: 0, y: 0 }, depth: new_depth, history: Vec::new(), - mode: PrefabMode::RexLevel { template: "../resources/wfc-populated.xp" }, + mode: PrefabMode::Constant { level: prefab_levels::WFC_POPULATED }, spawns: Vec::new(), } } @@ -65,6 +68,7 @@ impl PrefabBuilder { fn build(&mut self, rng: &mut RandomNumberGenerator) { match self.mode { PrefabMode::RexLevel { template } => self.load_rex_map(&template), + PrefabMode::Constant { level } => self.load_ascii_map(&level), } self.take_snapshot(); @@ -88,6 +92,43 @@ impl PrefabBuilder { } } + fn char_to_map(&mut self, ch: char, idx: usize) { + match ch { + ' ' => self.map.tiles[idx] = TileType::Floor, + '#' => self.map.tiles[idx] = TileType::Wall, + '>' => self.map.tiles[idx] = TileType::DownStair, + '@' => { + let x = idx as i32 % self.map.width; + let y = idx as i32 / self.map.width; + self.map.tiles[idx] = TileType::Floor; + self.starting_position = Position { x: x as i32, y: y as i32 }; + } + 'g' => { + self.map.tiles[idx] = TileType::Floor; + self.spawns.push((idx, "Goblin".to_string())); + } + 'o' => { + self.map.tiles[idx] = TileType::Floor; + self.spawns.push((idx, "Orc".to_string())); + } + '^' => { + self.map.tiles[idx] = TileType::Floor; + self.spawns.push((idx, "Bear Trap".to_string())); + } + '%' => { + self.map.tiles[idx] = TileType::Floor; + self.spawns.push((idx, "Rations".to_string())); + } + '!' => { + self.map.tiles[idx] = TileType::Floor; + self.spawns.push((idx, "Health Potion".to_string())); + } + _ => { + rltk::console::log(format!("Unknown glyph loading map: {}", (ch as u8) as char)); + } + } + } + #[allow(dead_code)] fn load_rex_map(&mut self, path: &str) { let xp_file = rltk::rex::XpFile::from_resource(path).unwrap(); @@ -97,48 +138,38 @@ impl PrefabBuilder { for x in 0..layer.width { let cell = layer.get(x, y).unwrap(); if x < self.map.width as usize && y < self.map.height as usize { - // Saving these for later, for flipping the pref horizontally/vertically/both. - // let flipped_x = (self.map.width - 1) - x as i32; - // let flipped_y = (self.map.height - 1) - y as i32; let idx = self.map.xy_idx(x as i32, y as i32); - match (cell.ch as u8) as char { - '@' => { - self.map.tiles[idx] = TileType::Floor; - self.starting_position = Position { x: x as i32, y: y as i32 } - } - ' ' => self.map.tiles[idx] = TileType::Floor, - '#' => self.map.tiles[idx] = TileType::Wall, - '>' => self.map.tiles[idx] = TileType::DownStair, - 'g' => { - self.map.tiles[idx] = TileType::Floor; - self.spawns.push((idx, "goblin".to_string())); - } - 'o' => { - self.map.tiles[idx] = TileType::Floor; - self.spawns.push((idx, "orc".to_string())); - } - '^' => { - self.map.tiles[idx] = TileType::Floor; - self.spawns.push((idx, "bear trap".to_string())); - } - '%' => { - self.map.tiles[idx] = TileType::Floor; - self.spawns.push((idx, "rations".to_string())); - } - '!' => { - self.map.tiles[idx] = TileType::Floor; - self.spawns.push((idx, "health potion".to_string())); - } - _ => { - rltk::console::log(format!( - "Unknown glyph {} when loading map", - (cell.ch as u8) as char - )); - } - } + // We're doing some nasty casting to make it easier to type things like '#' in the match + self.char_to_map(cell.ch as u8 as char, idx); } } } } } + + fn read_ascii_to_vec(template: &str) -> Vec { + let mut string_vec: Vec = template.chars().filter(|a| *a != '\r' && *a != '\n').collect(); + for c in string_vec.iter_mut() { + if *c as u8 == 160u8 { + *c = ' '; + } + } + return string_vec; + } + + #[allow(dead_code)] + fn load_ascii_map(&mut self, level: &prefab_levels::PrefabLevel) { + let string_vec = PrefabBuilder::read_ascii_to_vec(level.template); + + let mut i = 0; + for y in 0..level.height { + for x in 0..level.width { + if x < self.map.width as usize && y < self.map.height as usize { + let idx = self.map.xy_idx(x as i32, y as i32); + self.char_to_map(string_vec[i], idx); + } + i += 1; + } + } + } } diff --git a/src/map_builders/prefab_builder/prefab_levels.rs b/src/map_builders/prefab_builder/prefab_levels.rs new file mode 100644 index 0000000..c1f56c5 --- /dev/null +++ b/src/map_builders/prefab_builder/prefab_levels.rs @@ -0,0 +1,53 @@ +#[derive(PartialEq, Copy, Clone)] +pub struct PrefabLevel { + pub template: &'static str, + pub width: usize, + pub height: usize, +} + +pub const WFC_POPULATED: PrefabLevel = PrefabLevel { template: LEVEL_MAP, width: 80, height: 43 }; + +const LEVEL_MAP: &str = " +################################################################################ +#          ########################################################    ######### +#    @     ######    #########       ####     ###################        ####### +#          ####   g  #                          ###############            ##### +#          #### #    # #######       ####       #############                ### +##### ######### #    # #######       #########  ####    #####                ### +##### ######### ###### #######   o   #########  #### ## #####                ### +##                        ####       #########   ### ##         o            ### +##### ######### ###       ####       #######         ## #####                ### +##### ######### ###       ####       ####### #   ### ## #####                ### +##### ######### ###       ####       ####### #######    #####     o          ### +###          ## ###       ####       ####### ################                ### +###          ## ###   o   ###### ########### #   ############                ### +###          ## ###       ###### ###########     ###                         ### +###    %                  ###### ########### #   ###   !   ##                ### +###          ## ###              ######   ## #######       ##                ### +###          ## ###       ## ### #####     # ########################      ##### +###          ## ###       ## ### #####     # #   ######################    ##### +#### ## ####### ###### ##### ### ####          o ###########     ######    ##### +#### ## ####### ###### ####   ## ####        #   #########         ###### ###### +#    ## ####### ###### ####   ## ####        ############           ##### ###### +# g  ## ####### ###### ####   ##        %    ###########   o      o  #### #    # +#    ## ###            ####   ## ####        #   #######   ##    ##  ####   g  # +#######                  ####### ####            ######     !    !    ### #    # +######                     ##### ####        #   ######               ### ###### +#####                            #####     # ##########               ### ###### +#####           !           ### ######     # ##########      o##o     ### #   ## +#####                       ### #######   ## #   ######               ###   g ## +#   ##                     #### ######## ###   o #######  ^########^ #### #   ## +# g    #                 ###### ######## #####   #######  ^        ^ #### ###### +#   ##g####           ######    ######## ################           ##### ###### +#   ## ########## ##########    ######## #################         ######      # +#####   ######### ########## %  ######## ###################     ######## ##   # +#### ### ######## ##########    ######## #################### ##########   #   # +### ##### ######   #########    ########          ########### #######   # g#   # +### #####           ###############      ###      ########### #######   ####   # +### ##### ####       ############## ######## g  g ########### ####         # ^ # +#### ###^####         ############# ########      #####       ####      # g#   # +#####   ######       ###            ########      ##### g     ####   !  ####^^ # +#!%^## ###  ##           ########## ########  gg                 g         # > # +#!%^   ###  ###     ############### ########      ##### g     ####      # g#   # +# %^##  ^   ###     ############### ########      #####       ################## +################################################################################";