Refactor map generation to return original player position

This commit is contained in:
Timothy Warren 2021-12-01 11:02:39 -05:00
parent 8bb6bf35d6
commit 7f8c16a79f
3 changed files with 29 additions and 15 deletions

View File

@ -386,11 +386,15 @@ impl State {
// Build a new map and place the player
let worldmap;
let current_depth;
let player_start;
{
let mut worldmap_resource = self.ecs.write_resource::<Map>();
current_depth = worldmap_resource.depth;
*worldmap_resource = map_builders::build_random_map(current_depth + 1);
let (newmap, start) = map_builders::build_random_map(current_depth + 1);
*worldmap_resource = newmap;
player_start = start;
worldmap = worldmap_resource.clone();
}
@ -400,7 +404,7 @@ impl State {
}
// Place the player and update resources
let (player_x, player_y) = worldmap.rooms[0].center();
let (player_x, player_y) = (player_start.x, player_start.y);
let mut player_position = self.ecs.write_resource::<Point>();
*player_position = Point::new(player_x, player_y);
let mut position_components = self.ecs.write_storage::<Position>();
@ -442,9 +446,13 @@ impl State {
// Build a new map and place the player
let worldmap;
let player_start;
{
let mut worldmap_resource = self.ecs.write_resource::<Map>();
*worldmap_resource = map_builders::build_random_map(1);
let (newmap, start) = map_builders::build_random_map(1);
*worldmap_resource = newmap;
player_start = start;
worldmap = worldmap_resource.clone();
}
@ -454,7 +462,7 @@ impl State {
}
// Place the player and update resources
let (player_x, player_y) = worldmap.rooms[0].center();
let (player_x, player_y) = (player_start.x, player_start.y);
let player_entity = spawner::player(&mut self.ecs, player_x, player_y);
let mut player_position = self.ecs.write_resource::<Point>();
*player_position = Point::new(player_x, player_y);
@ -526,8 +534,8 @@ fn main() -> rltk::BError {
gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());
let map = map_builders::build_random_map(1);
let (player_x, player_y) = map.rooms[0].center();
let (map, player_start) = map_builders::build_random_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);

View File

@ -1,14 +1,14 @@
mod common;
mod simple_map;
use crate::Map;
use crate::{Map, Position};
use common::*;
use simple_map::SimpleMapBuilder;
trait MapBuilder {
fn build(new_depth: i32) -> Map;
fn build(new_depth: i32) -> (Map, Position);
}
pub fn build_random_map(new_depth: i32) -> Map {
pub fn build_random_map(new_depth: i32) -> (Map, Position) {
SimpleMapBuilder::build(new_depth)
}

View File

@ -1,21 +1,20 @@
use super::{apply_horizontal_tunnel, apply_room_to_map, apply_vertical_tunnel, MapBuilder};
use crate::{Map, Rect, TileType};
use crate::{Map, Position, Rect, TileType};
use rltk::RandomNumberGenerator;
pub struct SimpleMapBuilder {}
impl MapBuilder for SimpleMapBuilder {
fn build(new_depth: i32) -> Map {
fn build(new_depth: i32) -> (Map, Position) {
let mut map = Map::new(new_depth);
let playerpos = SimpleMapBuilder::rooms_and_corridors(&mut map);
SimpleMapBuilder::rooms_and_corridors(&mut map);
map
(map, playerpos)
}
}
impl SimpleMapBuilder {
fn rooms_and_corridors(map: &mut Map) {
fn rooms_and_corridors(map: &mut Map) -> Position {
const MAX_ROOMS: i32 = 30;
const MIN_SIZE: i32 = 6;
const MAX_SIZE: i32 = 10;
@ -60,5 +59,12 @@ impl SimpleMapBuilder {
let stairs_position = map.rooms[map.rooms.len() - 1].center();
let stairs_idx = map.xy_idx(stairs_position.0, stairs_position.1);
map.tiles[stairs_idx] = TileType::DownStairs;
let start_pos = map.rooms[0].center();
Position {
x: start_pos.0,
y: start_pos.1,
}
}
}