Skip to content

Commit

Permalink
feat(map): Add prefabricated map
Browse files Browse the repository at this point in the history
It is now possible to add a prefabricated map and add that section
to an existing map.
  • Loading branch information
yngtdd committed Apr 8, 2022
1 parent 0645d7d commit f60f465
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/map_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::prelude::*;
mod automata;
mod drunkard;
mod empty;
mod prefab;
mod rooms;

const NUM_ROOMS: usize = 20;
Expand All @@ -21,8 +22,16 @@ pub struct MapBuilder {

impl MapBuilder {
pub fn new(rng: &mut RandomNumberGenerator) -> Self {
let mut architect = drunkard::DrunkardsWalkArchitect {};
architect.new(rng)
let mut architect: Box<dyn MapArchitect> = match rng.range(0, 4) {
0 => Box::new(drunkard::DrunkardsWalkArchitect {}),
1 => Box::new(rooms::RoomsArchitect {}),
2 => Box::new(automata::CellularAutomataArchitect {}),
_ => Box::new(empty::EmptyArchitect {}),
};

let mut mb = architect.new(rng);
prefab::apply_prefab(&mut mb, rng);
mb
}

fn fill(&mut self, tile: TileType) {
Expand Down
107 changes: 107 additions & 0 deletions src/map_builder/prefab.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use crate::prelude::*;

const FORTRESS: (&str, i32, i32) = (
"
------------
---######---
---#----#---
---#-M--#---
-###----###-
--M------M--
-###----###-
---#----#---
---#----#---
---######---
------------
",
12,
11,
);

pub fn apply_prefab(mb: &mut MapBuilder, rng: &mut RandomNumberGenerator) {
let mut placement = None;

let dijkstra_map = DijkstraMap::new(
SCREEN_WIDTH,
SCREEN_HEIGHT,
&vec![mb.map.point2d_to_index(mb.player_start)],
&mb.map,
1024.0,
);

let mut attempts = 0;
while placement.is_none() && attempts < 10 {
let dimensions = Rect::with_size(
rng.range(0, SCREEN_WIDTH - FORTRESS.1),
rng.range(0, SCREEN_HEIGHT - FORTRESS.2),
FORTRESS.1,
FORTRESS.2,
);

let mut can_place = false;
dimensions.for_each(|pt| {
let idx = mb.map.point2d_to_index(pt);
let distance = dijkstra_map.map[idx];
if distance < 2000.0 && distance > 20.0 && mb.amulet_start != pt {
can_place = true;
}
});

if can_place {
placement = Some(Point::new(dimensions.x1, dimensions.y1));
let points = dimensions.point_set();
mb.monster_spawns.retain(|pt| !points.contains(pt));
}

attempts += 1;
}

// if let Some(placement) = placement {// (9)
// let string_vec : Vec<char> = FORTRESS.0
// .chars().filter(|a| *a != '\r' && *a !='\n')
// .collect();// (10)
// let mut i = 0;// (11)
// for ty in placement.y .. placement.y + FORTRESS.2 {// (12)
// for tx in placement.x .. placement.x + FORTRESS.1 {
// let idx = map_idx(tx, ty);
// let c = string_vec[i];// (13)
// match c {// (14)
// 'M' => {// (15)
// mb.map.tiles[idx] = TileType::Floor;
// mb.monster_spawns.push(Point::new(tx, ty));
// }
// '-' => mb.map.tiles[idx] = TileType::Floor,// (16)
// '#' => mb.map.tiles[idx] = TileType::Wall,
// _ => println!("No idea what to do with [{}]", c)// (17)
// }
// i += 1;
// }
// }
// }

if let Some(placement) = placement {
let string_vec: Vec<char> = FORTRESS
.0
.chars()
.filter(|a| *a != '\r' && *a != '\n')
.collect();

let mut i = 0;
for ty in placement.y..placement.y + FORTRESS.2 {
for tx in placement.x..placement.x + FORTRESS.1 {
let idx = map_idx(tx, ty);
let c = string_vec[i];
match c {
'M' => {
mb.map.tiles[idx] = TileType::Floor;
mb.monster_spawns.push(Point::new(tx, ty));
}
'-' => mb.map.tiles[idx] = TileType::Floor,
'#' => mb.map.tiles[idx] = TileType::Wall,
_ => println!("No idea what to do with [{}]", c),
}
i += 1;
}
}
}
}

0 comments on commit f60f465

Please sign in to comment.