From c841d08d14366796ab4853f79453b328d73df364 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 19 Nov 2018 15:21:02 +0100 Subject: [PATCH] Cleaning for clippy --- src/app/game.rs | 2 +- src/engine/character/mod.rs | 10 ++-- src/engine/controls/mod.rs | 2 +- src/engine/map/mod.rs | 99 +++++++++++++++++-------------------- src/engine/math/mod.rs | 4 +- src/engine/renderer/mod.rs | 2 +- 6 files changed, 53 insertions(+), 66 deletions(-) diff --git a/src/app/game.rs b/src/app/game.rs index 857ff8e..3aa1bbd 100644 --- a/src/app/game.rs +++ b/src/app/game.rs @@ -68,7 +68,7 @@ fn main() { let controls = if gamepads.is_empty() { Controls::default_keyboard() } else { - gamepads[0].clone() + gamepads[0] }; let mut character = Character::with_controls(controls); diff --git a/src/engine/character/mod.rs b/src/engine/character/mod.rs index 7eed504..55f2afa 100644 --- a/src/engine/character/mod.rs +++ b/src/engine/character/mod.rs @@ -97,7 +97,7 @@ impl Character { Character { position: Vector2::new(0.0, 0.0), speed: Vector2::new(0.0, 0.0), - controls: controls, + controls, side: Side::Right, jump_counter: 1, max_jump: 1, @@ -162,12 +162,8 @@ impl Updatable for Character { let mut force: Vector2 = Vector2::new(0.0, 0.0); - match self.controls { - Some(ref controls) => { - force += controls.direction(); - } - - _ => (), + if let Some(ref controls) = self.controls { + force += controls.direction(); } if let Some(side) = Side::from_force(force) { diff --git a/src/engine/controls/mod.rs b/src/engine/controls/mod.rs index a30429e..135c3b4 100644 --- a/src/engine/controls/mod.rs +++ b/src/engine/controls/mod.rs @@ -202,7 +202,7 @@ impl GamepadMap { } Some(GamepadMap { - id: id, + id, jump_button: 1, run_button: 0, left_right_axis: Axis::X, diff --git a/src/engine/map/mod.rs b/src/engine/map/mod.rs index 52844e2..e8b1cb6 100644 --- a/src/engine/map/mod.rs +++ b/src/engine/map/mod.rs @@ -26,16 +26,16 @@ pub enum CollisionAxis { impl CollisionAxis { /// Returns true if the collision occured on X axis. - pub fn is_x(&self) -> bool { - match *self { + pub fn is_x(self) -> bool { + match self { CollisionAxis::Y => false, _ => true, } } /// Returns true if the collision occured on Y axis. - pub fn is_y(&self) -> bool { - match *self { + pub fn is_y(self) -> bool { + match self { CollisionAxis::X => false, _ => true, } @@ -120,32 +120,32 @@ pub enum GraphicTile { impl GraphicTile { /// Checks if a graphic tile has a top border. - pub fn is_top(&self) -> bool { - match *self { + pub fn is_top(self) -> bool { + match self { GraphicTile::TopLeft | GraphicTile::Top | GraphicTile::TopRight => true, _ => false, } } /// Checks if a graphic tile has a left border. - pub fn is_left(&self) -> bool { - match *self { + pub fn is_left(self) -> bool { + match self { GraphicTile::TopLeft | GraphicTile::Left | GraphicTile::BottomLeft => true, _ => false, } } /// Checks if a graphic tile has a right border. - pub fn is_right(&self) -> bool { - match *self { + pub fn is_right(self) -> bool { + match self { GraphicTile::TopRight | GraphicTile::Right | GraphicTile::BottomRight => true, _ => false, } } /// Checks if a graphic tile has a bottom border. - pub fn is_bottom(&self) -> bool { - match *self { + pub fn is_bottom(self) -> bool { + match self { GraphicTile::BottomLeft | GraphicTile::Bottom | GraphicTile::BottomRight => true, _ => false, } @@ -178,10 +178,10 @@ impl GraphicTile { bottom: Option) -> GraphicTile { GraphicTile::from_neighbours( - top.unwrap_or(CollisionTile::full()), - left.unwrap_or(CollisionTile::full()), - right.unwrap_or(CollisionTile::full()), - bottom.unwrap_or(CollisionTile::full()), + top.unwrap_or_else(CollisionTile::full), + left.unwrap_or_else(CollisionTile::full), + right.unwrap_or_else(CollisionTile::full), + bottom.unwrap_or_else(CollisionTile::full), ) } @@ -227,15 +227,15 @@ impl GraphicTile { } /// Returns the offset to the corresponding graphic tile in the texture. - pub fn offset(&self) -> (i32, i32) { - let vertical = match *self { + pub fn offset(self) -> (i32, i32) { + let vertical = match self { GraphicTile::TopLeft | GraphicTile::Top | GraphicTile::TopRight => 0, GraphicTile::Left | GraphicTile::Center | GraphicTile::Right => 16, GraphicTile::BottomLeft | GraphicTile::Bottom | GraphicTile::BottomRight => 32, _ => 0, }; - let horizontal = match *self { + let horizontal = match self { GraphicTile::TopLeft | GraphicTile::Left | GraphicTile::BottomLeft => 0, GraphicTile::Top | GraphicTile::Center | GraphicTile::Bottom => 16, GraphicTile::TopRight | GraphicTile::Right | GraphicTile::BottomRight => 32, @@ -351,10 +351,10 @@ impl Map { let (i, j) = (i as isize, j as isize); GraphicTile::from_neighbour_options( - tiles.get(((i ) as usize, (j-1) as usize)).map(|x| *x), - tiles.get(((i-1) as usize, (j ) as usize)).map(|x| *x), - tiles.get(((i+1) as usize, (j ) as usize)).map(|x| *x), - tiles.get(((i ) as usize, (j+1) as usize)).map(|x| *x), + tiles.get(((i ) as usize, (j-1) as usize)).cloned(), + tiles.get(((i-1) as usize, (j ) as usize)).cloned(), + tiles.get(((i+1) as usize, (j ) as usize)).cloned(), + tiles.get(((i ) as usize, (j+1) as usize)).cloned(), ) } else { GraphicTile::Hidden @@ -419,8 +419,8 @@ impl Map { let mut new = new; - for col in min_col .. max_col + 1 { - for row in min_row .. max_row + 1 { + for col in min_col ..= max_col { + for row in min_row ..= max_row { let tile_left = col as f32 * 16.0; let tile_top = row as f32 * 16.0; @@ -432,15 +432,12 @@ impl Map { } // Collisions between feet and ground - if self.tiles[(row, col)].0.from_top { - - if old.top + old.height <= tile_top && - new.top + new.height >= tile_top { - - collision_y = true; - new.top = tile_top - new.height; - } + if self.tiles[(row, col)].0.from_top && + old.top + old.height <= tile_top && + new.top + new.height >= tile_top { + collision_y = true; + new.top = tile_top - new.height; } if ! overlap(new, tile) { @@ -448,14 +445,12 @@ impl Map { } // Collisions between right and right wall - if self.tiles[(row, col)].0.from_left { + if self.tiles[(row, col)].0.from_left && + old.left + old.width <= tile_left && + new.left + new.width >= tile_left { - if old.left + old.width <= tile_left && - new.left + new.width >= tile_left { - - collision_x = true; - new.left = tile_left - new.width; - } + collision_x = true; + new.left = tile_left - new.width; } if ! overlap(new, tile) { @@ -463,14 +458,12 @@ impl Map { } // Collisions between left and left wall - if self.tiles[(row, col)].0.from_right { + if self.tiles[(row, col)].0.from_right && + old.left >= tile_left + 16.0 && + new.left <= tile_left + 16.0 { - if old.left >= tile_left + 16.0 && - new.left <= tile_left + 16.0 { - - collision_x = true; - new.left = tile_left + 16.0; - } + collision_x = true; + new.left = tile_left + 16.0; } if ! overlap(new, tile) { @@ -478,14 +471,12 @@ impl Map { } // Collisions between head and roof - if self.tiles[(row, col)].0.from_bottom { + if self.tiles[(row, col)].0.from_bottom && + old.top >= tile_top + 16.0 && + new.top <= tile_top + 16.0 { - if old.top >= tile_top + 16.0 && - new.top <= tile_top + 16.0 { - - collision_y = true; - new.top = tile_top + 16.0; - } + collision_y = true; + new.top = tile_top + 16.0; } } diff --git a/src/engine/math/mod.rs b/src/engine/math/mod.rs index 1729dd6..da379c5 100644 --- a/src/engine/math/mod.rs +++ b/src/engine/math/mod.rs @@ -46,8 +46,8 @@ impl Matrix where T: Clone { /// Creates a matrix from an element and duplicates it. pub fn from_size(rows: usize, cols: usize, element: T) -> Matrix { Matrix { - rows: rows, - cols: cols, + rows, + cols, data: vec![element; rows * cols], } } diff --git a/src/engine/renderer/mod.rs b/src/engine/renderer/mod.rs index 6d9b861..6e02140 100644 --- a/src/engine/renderer/mod.rs +++ b/src/engine/renderer/mod.rs @@ -62,7 +62,7 @@ impl Renderer { window.set_framerate_limit(60); Renderer { - window: window, + window, texture_manager: TextureManager::new(), started: Instant::now(), }