Move map builder state into map builder

This commit is contained in:
Timothy Warren 2021-12-01 12:03:49 -05:00
parent fdff800b2b
commit feb7988f5f
3 changed files with 66 additions and 53 deletions

View File

@ -384,24 +384,20 @@ impl State {
} }
// Build a new map and place the player // Build a new map and place the player
let mut builder = map_builders::random_builder(); let mut builder;
let mut worldmap;
let current_depth; let current_depth;
let player_start; let player_start;
{ {
let mut worldmap_resource = self.ecs.write_resource::<Map>(); let mut worldmap_resource = self.ecs.write_resource::<Map>();
current_depth = worldmap_resource.depth; current_depth = worldmap_resource.depth;
// builder = map_builders::random_builder(current_depth + 1); builder = map_builders::random_builder(current_depth + 1);
builder.build_map();
let (newmap, start) = builder.build_map(current_depth + 1); *worldmap_resource = builder.get_map();
player_start = builder.get_starting_position();
*worldmap_resource = newmap;
player_start = start;
worldmap = worldmap_resource.clone();
} }
// Spawn bad guys // Spawn bad guys
builder.spawn_entities(&mut worldmap, &mut self.ecs, current_depth + 1); builder.spawn_entities(&mut self.ecs);
// Place the player and update resources // Place the player and update resources
let (player_x, player_y) = (player_start.x, player_start.y); let (player_x, player_y) = (player_start.x, player_start.y);
@ -445,20 +441,17 @@ impl State {
} }
// Build a new map and place the player // Build a new map and place the player
let mut builder = map_builders::random_builder(); let mut builder = map_builders::random_builder(1);
let mut worldmap;
let player_start; let player_start;
{ {
let mut worldmap_resource = self.ecs.write_resource::<Map>(); let mut worldmap_resource = self.ecs.write_resource::<Map>();
let (newmap, start) = builder.build_map(1); builder.build_map();
player_start = builder.get_starting_position();
*worldmap_resource = newmap; *worldmap_resource = builder.get_map();
player_start = start;
worldmap = worldmap_resource.clone();
} }
// Spawn bad guys // Spawn bad guys
builder.spawn_entities(&mut worldmap, &mut self.ecs, 1); builder.spawn_entities(&mut self.ecs);
// Place the player and update resources // Place the player and update resources
let (player_x, player_y) = (player_start.x, player_start.y); let (player_x, player_y) = (player_start.x, player_start.y);
@ -533,14 +526,16 @@ fn main() -> rltk::BError {
gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new()); gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());
let mut builder = map_builders::random_builder(); let mut builder = map_builders::random_builder(1);
let (mut map, player_start) = builder.build_map(1); builder.build_map();
let player_start = builder.get_starting_position();
let map = builder.get_map();
let (player_x, player_y) = (player_start.x, player_start.y); let (player_x, player_y) = (player_start.x, player_start.y);
let player_entity = spawner::player(&mut gs.ecs, player_x, player_y); let player_entity = spawner::player(&mut gs.ecs, player_x, player_y);
gs.ecs.insert(rltk::RandomNumberGenerator::new()); gs.ecs.insert(rltk::RandomNumberGenerator::new());
builder.spawn_entities(&mut map, &mut gs.ecs, 1); builder.spawn_entities(&mut gs.ecs);
gs.ecs.insert(map); gs.ecs.insert(map);
gs.ecs.insert(Point::new(player_x, player_y)); gs.ecs.insert(Point::new(player_x, player_y));

View File

@ -7,11 +7,13 @@ use simple_map::SimpleMapBuilder;
use specs::prelude::*; use specs::prelude::*;
pub trait MapBuilder { pub trait MapBuilder {
fn build_map(&mut self, new_depth: i32) -> (Map, Position); fn build_map(&mut self);
fn spawn_entities(&mut self, map: &Map, ecs: &mut World, new_depth: i32); fn spawn_entities(&mut self, ecs: &mut World);
fn get_map(&self) -> Map;
fn get_starting_position(&self) -> Position;
} }
pub fn random_builder() -> Box<dyn MapBuilder> { pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
// Note that until we have a second map type, this isn't even slightly random // Note that until we have a second map type, this isn't even slightly random
Box::new(SimpleMapBuilder {}) Box::new(SimpleMapBuilder::new(new_depth))
} }

View File

