From f4f1800770243a7e96e71d2bb7f0a30e5a5fb915 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 12 Nov 2021 14:12:15 -0500 Subject: [PATCH] Complete Chapter 1.12 --- src/main.rs | 7 ++++--- src/spawner.rs | 14 +++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9eb07b2..5ba1e39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -313,9 +313,10 @@ impl State { // Build a new map and place the player let worldmap; + let current_depth; { let mut worldmap_resource = self.ecs.write_resource::(); - let current_depth = worldmap_resource.depth; + current_depth = worldmap_resource.depth; *worldmap_resource = Map::new_map_rooms_and_corridors(current_depth + 1); worldmap = worldmap_resource.clone(); @@ -323,7 +324,7 @@ impl State { // Spawn bad guys for room in worldmap.rooms.iter().skip(1) { - spawner::spawn_room(&mut self.ecs, room); + spawner::spawn_room(&mut self.ecs, room, current_depth + 1); } // Place the player and update resources @@ -403,7 +404,7 @@ fn main() -> rltk::BError { gs.ecs.insert(rltk::RandomNumberGenerator::new()); for room in map.rooms.iter().skip(1) { - spawner::spawn_room(&mut gs.ecs, room); + spawner::spawn_room(&mut gs.ecs, room, 1); } gs.ecs.insert(map); diff --git a/src/spawner.rs b/src/spawner.rs index 90c6e99..995eac1 100644 --- a/src/spawner.rs +++ b/src/spawner.rs @@ -36,26 +36,26 @@ pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity { const MAX_MONSTERS: i32 = 4; -fn room_table() -> RandomTable { +fn room_table(map_depth: i32) -> RandomTable { RandomTable::new() .add("Goblin", 10) - .add("Orc", 1) + .add("Orc", 1 + map_depth) .add("Health Potion", 7) - .add("Fireball Scroll", 2) - .add("Confusion Scroll", 2) + .add("Fireball Scroll", 2 + map_depth) + .add("Confusion Scroll", 2 + map_depth) .add("Magic Missile Scroll", 4) } /// fills a room with stuff! #[allow(clippy::map_entry)] -pub fn spawn_room(ecs: &mut World, room: &Rect) { - let spawn_table = room_table(); +pub fn spawn_room(ecs: &mut World, room: &Rect, map_depth: i32) { + let spawn_table = room_table(map_depth); let mut spawn_points: HashMap = HashMap::new(); // Scope to keep the borrow checker happy { let mut rng = ecs.write_resource::(); - let num_spawns = rng.roll_dice(1, MAX_MONSTERS + 3) - 3; + let num_spawns = rng.roll_dice(1, MAX_MONSTERS + 3) + (map_depth - 1) - 3; for _i in 0..num_spawns { let mut added = false;