diff --git a/demo/src/main.rs b/demo/src/main.rs index 783e539..89f6f87 100644 --- a/demo/src/main.rs +++ b/demo/src/main.rs @@ -38,13 +38,13 @@ impl Tile for MapTiles { let map = world.read_resource::(); let pos = Point::new(p.x as usize, p.y as usize); if map.starting_point == Some(pos) { - Some(64) + Some(160) } else if map.exit_point == Some(pos) { - Some(62) + Some(12) } else if map.at(p.x as usize, p.y as usize) == TileType::Wall { - Some(35) + Some(320) } else { - Some(46) + Some(19) } } @@ -104,7 +104,7 @@ impl SimpleState for PlayState { init_map(&mut world); let map_sprite_sheet_handle = - load_tiles_sprite_sheet(world, "texture/ascii.png", "texture/ascii.ron"); + load_tiles_sprite_sheet(world, "texture/basic.png", "texture/basic.ron"); let (width, height) = { let dim = world.read_resource::(); diff --git a/src/dungeon/map.rs b/src/dungeon/map.rs index 44c8f4d..ea3f2e3 100644 --- a/src/dungeon/map.rs +++ b/src/dungeon/map.rs @@ -11,7 +11,7 @@ use std::fmt; /// Position on the map -#[derive(PartialEq, Copy, Clone, Debug, Eq, Hash)] +#[derive(Default, PartialEq, Copy, Clone, Debug, Eq, Hash)] pub struct Point { pub x: usize, pub y: usize @@ -84,8 +84,12 @@ impl Map { /// Get TileType at the given location pub fn at(&self, x: usize, y: usize) -> TileType { - let idx = y * self.width + x; - self.tiles[idx] + if x >= self.width || y >= self.height { + TileType::Wall + } else { + let idx = (y as usize) * self.width + (x as usize); + self.tiles[idx] + } } /// Get available exists from the given tile @@ -109,14 +113,15 @@ impl Map { // Check if given tile can be accessed fn is_exit_valid(&self, x:usize, y:usize) -> bool { - if x < 1 || x > self.width-1 || y < 1 || y > self.height-1 { return false; } self.at(x, y) == TileType::Floor } /// Modify tile at the given location pub fn set_tile(&mut self, x: usize, y: usize, tile: TileType) { - let idx = y * self.width + x; - self.tiles[idx] = tile; + if x < self.width && y < self.height { + let idx = (y as usize) * self.width + (x as usize); + self.tiles[idx] = tile; + } } }