@ -4,25 +4,42 @@ use crate::{Map, Position, Rect, TileType};
use rltk::RandomNumberGenerator; use rltk::RandomNumberGenerator;
use specs::prelude::*; use specs::prelude::*;
pub struct SimpleMapBuilder {} pub struct SimpleMapBuilder {
map: Map,
starting_position: Position,
depth: i32
}
impl MapBuilder for SimpleMapBuilder { impl MapBuilder for SimpleMapBuilder {
fn build_map(&mut self, new_depth: i32) -> (Map, Position) { fn build_map(&mut self) {
let mut map = Map::new(new_depth); self.rooms_and_corridors();
let playerpos = SimpleMapBuilder::rooms_and_corridors(&mut map);
(map, playerpos)
} }
fn spawn_entities(&mut self, map: &Map, ecs: &mut World, _new_depth: i32) { fn spawn_entities(&mut self, ecs: &mut World) {
for room in map.rooms.iter().skip(1) { for room in self.map.rooms.iter().skip(1) {
spawner::spawn_room(ecs, room, 1); spawner::spawn_room(ecs, room, self.depth);
} }
} }
fn get_map(&self) -> Map {
self.map.clone()
}
fn get_starting_position(&self) -> Position {
self.starting_position.clone()
}
} }
impl SimpleMapBuilder { impl SimpleMapBuilder {
fn rooms_and_corridors(map: &mut Map) -> Position { pub fn new(new_depth: i32) -> SimpleMapBuilder {
SimpleMapBuilder{
map: Map::new(new_depth),
starting_position: Position { x: 0, y: 0 },
depth: new_depth
}
}
fn rooms_and_corridors(&mut self) {
const MAX_ROOMS: i32 = 30; const MAX_ROOMS: i32 = 30;
const MIN_SIZE: i32 = 6; const MIN_SIZE: i32 = 6;
const MAX_SIZE: i32 = 10; const MAX_SIZE: i32 = 10;
@ -32,47 +49,46 @@ impl SimpleMapBuilder {
for _ in 0..MAX_ROOMS { for _ in 0..MAX_ROOMS {
let w = rng.range(MIN_SIZE, MAX_SIZE); let w = rng.range(MIN_SIZE, MAX_SIZE);
let h = rng.range(MIN_SIZE, MAX_SIZE); let h = rng.range(MIN_SIZE, MAX_SIZE);
let x = rng.roll_dice(1, map.width - w - 1) - 1; let x = rng.roll_dice(1, self.map.width - w - 1) - 1;
let y = rng.roll_dice(1, map.height - h - 1) - 1; let y = rng.roll_dice(1, self.map.height - h - 1) - 1;
let new_room = Rect::new(x, y, w, h); let new_room = Rect::new(x, y, w, h);
let mut ok = true; let mut ok = true;
for other_room in map.rooms.iter() { for other_room in self.map.rooms.iter() {
if new_room.intersect(other_room) { if new_room.intersect(other_room) {
ok = false; ok = false;
} }
} }
if ok { if ok {
apply_room_to_map(map, &new_room); apply_room_to_map(&mut self.map, &new_room);
if !map.rooms.is_empty() { if !self.map.rooms.is_empty() {
let (new_x, new_y) = new_room.center(); let (new_x, new_y) = new_room.center();
let (prev_x, prev_y) = map.rooms[map.rooms.len() - 1].center(); let (prev_x, prev_y) = self.map.rooms[self.map.rooms.len() - 1].center();
if rng.range(0, 2) == 1 { if rng.range(0, 2) == 1 {
apply_horizontal_tunnel(map, prev_x, new_x, prev_y); apply_horizontal_tunnel(&mut self.map, prev_x, new_x, prev_y);
apply_vertical_tunnel(map, prev_y, new_y, new_x); apply_vertical_tunnel(&mut self.map, prev_y, new_y, new_x);
} else { } else {
apply_vertical_tunnel(map, prev_y, new_y, prev_x); apply_vertical_tunnel(&mut self.map, prev_y, new_y, prev_x);
apply_horizontal_tunnel(map, prev_x, new_x, new_y); apply_horizontal_tunnel(&mut self.map, prev_x, new_x, new_y);
} }
} }
map.rooms.push(new_room); self.map.rooms.push(new_room);
} }
} }
let stairs_position = map.rooms[map.rooms.len() - 1].center(); let stairs_position = self.map.rooms[self.map.rooms.len() - 1].center();
let stairs_idx = map.xy_idx(stairs_position.0, stairs_position.1); let stairs_idx = self.map.xy_idx(stairs_position.0, stairs_position.1);
map.tiles[stairs_idx] = TileType::DownStairs; self.map.tiles[stairs_idx] = TileType::DownStairs;
let start_pos = map.rooms[0].center(); let start_pos = self.map.rooms[0].center();
self.starting_position = Position {
Position {
x: start_pos.0, x: start_pos.0,
y: start_pos.1, y: start_pos.1,
} };
} }
} }