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);
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));
}

View File

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

View File

@ -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 {

View File

@ -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!(

View File

@ -175,9 +175,8 @@ impl GameState for State {
gui::ItemMenuResult::Selected => {
let item_entity = result.1.unwrap();
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 {
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::<Position>();
let player_entity = self.ecs.fetch::<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;
}
// Mark the player's visibility as dirty
let mut viewshed_components = self.ecs.write_storage::<Viewshed>();
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::<Position>();
let mut player_entity_writer = self.ecs.write_resource::<Entity>();
*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;
}

View File

@ -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;
}

View File

@ -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);

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);
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,

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);
// 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
}