From 6b48111a97c1b446b9c3ab5c6e34b223276541e3 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 1 Dec 2021 11:41:29 -0500 Subject: [PATCH] Move more map procedure stuff into the map builder --- src/main.rs | 26 ++++++++++++-------------- src/map_builders/mod.rs | 11 +++++++---- src/map_builders/simple_map.rs | 10 +++++++++- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index efe06a1..40e4cf7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -384,14 +384,16 @@ impl State { } // Build a new map and place the player - let worldmap; + let mut builder = map_builders::random_builder(); + let mut worldmap; let current_depth; let player_start; { let mut worldmap_resource = self.ecs.write_resource::(); current_depth = worldmap_resource.depth; + // builder = map_builders::random_builder(current_depth + 1); - let (newmap, start) = map_builders::build_random_map(current_depth + 1); + let (newmap, start) = builder.build_map(current_depth + 1); *worldmap_resource = newmap; player_start = start; @@ -399,9 +401,7 @@ impl State { } // Spawn bad guys - for room in worldmap.rooms.iter().skip(1) { - spawner::spawn_room(&mut self.ecs, room, current_depth + 1); - } + builder.spawn_entities(&mut worldmap, &mut self.ecs, current_depth + 1); // Place the player and update resources let (player_x, player_y) = (player_start.x, player_start.y); @@ -445,11 +445,12 @@ impl State { } // Build a new map and place the player - let worldmap; + let mut builder = map_builders::random_builder(); + let mut worldmap; let player_start; { let mut worldmap_resource = self.ecs.write_resource::(); - let (newmap, start) = map_builders::build_random_map(1); + let (newmap, start) = builder.build_map(1); *worldmap_resource = newmap; player_start = start; @@ -457,9 +458,7 @@ impl State { } // Spawn bad guys - for room in worldmap.rooms.iter().skip(1) { - spawner::spawn_room(&mut self.ecs, room, 1); - } + builder.spawn_entities(&mut worldmap, &mut self.ecs, 1); // Place the player and update resources let (player_x, player_y) = (player_start.x, player_start.y); @@ -534,15 +533,14 @@ fn main() -> rltk::BError { gs.ecs.insert(SimpleMarkerAllocator::::new()); - let (map, player_start) = map_builders::build_random_map(1); + let mut builder = map_builders::random_builder(); + let (mut map, player_start) = builder.build_map(1); let (player_x, player_y) = (player_start.x, player_start.y); let player_entity = spawner::player(&mut gs.ecs, player_x, player_y); gs.ecs.insert(rltk::RandomNumberGenerator::new()); - for room in map.rooms.iter().skip(1) { - spawner::spawn_room(&mut gs.ecs, room, 1); - } + builder.spawn_entities(&mut map, &mut gs.ecs, 1); gs.ecs.insert(map); gs.ecs.insert(Point::new(player_x, player_y)); diff --git a/src/map_builders/mod.rs b/src/map_builders/mod.rs index cd7621b..bbc68c6 100644 --- a/src/map_builders/mod.rs +++ b/src/map_builders/mod.rs @@ -4,11 +4,14 @@ mod simple_map; use crate::{Map, Position}; use common::*; use simple_map::SimpleMapBuilder; +use specs::prelude::*; -trait MapBuilder { - fn build(new_depth: i32) -> (Map, Position); +pub trait MapBuilder { + fn build_map(&mut self, new_depth: i32) -> (Map, Position); + fn spawn_entities(&mut self, map: &Map, ecs: &mut World, new_depth: i32); } -pub fn build_random_map(new_depth: i32) -> (Map, Position) { - SimpleMapBuilder::build(new_depth) +pub fn random_builder() -> Box { + // Note that until we have a second map type, this isn't even slightly random + Box::new(SimpleMapBuilder {}) } diff --git a/src/map_builders/simple_map.rs b/src/map_builders/simple_map.rs index 20f6348..010a5a8 100644 --- a/src/map_builders/simple_map.rs +++ b/src/map_builders/simple_map.rs @@ -1,16 +1,24 @@ use super::{apply_horizontal_tunnel, apply_room_to_map, apply_vertical_tunnel, MapBuilder}; +use crate::spawner; use crate::{Map, Position, Rect, TileType}; use rltk::RandomNumberGenerator; +use specs::prelude::*; pub struct SimpleMapBuilder {} impl MapBuilder for SimpleMapBuilder { - fn build(new_depth: i32) -> (Map, Position) { + fn build_map(&mut self, new_depth: i32) -> (Map, Position) { let mut map = Map::new(new_depth); let playerpos = SimpleMapBuilder::rooms_and_corridors(&mut map); (map, playerpos) } + + fn spawn_entities(&mut self, map: &Map, ecs: &mut World, _new_depth: i32) { + for room in map.rooms.iter().skip(1) { + spawner::spawn_room(ecs, room, 1); + } + } } impl SimpleMapBuilder {