1
0
Fork 0

Use less redundant code style with 'if let' statements

This commit is contained in:
Timothy Warren 2021-11-19 19:52:15 -05:00
parent ecc2f21d40
commit 3effbdb586
9 changed files with 19 additions and 33 deletions

View File

@ -45,8 +45,7 @@ pub fn delete_the_dead(ecs: &mut World) {
let player = players.get(entity); let player = players.get(entity);
match player { match player {
None => { None => {
let victim_name = names.get(entity); if let Some(victim_name) = names.get(entity) {
if let Some(victim_name) = victim_name {
log.append(format!("{} is dead", &victim_name.name)); log.append(format!("{} is dead", &victim_name.name));
} }

View File

@ -1,12 +1,14 @@
#[derive(Default)]
pub struct GameLog { pub struct GameLog {
pub entries: Vec<String>, pub entries: Vec<String>,
} }
impl GameLog { impl GameLog {
pub fn new<S: ToString>(first_entry: S) -> Self { pub fn new<S: ToString>(first_entry: S) -> Self {
GameLog { let mut log = GameLog::default();
entries: vec![first_entry.to_string()], log.append(first_entry);
}
log
} }
pub fn append<S: ToString>(&mut self, s: S) { pub fn append<S: ToString>(&mut self, s: S) {

View File

@ -485,8 +485,7 @@ pub fn ranged_target(
// Highlight available target cells // Highlight available target cells
let mut available_cells = Vec::new(); let mut available_cells = Vec::new();
let visible = viewsheds.get(*player_entity); if let Some(visible) = viewsheds.get(*player_entity) {
if let Some(visible) = visible {
for idx in visible.visible_tiles.iter() { for idx in visible.visible_tiles.iter() {
let distance = rltk::DistanceAlg::Pythagoras.distance2d(*player_pos, *idx); let distance = rltk::DistanceAlg::Pythagoras.distance2d(*player_pos, *idx);
if distance <= range as f32 { if distance <= range as f32 {

View File

@ -214,8 +214,7 @@ impl<'a> System<'a> for ItemUseSystem {
used_item = false; used_item = false;
for target in targets.iter() { for target in targets.iter() {
let stats = combat_stats.get_mut(*target); if let Some(stats) = combat_stats.get_mut(*target) {
if let Some(stats) = stats {
stats.hp = i32::min(stats.max_hp, stats.hp + healer.heal_amount); stats.hp = i32::min(stats.max_hp, stats.hp + healer.heal_amount);
if entity == *player_entity { if entity == *player_entity {
gamelog.append(format!( gamelog.append(format!(

View File

@ -175,9 +175,8 @@ impl GameState for State {
gui::ItemMenuResult::Selected => { gui::ItemMenuResult::Selected => {
let item_entity = result.1.unwrap(); let item_entity = result.1.unwrap();
let is_ranged = self.ecs.read_storage::<Ranged>(); let is_ranged = self.ecs.read_storage::<Ranged>();
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 { newrunstate = RunState::ShowTargeting {
range: is_item_ranged.range, range: is_item_ranged.range,
item: item_entity, item: item_entity,
@ -201,7 +200,6 @@ impl GameState for State {
} }
RunState::ShowDropItem => { RunState::ShowDropItem => {
let result = gui::drop_item_menu(self, ctx); let result = gui::drop_item_menu(self, ctx);
match result.0 { match result.0 {
gui::ItemMenuResult::Cancel => newrunstate = RunState::AwaitingInput, gui::ItemMenuResult::Cancel => newrunstate = RunState::AwaitingInput,
gui::ItemMenuResult::NoResponse => {} gui::ItemMenuResult::NoResponse => {}
@ -322,21 +320,17 @@ impl State {
let mut should_delete = true; let mut should_delete = true;
// Don't delete the player // Don't delete the player
let p = player.get(entity); if let Some(_p) = player.get(entity) {
if let Some(_p) = p {
should_delete = false; should_delete = false;
} }
// Don't delete the player's equipment // Don't delete the player's equipment
let bp = backpack.get(entity); if let Some(bp) = backpack.get(entity) {
if let Some(bp) = bp {
if bp.owner == *player_entity { if bp.owner == *player_entity {
should_delete = false; should_delete = false;
} }
} }
if let Some(eq) = equipped.get(entity) {
let eq = equipped.get(entity);
if let Some(eq) = eq {
if eq.owner == *player_entity { if eq.owner == *player_entity {
should_delete = false; should_delete = false;
} }
@ -381,16 +375,14 @@ impl State {
*player_position = Point::new(player_x, player_y); *player_position = Point::new(player_x, player_y);
let mut position_components = self.ecs.write_storage::<Position>(); let mut position_components = self.ecs.write_storage::<Position>();
let player_entity = self.ecs.fetch::<Entity>(); let player_entity = self.ecs.fetch::<Entity>();
let player_pos_comp = position_components.get_mut(*player_entity); if let Some(player_pos_comp) = position_components.get_mut(*player_entity) {
if let Some(player_pos_comp) = player_pos_comp {
player_pos_comp.x = player_x; player_pos_comp.x = player_x;
player_pos_comp.y = player_y; player_pos_comp.y = player_y;
} }
// Mark the player's visibility as dirty // Mark the player's visibility as dirty
let mut viewshed_components = self.ecs.write_storage::<Viewshed>(); let mut viewshed_components = self.ecs.write_storage::<Viewshed>();
let vs = viewshed_components.get_mut(*player_entity); if let Some(vs) = viewshed_components.get_mut(*player_entity) {
if let Some(vs) = vs {
vs.dirty = true; vs.dirty = true;
} }
@ -439,8 +431,7 @@ impl State {
let mut position_components = self.ecs.write_storage::<Position>(); let mut position_components = self.ecs.write_storage::<Position>();
let mut player_entity_writer = self.ecs.write_resource::<Entity>(); let mut player_entity_writer = self.ecs.write_resource::<Entity>();
*player_entity_writer = player_entity; *player_entity_writer = player_entity;
let player_pos_comp = position_components.get_mut(player_entity); if let Some(player_pos_comp) = position_components.get_mut(player_entity) {
if let Some(player_pos_comp) = player_pos_comp {
player_pos_comp.x = player_x; player_pos_comp.x = player_x;
player_pos_comp.y = player_y; player_pos_comp.y = player_y;
} }

View File

@ -21,8 +21,7 @@ impl<'a> System<'a> for MapIndexingSystem {
let idx = map.xy_idx(position.x, position.y); let idx = map.xy_idx(position.x, position.y);
// If it's a blocking entity, note that in the map object // If it's a blocking entity, note that in the map object
let _p: Option<&BlocksTile> = blockers.get(entity); if let Some(_p) = blockers.get(entity) {
if let Some(_p) = _p {
map.blocked[idx] = true; map.blocked[idx] = true;
} }

View File

@ -45,8 +45,7 @@ impl<'a> System<'a> for MonsterAI {
{ {
let mut can_act = true; let mut can_act = true;
let is_confused = confused.get_mut(entity); if let Some(i_am_confused) = confused.get_mut(entity) {
if let Some(i_am_confused) = is_confused {
i_am_confused.turns -= 1; i_am_confused.turns -= 1;
if i_am_confused.turns < 1 { if i_am_confused.turns < 1 {
confused.remove(entity); confused.remove(entity);

View File

@ -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); let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
for potential_target in map.tile_content[destination_idx].iter() { for potential_target in map.tile_content[destination_idx].iter() {
let target = combat_stats.get(*potential_target); if let Some(_target) = combat_stats.get(*potential_target) {
if let Some(_target) = target {
wants_to_melee wants_to_melee
.insert( .insert(
entity, entity,

View File

@ -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); .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 // if this is the player, reveal what they can see
let _p: Option<&Player> = player.get(ent); if let Some(_p) = player.get(ent) {
if let Some(_p) = _p {
for t in map.visible_tiles.iter_mut() { for t in map.visible_tiles.iter_mut() {
*t = false *t = false
} }