aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Pasquet <dev@etenil.net>2019-11-20 17:04:23 +0000
committerGuillaume Pasquet <dev@etenil.net>2019-11-20 17:04:23 +0000
commit337e7928945f37bb422cbf0a7905763dc68cc8be (patch)
treeb8a8ae74b97f296b0208a5169ed7350b19b9a02a
parent15ccde4be6585865d01d9a620778dbcf5d8d998d (diff)
Ensure room sanity
-rw-r--r--src/tiling.rs12
-rw-r--r--src/world.rs5
2 files changed, 16 insertions, 1 deletions
diff --git a/src/tiling.rs b/src/tiling.rs
index 81b7be4..f1630d6 100644
--- a/src/tiling.rs
+++ b/src/tiling.rs
@@ -1,11 +1,15 @@
pub struct TileGrid {
grid: Vec<Vec<TileType>>,
+ 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 {
@@ -42,6 +46,14 @@ impl TileGrid {
pub fn block_at(&self, x: usize, y: usize) -> &TileType {
&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: &TileType) -> &str {
diff --git a/src/world.rs b/src/world.rs
index a315111..451b091 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, TileType::Wall);