Refactor
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
use specs::World;
|
||||
|
||||
use crate::{Map, Position};
|
||||
use crate::map_builders::simple_map::SimpleMapBuilder;
|
||||
use crate::{Map, Position};
|
||||
|
||||
pub mod common;
|
||||
pub mod simple_map;
|
||||
mod common;
|
||||
mod simple_map;
|
||||
|
||||
pub trait MapBuilder {
|
||||
fn build_map(&mut self);
|
||||
fn spawn_entities(&mut self, ecs: &mut World);
|
||||
fn get_map(&self) -> Map;
|
||||
fn get_starting_position(&self) -> Position;
|
||||
fn get_snapshot_history(&self) -> Vec<Map>;
|
||||
fn take_snapshot(&mut self);
|
||||
}
|
||||
|
||||
pub fn new_random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
|
||||
Box::new(SimpleMapBuilder::new(new_depth))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
use rltk::RandomNumberGenerator;
|
||||
use specs::prelude::*;
|
||||
|
||||
use crate::{Map, Position, spawner, TileType};
|
||||
use crate::map_builders::{common, MapBuilder};
|
||||
use crate::rect::Rect;
|
||||
use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER};
|
||||
|
||||
pub struct SimpleMapBuilder {
|
||||
map: Map,
|
||||
starting_position: Position,
|
||||
depth: i32,
|
||||
rooms: Vec<Rect>,
|
||||
history: Vec<Map>,
|
||||
}
|
||||
|
||||
impl SimpleMapBuilder {
|
||||
@@ -19,6 +20,7 @@ impl SimpleMapBuilder {
|
||||
map: Map::new(new_depth),
|
||||
depth: new_depth,
|
||||
rooms: Vec::new(),
|
||||
history: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +45,7 @@ impl SimpleMapBuilder {
|
||||
}
|
||||
if ok {
|
||||
common::apply_room_to_map(&mut self.map, &new_room);
|
||||
self.take_snapshot();
|
||||
|
||||
if !self.rooms.is_empty() {
|
||||
let (new_x, new_y) = new_room.center();
|
||||
@@ -57,6 +60,7 @@ impl SimpleMapBuilder {
|
||||
}
|
||||
|
||||
self.rooms.push(new_room);
|
||||
self.take_snapshot();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +69,10 @@ impl SimpleMapBuilder {
|
||||
self.map.tiles[stairs_idx] = TileType::DownStairs;
|
||||
|
||||
let start_pos = self.rooms[0].center();
|
||||
self.starting_position = Position { x: start_pos.0, y: start_pos.1 }
|
||||
self.starting_position = Position {
|
||||
x: start_pos.0,
|
||||
y: start_pos.1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,4 +94,18 @@ impl MapBuilder for SimpleMapBuilder {
|
||||
fn get_starting_position(&self) -> Position {
|
||||
self.starting_position.clone()
|
||||
}
|
||||
|
||||
fn get_snapshot_history(&self) -> Vec<Map> {
|
||||
self.history.clone()
|
||||
}
|
||||
|
||||
fn take_snapshot(&mut self) {
|
||||
if SHOW_MAPGEN_VISUALIZER {
|
||||
let mut snapshot = self.map.clone();
|
||||
for v in snapshot.revealed_tiles.iter_mut() {
|
||||
*v = true;
|
||||
}
|
||||
self.history.push(snapshot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user