aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/tiling.rs23
-rw-r--r--src/world.rs97
2 files changed, 91 insertions, 29 deletions
diff --git a/src/tiling.rs b/src/tiling.rs
index fc9f4d4..c79d6ef 100644
--- a/src/tiling.rs
+++ b/src/tiling.rs
@@ -19,10 +19,7 @@ pub struct Tile {
impl Tile {
pub fn new(tile_type: TileType, visible: bool) -> Self {
- Tile {
- tile_type,
- visible,
- }
+ Tile { tile_type, visible }
}
pub fn get_type(&self) -> &TileType {
@@ -34,21 +31,25 @@ impl From<TileType> for Tile {
fn from(tile_type: TileType) -> Self {
Tile {
tile_type,
- visible: true, // <--- TODO: this set the default beaviour
- // - true: all tiles of world and entities will be drawn
- // - false: only draw tiles visible for the player
+ visible: true, // <--- TODO: this set the default beaviour
+ // - true: all tiles of world and entities will be drawn
+ // - false: only draw tiles visible for the player
}
}
}
pub struct TileGrid {
grid: Vec<Vec<Tile>>,
+ xsize: usize,
+ ysize: usize,
}
impl TileGrid {
pub fn new(xsize: usize, ysize: usize) -> TileGrid {
let mut grid = TileGrid {
grid: Vec::with_capacity(ysize),
+ xsize,
+ ysize,
};
for _ in 0..ysize {
@@ -85,6 +86,14 @@ impl TileGrid {
pub fn block_at(&self, x: usize, y: usize) -> &Tile {
&self.grid[y + 1][x]
}
+
+ pub fn xsize(&self) -> usize {
+ self.xsize
+ }
+
+ pub fn ysize(&self) -> usize {
+ self.ysize
+ }
}
pub fn tile_to_str(tile: &Tile) -> &str {
diff --git a/src/world.rs b/src/world.rs
index 98f49c2..507e6b8 100644
--- a/src/world.rs
+++ b/src/world.rs
@@ -50,10 +50,13 @@ impl Room {
impl Tileable for Room {
fn tile(&self, grid: &mut TileGrid) -> Result<(), String> {
- // TODO: Detect if the room would leave the grid.
let endx = self.start.0 + self.width;
let endy = self.start.1 + self.height;
+ if endx >= grid.xsize() || endy > grid.ysize() {
+ return Err(String::from("Room outside of grid bounds"));
+ }
+
// Set the walls
for x in self.start.0..=endx {
grid.set_empty_tile(x, self.start.1, Tile::from(TileType::Wall));
@@ -115,16 +118,22 @@ impl Corridor {
));
}
- let length = distance(start, end);
- if length < 1.0 {
+ let (dir, length) = if start.0 == end.0 {
+ (
+ CorridorType::Vertical,
+ start.1.max(end.1) - start.1.min(end.1),
+ )
+ } else {
+ (
+ CorridorType::Horizontal,
+ start.0.max(end.0) - start.0.min(end.0),
+ )
+ };
+
+ if length == 0 {
return Err(String::from("Can't create 0-length corridor"));
}
- let dir = if start.0 == end.0 {
- CorridorType::Vertical
- } else {
- CorridorType::Horizontal
- };
let origin = match dir {
CorridorType::Horizontal => {
if start.0 < end.0 {
@@ -142,7 +151,7 @@ impl Corridor {
}
};
- Ok(Corridor::new(origin, length.round() as usize, dir))
+ Ok(Corridor::new(origin, length, dir))
}
pub fn link(start: Point, end: Point) -> Result<Vec<Corridor>, String> {
@@ -231,19 +240,6 @@ pub trait Generatable {
fn generate(&mut self);
}
-fn hor_dist(point1: Point, point2: Point) -> f32 {
- point2.0 as f32 - point1.0 as f32
-}
-
-fn ver_dist(point1: Point, point2: Point) -> f32 {
- point2.1 as f32 - point1.1 as f32
-}
-
-/// The distance between 2 points
-fn distance(point1: Point, point2: Point) -> f32 {
- (hor_dist(point1, point2).powf(2.0) + ver_dist(point1, point2).powf(2.0)).sqrt()
-}
-
impl Dungeon {
pub fn new(xsize: usize, ysize: usize, depth: usize) -> Dungeon {
Dungeon {
@@ -502,6 +498,63 @@ mod tests {
}
#[test]
+ fn test_link_corridors_returns_a_vec_of_corridors_on_reversed_diagonal_points() {
+ let cor = Corridor::link((5, 5), (0, 0)).unwrap();
+
+ let exp_horz = vec![
+ Corridor::new((0, 5), 5, CorridorType::Horizontal),
+ Corridor::new((0, 0), 5, CorridorType::Vertical),
+ ];
+ let exp_vert = vec![
+ Corridor::new((5, 0), 5, CorridorType::Vertical),
+ Corridor::new((0, 0), 5, CorridorType::Horizontal),
+ ];
+
+ match cor[0].direction {
+ CorridorType::Horizontal => assert_eq!(cor, exp_horz),
+ CorridorType::Vertical => assert_eq!(cor, exp_vert),
+ };
+ }
+
+ #[test]
+ fn test_link_corridors_returns_a_vec_of_corridors_on_reversed_vertical_points() {
+ let cor = Corridor::link((0, 5), (5, 0)).unwrap();
+
+ let exp_horz = vec![
+ Corridor::new((0, 5), 5, CorridorType::Horizontal),
+ Corridor::new((5, 0), 5, CorridorType::Vertical),
+ ];
+ let exp_vert = vec![
+ Corridor::new((0, 0), 5, CorridorType::Vertical),
+ Corridor::new((0, 0), 5, CorridorType::Horizontal),
+ ];
+
+ match cor[0].direction {
+ CorridorType::Horizontal => assert_eq!(cor, exp_horz),
+ CorridorType::Vertical => assert_eq!(cor, exp_vert),
+ };
+ }
+
+ #[test]
+ fn test_link_corridors_returns_a_vec_of_corridors_on_reversed_horizontal_points() {
+ let cor = Corridor::link((5, 0), (0, 5)).unwrap();
+
+ let exp_horz = vec![
+ Corridor::new((0, 0), 5, CorridorType::Horizontal),
+ Corridor::new((0, 0), 5, CorridorType::Vertical),
+ ];
+ let exp_vert = vec![
+ Corridor::new((5, 0), 5, CorridorType::Vertical),
+ Corridor::new((0, 5), 5, CorridorType::Horizontal),
+ ];
+
+ match cor[0].direction {
+ CorridorType::Horizontal => assert_eq!(cor, exp_horz),
+ CorridorType::Vertical => assert_eq!(cor, exp_vert),
+ };
+ }
+
+ #[test]
fn test_link_corridors_with_horizontal_aligned_points_returns_one_corridor() {
let cor = Corridor::link((0, 0), (5, 0)).unwrap();