I prefer this way

This commit is contained in:
Thomas Forgione 2019-04-02 22:03:41 +02:00
parent a66e596db0
commit 0bc6dcdf02
No known key found for this signature in database
GPG Key ID: BFD17A2D71B3B5E7
2 changed files with 14 additions and 24 deletions

View File

@ -303,23 +303,23 @@ impl Map {
}
/// Creates the neighbours of a tile.
pub fn neighbours(&self, i: isize, j: isize) -> [Option<CollisionTile>; 8] {
pub fn neighbours(&self, i: usize, j: usize) -> [Option<CollisionTile>; 8] {
[
self.collision_tiles.get_isize(i - 1, j - 1).cloned(),
self.collision_tiles.get_isize(i - 1, j ).cloned(),
self.collision_tiles.get_isize(i - 1, j + 1).cloned(),
self.collision_tiles.get_isize(i , j + 1).cloned(),
self.collision_tiles.get_isize(i + 1, j + 1).cloned(),
self.collision_tiles.get_isize(i + 1, j ).cloned(),
self.collision_tiles.get_isize(i + 1, j - 1).cloned(),
self.collision_tiles.get_isize(i , j - 1).cloned(),
if i > 0 && j > 0 { self.collision_tiles.get(i - 1, j - 1).cloned() } else { None },
if i > 0 { self.collision_tiles.get(i - 1, j ).cloned() } else { None },
if i > 0 { self.collision_tiles.get(i - 1, j + 1).cloned() } else { None },
self.collision_tiles.get(i , j + 1).cloned(),
self.collision_tiles.get(i + 1, j + 1).cloned(),
self.collision_tiles.get(i + 1, j ).cloned(),
if j > 0 { self.collision_tiles.get(i + 1, j - 1).cloned() } else { None },
if j > 0 { self.collision_tiles.get(i , j - 1).cloned() } else { None },
]
}
/// Returns the graphic tile corresponding to the collision tiles.
pub fn graphic_tile(&self, i: usize, j: usize) -> GraphicTile {
if self.collision_tiles[(i, j)].is_full() {
GraphicTile::from_neighbour_options(&self.neighbours(i as isize, j as isize))
GraphicTile::from_neighbour_options(&self.neighbours(i, j))
} else {
GraphicTile(None)
}
@ -327,7 +327,7 @@ impl Map {
/// Returns a tile of the map.
pub fn collision_tile(&self, i: usize, j: usize) -> Option<CollisionTile> {
self.collision_tiles.get((i, j)).cloned()
self.collision_tiles.get(i, j).cloned()
}
/// Changes a tile of the map.
@ -339,7 +339,7 @@ impl Map {
for i in (i - 1) ..= (i + 1) {
for j in (j - 1) ..= (j + 1) {
let new_tile = self.graphic_tile(i, j);
if let Some(tile) = self.graphic_tiles.get_mut((i, j)) {
if let Some(tile) = self.graphic_tiles.get_mut(i, j) {
*tile = new_tile;
}
}

View File

@ -70,7 +70,7 @@ impl<T> Matrix<T> {
}
/// Returns the tile if any, none otherwise.
pub fn get(&self, (row, col): (usize, usize)) -> Option<&T> {
pub fn get(&self, row: usize, col: usize) -> Option<&T> {
if row < self.rows && col < self.cols {
Some(&self[(row, col)])
} else {
@ -78,18 +78,8 @@ impl<T> Matrix<T> {
}
}
/// Returns the tile if any, none otherwise.
pub fn get_isize(&self, row: isize, col: isize) -> Option<&T> {
if row >= 0 && col >= 0 && (row as usize) < self.rows && (col as usize) < self.cols {
Some(&self[(row as usize, col as usize)])
} else {
None
}
}
/// Returns a mutable reference to the tile if any.
pub fn get_mut(&mut self, (row, col): (usize, usize)) -> Option<&mut T> {
pub fn get_mut(&mut self, row: usize, col: usize) -> Option<&mut T> {
if row < self.rows && col < self.cols {
Some(&mut self[(row, col)])
} else {