From 66350ac2db1deaf3453987d8af9b83a02953ba83 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sun, 31 Jan 2021 17:57:35 +0100 Subject: fmtd linted cleaned unwraps --- src/main.rs | 87 ++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/src/main.rs b/src/main.rs index 888c494..42595ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ -use ggez; -use ggez::event; +use ggez::event::{self, EventHandler}; use ggez::graphics::{ self, spritebatch::SpriteBatch, Color, DrawMode, DrawParam, FilterMode, Image, Mesh, Rect, StrokeOptions, WrapMode, }; use ggez::input::mouse; use ggez::mint::{Point2, Vector2}; +use ggez::{Context, ContextBuilder, GameResult}; use rand::{ distributions::{Distribution, Standard}, Rng, @@ -49,21 +49,19 @@ impl Cell { pub fn new(position: Point2) -> Cell { Cell { occupant: rand::random(), - position: position, + position, hover: false, clicked: false, } } pub fn contains(&self, position: Point2) -> bool { - if position.x > self.position.x { - if position.y > self.position.y { - if position.x < self.position.x + TILE_WIDTH * TILE_SCALE { - if position.y < self.position.y + TILE_WIDTH * TILE_SCALE { - return true; - } - } - } + if position.x > self.position.x + && position.y > self.position.y + && position.x < self.position.x + TILE_WIDTH * TILE_SCALE + && position.y < self.position.y + TILE_WIDTH * TILE_SCALE + { + return true; } false @@ -85,7 +83,7 @@ impl Cell { self.hover = false; } - pub fn draw(&self, context: &mut ggez::Context, spritebatch: &mut SpriteBatch) { + pub fn draw(&self, context: &mut Context, spritebatch: &mut SpriteBatch) -> GameResult { let source = match self.occupant { Occupant::None => None, Occupant::Green => Some(Rect::new(0.0, 0.0, 0.25, 1.0)), @@ -117,8 +115,8 @@ impl Cell { TILE_HEIGHT * TILE_SCALE, ), Color::from_rgb(255, 100, 100), - ); - graphics::draw(context, &mesh.unwrap(), DrawParam::default()).unwrap(); + )?; + graphics::draw(context, &mesh, DrawParam::default())?; } if self.clicked { let mesh = Mesh::new_rectangle( @@ -131,9 +129,10 @@ impl Cell { TILE_HEIGHT * TILE_SCALE, ), Color::from_rgb(100, 255, 100), - ); - graphics::draw(context, &mesh.unwrap(), DrawParam::default()).unwrap(); + )?; + graphics::draw(context, &mesh, DrawParam::default())?; } + Ok(()) } } @@ -144,7 +143,7 @@ struct Game { } impl Game { - fn new(context: &mut ggez::Context) -> ggez::GameResult { + fn new(context: &mut Context) -> GameResult { let mut image = Image::new(context, "/gem.png")?; image.set_filter(FilterMode::Nearest); image.set_wrap(WrapMode::Mirror, WrapMode::Mirror); @@ -153,34 +152,34 @@ impl Game { let mut y = 0.0; for _ in 0..8 { let mut column = Vec::new(); - column.push(Cell::new(Point2 { x: 0.0, y: y })); + column.push(Cell::new(Point2 { x: 0.0, y })); column.push(Cell::new(Point2 { x: 1.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE), - y: y, + y, })); column.push(Cell::new(Point2 { x: 2.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE), - y: y, + y, })); column.push(Cell::new(Point2 { x: 3.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE), - y: y, + y, })); column.push(Cell::new(Point2 { x: 4.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE), - y: y, + y, })); column.push(Cell::new(Point2 { x: 5.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE), - y: y, + y, })); column.push(Cell::new(Point2 { x: 6.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE), - y: y, + y, })); column.push(Cell::new(Point2 { x: 7.0 * (TILE_WIDTH * TILE_SCALE + BORDER_SIZE), - y: y, + y, })); y += TILE_HEIGHT * TILE_SCALE + BORDER_SIZE; @@ -188,15 +187,15 @@ impl Game { } Ok(Game { - grid: grid, + grid, selected: None, spritebatch: SpriteBatch::new(image), }) } } -impl event::EventHandler for Game { - fn update(&mut self, context: &mut ggez::Context) -> ggez::GameResult { +impl EventHandler for Game { + fn update(&mut self, context: &mut Context) -> GameResult { for row in self.grid.iter_mut() { for cell in row.iter_mut() { cell.hover_off(); @@ -218,13 +217,13 @@ impl event::EventHandler for Game { fn mouse_button_down_event( &mut self, - _context: &mut ggez::Context, + _context: &mut Context, button: mouse::MouseButton, x: f32, y: f32, ) { if button == mouse::MouseButton::Left { - let position = Point2 { x: x, y: y }; + let position = Point2 { x, y }; for (i, row) in self.grid.iter_mut().enumerate() { for (j, cell) in row.iter_mut().enumerate() { if cell.contains(position) { @@ -238,7 +237,7 @@ impl event::EventHandler for Game { fn mouse_button_up_event( &mut self, - _context: &mut ggez::Context, + _context: &mut Context, button: mouse::MouseButton, x: f32, y: f32, @@ -251,19 +250,22 @@ impl event::EventHandler for Game { } if let Some(selected) = self.selected { - let position = Point2 { x: x, y: y }; + let position = Point2 { x, y }; let mut swap = None; for (i, row) in self.grid.iter_mut().enumerate() { for (j, cell) in row.iter_mut().enumerate() { - if cell.contains(position) { - if (i + 1 == selected.0) && (j == selected.1) { - swap = Some((i, j)); - } + if cell.contains(position) + && (((i + 1 == selected.0) && (j == selected.1)) + || ((i - 1 == selected.0) && (j == selected.1)) + || ((i == selected.0) && (j + 1 == selected.1)) + || ((i == selected.0) && (j - 1 == selected.1))) + { + swap = Some((i, j)); } } } if let Some((i, j)) = swap { - let clone = self.grid[i][j].occupant.clone(); + let clone = self.grid[i][j].occupant; self.grid[i][j].occupant = self.grid[selected.0][selected.1].occupant; self.grid[selected.0][selected.1].occupant = clone; self.selected = None; @@ -272,12 +274,12 @@ impl event::EventHandler for Game { } } - fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult { + fn draw(&mut self, context: &mut Context) -> GameResult { graphics::clear(context, [0.1, 0.2, 0.3, 1.0].into()); for row in self.grid.iter() { for cell in row.iter() { - cell.draw(context, &mut self.spritebatch) + cell.draw(context, &mut self.spritebatch)?; } } @@ -290,9 +292,10 @@ impl event::EventHandler for Game { } } -pub fn main() -> ggez::GameResult { - let cb = ggez::ContextBuilder::new("super_simple", "ggez"); - let (context, event_loop) = &mut cb.add_resource_path("./resources").build()?; +pub fn main() -> GameResult { + let (ref mut context, ref mut event_loop) = ContextBuilder::new("gems", "jw&tb") + .add_resource_path("./resources") + .build()?; let game = &mut Game::new(context)?; event::run(context, event_loop, game) } -- cgit v1.2.3