From 0bc6dcdf026027f06ae05773e5c55926372969f1 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 2 Apr 2019 22:03:41 +0200 Subject: [PATCH] I prefer this way --- src/engine/map/mod.rs | 24 ++++++++++++------------ src/engine/math/mod.rs | 14 ++------------ 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/engine/map/mod.rs b/src/engine/map/mod.rs index 7ff5c48..e12ec12 100644 --- a/src/engine/map/mod.rs +++ b/src/engine/map/mod.rs @@ -303,23 +303,23 @@ impl Map { } /// Creates the neighbours of a tile. - pub fn neighbours(&self, i: isize, j: isize) -> [Option; 8] { + pub fn neighbours(&self, i: usize, j: usize) -> [Option; 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 { - 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; } } diff --git a/src/engine/math/mod.rs b/src/engine/math/mod.rs index 837b375..087761f 100644 --- a/src/engine/math/mod.rs +++ b/src/engine/math/mod.rs @@ -70,7 +70,7 @@ impl Matrix { } /// 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 Matrix { } } - - /// 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 {