Move more map procedure stuff into the map builder

This commit is contained in:
Timothy Warren 2021-12-01 11:41:29 -05:00
parent a54377e4b7
commit 6b48111a97
3 changed files with 28 additions and 19 deletions

View File

@ -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::<Map>();
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::<Map>();
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::<SerializeMe>::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));

View File

@ -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<dyn MapBuilder> {
// Note that until we have a second map type, this isn't even slightly random
Box::new(SimpleMapBuilder {})
}

View File

@ -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 {