Fix clippy warnings

This commit is contained in:
Alexander Bass 2023-06-07 16:18:36 -04:00
parent 864a9ca3d5
commit 31c6c4858f
12 changed files with 53 additions and 63 deletions

View file

@ -53,13 +53,13 @@ pub struct UIState {
}
impl UIState {
pub fn new(width: usize, height: usize, tile_size: usize, top_offset: usize) -> Self {
return Self {
Self {
width,
height,
tile_size,
top_offset,
..Default::default()
};
}
}
pub fn update_dimensions(&mut self, width: usize, height: usize) {
self.width = width;
@ -91,12 +91,12 @@ impl UIState {
let (x, y) = self.pixel_screen_scale(x, y);
let x = x + self.letterbox.0;
let y = y + self.letterbox.1;
return (x, y);
(x, y)
}
pub fn pixel_screen_scale(&self, x: usize, y: usize) -> (f32, f32) {
let x = x as f32;
let y = y as f32;
return (x * self.scale, y * self.scale);
(x * self.scale, y * self.scale)
}
}
@ -113,10 +113,10 @@ pub struct GameUI {
impl GameUI {
pub fn new(settings: UIState) -> Self {
let set = settings;
return Self {
Self {
state: set,
..Default::default()
};
}
}
pub fn is_valid_position(&self, x: usize, y: usize) -> bool {
if x < self.state.width && y < self.state.height {
@ -145,6 +145,6 @@ impl GameUI {
if !self.is_valid_position(x, y) {
return None;
}
return Some((x, y));
Some((x, y))
}
}

View file

@ -117,7 +117,7 @@ impl Highlighter {
}
}
self.move_highlight(&interface, event_handler);
self.move_highlight(interface, event_handler);
}
fn move_highlight(&mut self, interface: &UIState, event_handler: &mut Events<GUIEvent>) {

View file

@ -51,18 +51,18 @@ impl SettingsMenu {
root_ui().window(hash!(), vec2(0., 0.), vec2(screen_width, screen_height), |ui| {
draw_rectangle(0f32, 0f32, screen_width, screen_height, background_color);
ui.push_skin(&exit_button_skin);
ui.push_skin(exit_button_skin);
if widgets::Button::new("").size(vec2(50.0, 50.0)).position(vec2(0f32, 0f32)).ui(ui) {
event_handler.add(GUIEvent::CloseSettings)
}
ui.pop_skin();
ui.push_skin(&skin);
ui.push_skin(skin);
let half_screen_width = screen_width * 0.5;
render_counter(
&mut self.width,
ui,
&textures,
textures,
vec2(half_screen_width, 100f32),
"Minefield Width",
MIN_MINEFIELD_WIDTH,
@ -71,7 +71,7 @@ impl SettingsMenu {
render_counter(
&mut self.height,
ui,
&textures,
textures,
vec2(half_screen_width, 200f32),
"Minefield Height",
MIN_MINEFIELD_HEIGHT,
@ -80,7 +80,7 @@ impl SettingsMenu {
render_counter(
&mut self.mines,
ui,
&textures,
textures,
vec2(half_screen_width, 300f32),
"Mines",
1,
@ -113,14 +113,12 @@ impl SettingsMenu {
{
event_handler.add(GUIEvent::SwitchLanguage(Language::Japanese));
}
} else {
if widgets::Button::new("Japanese")
.size(vec2(BUTTON_SIZE, BUTTON_SIZE))
.position(vec2(language_button_x, BUTTON_MENU_Y))
.ui(ui)
{
event_handler.add(GUIEvent::SwitchLanguage(Language::English));
}
} else if widgets::Button::new("Japanese")
.size(vec2(BUTTON_SIZE, BUTTON_SIZE))
.position(vec2(language_button_x, BUTTON_MENU_Y))
.ui(ui)
{
event_handler.add(GUIEvent::SwitchLanguage(Language::English));
}
if let ModifyMode::Question = self.board_modify_mode {
if widgets::Button::new("ON")
@ -131,15 +129,13 @@ impl SettingsMenu {
self.board_modify_mode = ModifyMode::Flag;
event_handler.add(GUIEvent::SetQuestionMode(ModifyMode::Flag));
}
} else {
if widgets::Button::new("OFF")
.size(vec2(BUTTON_SIZE, BUTTON_SIZE))
.position(vec2(question_button_x, BUTTON_MENU_Y))
.ui(ui)
{
self.board_modify_mode = ModifyMode::Question;
event_handler.add(GUIEvent::SetQuestionMode(ModifyMode::Question));
}
} else if widgets::Button::new("OFF")
.size(vec2(BUTTON_SIZE, BUTTON_SIZE))
.position(vec2(question_button_x, BUTTON_MENU_Y))
.ui(ui)
{
self.board_modify_mode = ModifyMode::Question;
event_handler.add(GUIEvent::SetQuestionMode(ModifyMode::Question));
}
});
}
@ -172,11 +168,9 @@ fn render_counter(count: &mut usize, ui: &mut Ui, textures: &TextureStore, posit
if widgets::Button::new("-")
.size(vec2(COUNTER_BUTTON_HEIGHT, COUNTER_BUTTON_HEIGHT))
.position(position - vec2(COUNTER_BUTTON_HEIGHT + COUNTER_BUTTON_MARGIN, -BUTTON_OFFSET_HEIGHT))
.ui(ui)
.ui(ui) && *count > min
{
if *count > min {
*count -= 1;
}
*count -= 1;
}
if widgets::Button::new("++")
.size(vec2(COUNTER_BUTTON_HEIGHT, COUNTER_BUTTON_HEIGHT))

View file

@ -7,9 +7,9 @@ pub const HEIGHT: usize = 23 * 2;
use super::{texture_store::TextureStore, UIState};
pub fn draw_seven_segment(ui_state: &UIState, ui: &mut Ui, textures: &TextureStore, val: &Vec<usize>, x: usize, y: usize) {
pub fn draw_seven_segment(ui_state: &UIState, ui: &mut Ui, textures: &TextureStore, val: &[usize], x: usize, y: usize) {
for (n, digit) in val.iter().enumerate() {
let (scaled_width, scaled_height) = ui_state.pixel_screen_scale(WIDTH as usize, HEIGHT);
let (scaled_width, scaled_height) = ui_state.pixel_screen_scale(WIDTH, HEIGHT);
let (pos_x, pos_y) = ui_state.pixel_screen_offset(n * WIDTH + x, y);
widgets::Texture::new(textures.numbers[*digit])
@ -19,7 +19,7 @@ pub fn draw_seven_segment(ui_state: &UIState, ui: &mut Ui, textures: &TextureSto
}
}
pub fn draw_seven_segment_unscaled(ui: &mut Ui, textures: &TextureStore, val: &Vec<usize>, x: usize, y: usize) {
pub fn draw_seven_segment_unscaled(ui: &mut Ui, textures: &TextureStore, val: &[usize], x: usize, y: usize) {
for (n, digit) in val.iter().enumerate() {
let (pos_x, pos_y) = ((n * WIDTH + x) as f32, y as f32);

View file

@ -33,9 +33,9 @@ impl TextureStore {
}
}
pub fn get_tiles(&self) -> &Vec<Texture2D> {
return match self.lang {
match self.lang {
Language::English => &self.english_tiles,
Language::Japanese => &self.japanese_tiles,
};
}
}
}

View file

@ -54,7 +54,7 @@ impl GUITop {
const HEIGHT: usize = 35;
let pos_y = (ui_state.top_offset - HEIGHT) / 2;
let pos_x = (13 * 2 * 2 - WIDTH) / 2;
let (scaled_width, scaled_height) = ui_state.pixel_screen_scale(WIDTH as usize, HEIGHT);
let (scaled_width, scaled_height) = ui_state.pixel_screen_scale(WIDTH, HEIGHT);
let (pos_x, pos_y) = ui_state.pixel_screen_offset(pos_x, pos_y);
if widgets::Button::new(textures.cog)
.size(vec2(scaled_width, scaled_height))
@ -69,9 +69,9 @@ impl GUITop {
}
}
self.timer.render(&ui_state, game_logic.get_time(), ui, &textures);
self.smile.render(&ui_state, ui, event_handler, &textures);
self.flag_counter.render(&ui_state, game_logic.board.remaining_flags(), ui, &textures);
self.timer.render(ui_state, game_logic.get_time(), ui, textures);
self.smile.render(ui_state, ui, event_handler, textures);
self.flag_counter.render(ui_state, game_logic.board.remaining_flags(), ui, textures);
});
}
}

View file

@ -82,7 +82,7 @@ impl Minesweeper {
self.board.modify(x, y, &mut self.events)
}
pub fn get_time(&self) -> Option<f64> {
return self.timer.elapsed();
self.timer.elapsed()
}
pub fn highlight(&mut self, x: usize, y: usize) {
if self.state == GameState::Playing || self.state == GameState::Empty {

View file

@ -109,11 +109,11 @@ impl GameBoard {
self.flags -= 1;
match self.modify_mode {
ModifyMode::Flag => {
event_handler.add(GameEvent::FlagTile(x, y, tile.clone()));
event_handler.add(GameEvent::FlagTile(x, y, tile));
None
}
ModifyMode::Question => {
event_handler.add(GameEvent::QuestionTile(x, y, tile.clone()));
event_handler.add(GameEvent::QuestionTile(x, y, tile));
Some(TileModifier::Unsure)
}
@ -123,7 +123,7 @@ impl GameBoard {
}
} else {
self.flags += 1;
event_handler.add(GameEvent::FlagTile(x, y, tile.clone()));
event_handler.add(GameEvent::FlagTile(x, y, tile));
Some(TileModifier::Flagged)
};
if let Some(tile) = self.get_tile_mut(x, y) {
@ -136,8 +136,8 @@ impl GameBoard {
if let BoardState::Ungenerated = self.state {
self.generate(x, y);
}
let &tile = &self.tiles[x][y];
if let Some(_) = tile.modifier {
let tile = self.tiles[x][y];
if tile.modifier.is_some() {
return None;
}
if tile.swept {
@ -145,10 +145,10 @@ impl GameBoard {
}
self.tiles[x][y].swept = true;
self.revealed_tiles += 1;
event_handler.add(GameEvent::RevealTile(x, y, self.tiles[x][y].clone()));
event_handler.add(GameEvent::RevealTile(x, y, self.tiles[x][y]));
if tile.state == TileState::Mine {
event_handler.add(GameEvent::Lose(x, y, tile.clone()));
event_handler.add(GameEvent::Lose(x, y, tile));
event_handler.add(GameEvent::GameEnd(self.clone()));
return Some(GameState::GameOver);
@ -157,7 +157,7 @@ impl GameBoard {
let mut scan_list = VecDeque::from([(x, y)]);
let mut revealed: usize = 0;
while scan_list.len() > 0 {
while !scan_list.is_empty() {
for &scan_location in ADJACENT_WITHOUT_CENTER.iter() {
if let Some((x, y)) = scan_list.front() {
if let Some(old_tile) = self.get_tile(*x, *y) {
@ -179,7 +179,7 @@ impl GameBoard {
scan_list.push_back((x, y));
tile.swept = true;
revealed += 1;
event_handler.add(GameEvent::RevealTile(x, y, tile.clone()));
event_handler.add(GameEvent::RevealTile(x, y, *tile));
}
}
}
@ -226,7 +226,7 @@ impl GameBoard {
let y = macroquad::rand::gen_range(0, height);
let mut tile = &mut self.tiles[x][y];
if tile.state == TileState::Mine || tile.safe == true {
if tile.state == TileState::Mine || tile.safe {
continue;
}

View file

@ -27,7 +27,7 @@ impl Tile {
}
}
pub fn highlight(&mut self) {
if self.swept == false {
if !self.swept {
self.highlighted = true;
}
}

View file

@ -26,11 +26,7 @@ impl Timer {
if let TimerState::Frozen = self.state {
return Some(self.old);
}
if let Some(time) = self.start_time {
Some(get_time() - time)
} else {
None
}
self.start_time.map(|time| get_time() - time)
}
pub fn stop(&mut self) {
self.old = self.elapsed().unwrap_or(0f64);

View file

@ -12,8 +12,8 @@ mod sprite_loader;
mod util;
fn main() {
let width = (30 * 32) as i32;
let height = (16 * 32) as i32 + 100;
let width = 30 * 32;
let height = 16 * 32 + 100;
Window::from_config(
Conf {
sample_count: 2,

View file

@ -25,7 +25,7 @@ impl<E> Events<E> {
}
pub fn next(&mut self) -> Option<E> {
if self.events.len() > 0 {
if !self.events.is_empty() {
self.events.pop()
} else {
None