Move more map procedure stuff into the map builder
This commit is contained in:
parent
a54377e4b7
commit
6b48111a97
26
src/main.rs
26
src/main.rs
@ -384,14 +384,16 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build a new map and place the player
|
// Build a new map and place the player
|
||||||
let worldmap;
|
let mut builder = map_builders::random_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);
|
||||||
|
|
||||||
let (newmap, start) = map_builders::build_random_map(current_depth + 1);
|
let (newmap, start) = builder.build_map(current_depth + 1);
|
||||||
|
|
||||||
*worldmap_resource = newmap;
|
*worldmap_resource = newmap;
|
||||||
player_start = start;
|
player_start = start;
|
||||||
@ -399,9 +401,7 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Spawn bad guys
|
// Spawn bad guys
|
||||||
for room in worldmap.rooms.iter().skip(1) {
|
builder.spawn_entities(&mut worldmap, &mut self.ecs, current_depth + 1);
|
||||||
spawner::spawn_room(&mut self.ecs, room, current_depth + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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,11 +445,12 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build a new map and place the player
|
// Build a new map and place the player
|
||||||
let worldmap;
|
let mut builder = map_builders::random_builder();
|
||||||
|
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) = map_builders::build_random_map(1);
|
let (newmap, start) = builder.build_map(1);
|
||||||
|
|
||||||
*worldmap_resource = newmap;
|
*worldmap_resource = newmap;
|
||||||
player_start = start;
|
player_start = start;
|
||||||
@ -457,9 +458,7 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Spawn bad guys
|
// Spawn bad guys
|
||||||
for room in worldmap.rooms.iter().skip(1) {
|
builder.spawn_entities(&mut worldmap, &mut self.ecs, 1);
|
||||||
spawner::spawn_room(&mut self.ecs, room, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
||||||
@ -534,15 +533,14 @@ fn main() -> rltk::BError {
|
|||||||
|
|
||||||
gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());
|
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_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());
|
||||||
for room in map.rooms.iter().skip(1) {
|
builder.spawn_entities(&mut map, &mut gs.ecs, 1);
|
||||||
spawner::spawn_room(&mut gs.ecs, room, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
||||||
|
@ -4,11 +4,14 @@ mod simple_map;
|
|||||||
use crate::{Map, Position};
|
use crate::{Map, Position};
|
||||||
use common::*;
|
use common::*;
|
||||||
use simple_map::SimpleMapBuilder;
|
use simple_map::SimpleMapBuilder;
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
trait MapBuilder {
|
pub trait MapBuilder {
|
||||||
fn build(new_depth: i32) -> (Map, Position);
|
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) {
|
pub fn random_builder() -> Box<dyn MapBuilder> {
|
||||||
SimpleMapBuilder::build(new_depth)
|
// Note that until we have a second map type, this isn't even slightly random
|
||||||
|
Box::new(SimpleMapBuilder {})
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,24 @@
|
|||||||
use super::{apply_horizontal_tunnel, apply_room_to_map, apply_vertical_tunnel, MapBuilder};
|
use super::{apply_horizontal_tunnel, apply_room_to_map, apply_vertical_tunnel, MapBuilder};
|
||||||
|
use crate::spawner;
|
||||||
use crate::{Map, Position, Rect, TileType};
|
use crate::{Map, Position, Rect, TileType};
|
||||||
use rltk::RandomNumberGenerator;
|
use rltk::RandomNumberGenerator;
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
pub struct SimpleMapBuilder {}
|
pub struct SimpleMapBuilder {}
|
||||||
|
|
||||||
impl MapBuilder for 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 mut map = Map::new(new_depth);
|
||||||
let playerpos = SimpleMapBuilder::rooms_and_corridors(&mut map);
|
let playerpos = SimpleMapBuilder::rooms_and_corridors(&mut map);
|
||||||
|
|
||||||
(map, playerpos)
|
(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 {
|
impl SimpleMapBuilder {
|
||||||
|
Loading…
Reference in New Issue
Block a user