From 3effbdb586bcf7e095b562e7f5f60220d1470c26 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 19 Nov 2021 19:52:15 -0500 Subject: [PATCH] Use less redundant code style with 'if let' statements --- src/damage_system.rs | 3 +-- src/game_log.rs | 8 +++++--- src/gui.rs | 3 +-- src/inventory_system.rs | 3 +-- src/main.rs | 23 +++++++---------------- src/map_indexing_system.rs | 3 +-- src/monster_ai_system.rs | 3 +-- src/player.rs | 3 +-- src/visibility_system.rs | 3 +-- 9 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/damage_system.rs b/src/damage_system.rs index 2a05a4a..d431b43 100644 --- a/src/damage_system.rs +++ b/src/damage_system.rs @@ -45,8 +45,7 @@ pub fn delete_the_dead(ecs: &mut World) { let player = players.get(entity); match player { None => { - let victim_name = names.get(entity); - if let Some(victim_name) = victim_name { + if let Some(victim_name) = names.get(entity) { log.append(format!("{} is dead", &victim_name.name)); } diff --git a/src/game_log.rs b/src/game_log.rs index 5967f8c..d598d2a 100644 --- a/src/game_log.rs +++ b/src/game_log.rs @@ -1,12 +1,14 @@ +#[derive(Default)] pub struct GameLog { pub entries: Vec, } impl GameLog { pub fn new(first_entry: S) -> Self { - GameLog { - entries: vec![first_entry.to_string()], - } + let mut log = GameLog::default(); + log.append(first_entry); + + log } pub fn append(&mut self, s: S) { diff --git a/src/gui.rs b/src/gui.rs index 80be162..51f0697 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -485,8 +485,7 @@ pub fn ranged_target( // Highlight available target cells let mut available_cells = Vec::new(); - let visible = viewsheds.get(*player_entity); - if let Some(visible) = visible { + if let Some(visible) = viewsheds.get(*player_entity) { for idx in visible.visible_tiles.iter() { let distance = rltk::DistanceAlg::Pythagoras.distance2d(*player_pos, *idx); if distance <= range as f32 { diff --git a/src/inventory_system.rs b/src/inventory_system.rs index 9729fa9..1460932 100644 --- a/src/inventory_system.rs +++ b/src/inventory_system.rs @@ -214,8 +214,7 @@ impl<'a> System<'a> for ItemUseSystem { used_item = false; for target in targets.iter() { - let stats = combat_stats.get_mut(*target); - if let Some(stats) = stats { + if let Some(stats) = combat_stats.get_mut(*target) { stats.hp = i32::min(stats.max_hp, stats.hp + healer.heal_amount); if entity == *player_entity { gamelog.append(format!( diff --git a/src/main.rs b/src/main.rs index 4f65772..d6fc39a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -175,9 +175,8 @@ impl GameState for State { gui::ItemMenuResult::Selected => { let item_entity = result.1.unwrap(); let is_ranged = self.ecs.read_storage::(); - let is_item_ranged = is_ranged.get(item_entity); - if let Some(is_item_ranged) = is_item_ranged { + if let Some(is_item_ranged) = is_ranged.get(item_entity) { newrunstate = RunState::ShowTargeting { range: is_item_ranged.range, item: item_entity, @@ -201,7 +200,6 @@ impl GameState for State { } RunState::ShowDropItem => { let result = gui::drop_item_menu(self, ctx); - match result.0 { gui::ItemMenuResult::Cancel => newrunstate = RunState::AwaitingInput, gui::ItemMenuResult::NoResponse => {} @@ -322,21 +320,17 @@ impl State { let mut should_delete = true; // Don't delete the player - let p = player.get(entity); - if let Some(_p) = p { + if let Some(_p) = player.get(entity) { should_delete = false; } // Don't delete the player's equipment - let bp = backpack.get(entity); - if let Some(bp) = bp { + if let Some(bp) = backpack.get(entity) { if bp.owner == *player_entity { should_delete = false; } } - - let eq = equipped.get(entity); - if let Some(eq) = eq { + if let Some(eq) = equipped.get(entity) { if eq.owner == *player_entity { should_delete = false; } @@ -381,16 +375,14 @@ impl State { *player_position = Point::new(player_x, player_y); let mut position_components = self.ecs.write_storage::(); let player_entity = self.ecs.fetch::(); - let player_pos_comp = position_components.get_mut(*player_entity); - if let Some(player_pos_comp) = player_pos_comp { + if let Some(player_pos_comp) = position_components.get_mut(*player_entity) { player_pos_comp.x = player_x; player_pos_comp.y = player_y; } // Mark the player's visibility as dirty let mut viewshed_components = self.ecs.write_storage::(); - let vs = viewshed_components.get_mut(*player_entity); - if let Some(vs) = vs { + if let Some(vs) = viewshed_components.get_mut(*player_entity) { vs.dirty = true; } @@ -439,8 +431,7 @@ impl State { let mut position_components = self.ecs.write_storage::(); let mut player_entity_writer = self.ecs.write_resource::(); *player_entity_writer = player_entity; - let player_pos_comp = position_components.get_mut(player_entity); - if let Some(player_pos_comp) = player_pos_comp { + if let Some(player_pos_comp) = position_components.get_mut(player_entity) { player_pos_comp.x = player_x; player_pos_comp.y = player_y; } diff --git a/src/map_indexing_system.rs b/src/map_indexing_system.rs index 74c73df..17ad747 100644 --- a/src/map_indexing_system.rs +++ b/src/map_indexing_system.rs @@ -21,8 +21,7 @@ impl<'a> System<'a> for MapIndexingSystem { let idx = map.xy_idx(position.x, position.y); // If it's a blocking entity, note that in the map object - let _p: Option<&BlocksTile> = blockers.get(entity); - if let Some(_p) = _p { + if let Some(_p) = blockers.get(entity) { map.blocked[idx] = true; } diff --git a/src/monster_ai_system.rs b/src/monster_ai_system.rs index 4a66562..8d34748 100644 --- a/src/monster_ai_system.rs +++ b/src/monster_ai_system.rs @@ -45,8 +45,7 @@ impl<'a> System<'a> for MonsterAI { { let mut can_act = true; - let is_confused = confused.get_mut(entity); - if let Some(i_am_confused) = is_confused { + if let Some(i_am_confused) = confused.get_mut(entity) { i_am_confused.turns -= 1; if i_am_confused.turns < 1 { confused.remove(entity); diff --git a/src/player.rs b/src/player.rs index 1fb9ead..fb41edb 100644 --- a/src/player.rs +++ b/src/player.rs @@ -29,8 +29,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y); for potential_target in map.tile_content[destination_idx].iter() { - let target = combat_stats.get(*potential_target); - if let Some(_target) = target { + if let Some(_target) = combat_stats.get(*potential_target) { wants_to_melee .insert( entity, diff --git a/src/visibility_system.rs b/src/visibility_system.rs index 4fd8c4a..5e9c135 100644 --- a/src/visibility_system.rs +++ b/src/visibility_system.rs @@ -27,8 +27,7 @@ impl<'a> System<'a> for VisibilitySystem { .retain(|p| p.x >= 0 && p.x < map.width && p.y >= 0 && p.y < map.height); // if this is the player, reveal what they can see - let _p: Option<&Player> = player.get(ent); - if let Some(_p) = _p { + if let Some(_p) = player.get(ent) { for t in map.visible_tiles.iter_mut() { *t = false }