From aeb80394633b5c9de27fb58765d7f5a41ff58a96 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Sun, 24 Mar 2024 12:15:19 -0500 Subject: [PATCH] Switch to `action_data_mut_or_default`. --- src/navigation.rs | 53 ++++++++++++++++++++++------------------------ src/pathfinding.rs | 29 ++++++++++++------------- 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/navigation.rs b/src/navigation.rs index 4bbb4fc..5667be1 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -202,26 +202,25 @@ fn controls( if character_controller.is_some() { let translation = velocity * time.delta_seconds(); actions.press(&NavigationAction::Translate); - if let Some(data) = actions.action_data_mut(&NavigationAction::Translate) { - data.axis_pair = Some(DualAxisData::from_xy(translation)); - } + actions + .action_data_mut_or_default(&NavigationAction::Translate) + .axis_pair = Some(DualAxisData::from_xy(translation)); } else { // println!("{entity:?}: SetLinearVelocity: {velocity:?}"); actions.press(&NavigationAction::SetLinearVelocity); - if let Some(data) = - actions.action_data_mut(&NavigationAction::SetLinearVelocity) - { - data.axis_pair = Some(DualAxisData::from_xy(velocity)); - } + + actions + .action_data_mut_or_default(&NavigationAction::SetLinearVelocity) + .axis_pair = Some(DualAxisData::from_xy(velocity)); } } } else if actions.just_released(&NavigationAction::Move) { trace!("{entity:?}: Stopped moving"); actions.release(&NavigationAction::SetLinearVelocity); actions.release(&NavigationAction::Translate); - if let Some(data) = actions.action_data_mut(&NavigationAction::Move) { - data.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); - } + actions + .action_data_mut_or_default(&NavigationAction::Move) + .axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); } if actions.pressed(&NavigationAction::SetLinearVelocity) { if let Some(pair) = actions.axis_pair(&NavigationAction::SetLinearVelocity) { @@ -234,9 +233,9 @@ fn controls( } else if actions.just_released(&NavigationAction::SetLinearVelocity) { // println!("{entity:?}: Released velocity"); velocity.linvel = Vec2::ZERO; - if let Some(data) = actions.action_data_mut(&NavigationAction::SetLinearVelocity) { - data.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); - } + actions + .action_data_mut_or_default(&NavigationAction::SetLinearVelocity) + .axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); } if actions.pressed(&NavigationAction::Translate) { if let Some(pair) = actions.axis_pair(&NavigationAction::Translate) { @@ -248,9 +247,9 @@ fn controls( if let Some(mut character_controller) = character_controller { character_controller.translation = None; } - if let Some(data) = actions.action_data_mut(&NavigationAction::Translate) { - data.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); - } + actions + .action_data_mut_or_default(&NavigationAction::Translate) + .axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); } if !snap_timers.contains_key(&entity) { if let Some(rotation_speed) = rotation_speed { @@ -259,26 +258,24 @@ fn controls( let delta = -rotation_speed.radians() * actions.clamped_value(&NavigationAction::Rotate); actions.press(&NavigationAction::SetAngularVelocity); - if let Some(data) = - actions.action_data_mut(&NavigationAction::SetAngularVelocity) - { - data.value = delta; - } + actions + .action_data_mut_or_default(&NavigationAction::SetAngularVelocity) + .value = delta; } } } if actions.just_released(&NavigationAction::Rotate) { actions.release(&NavigationAction::SetAngularVelocity); - if let Some(data) = actions.action_data_mut(&NavigationAction::Rotate) { - data.value = 0.; - } + actions + .action_data_mut_or_default(&NavigationAction::Rotate) + .value = 0.; } if actions.pressed(&NavigationAction::SetAngularVelocity) { velocity.angvel = actions.value(&NavigationAction::SetAngularVelocity); } else if actions.just_released(&NavigationAction::SetAngularVelocity) { - if let Some(data) = actions.action_data_mut(&NavigationAction::SetAngularVelocity) { - data.value = 0.; - } + actions + .action_data_mut_or_default(&NavigationAction::SetAngularVelocity) + .value = 0.; velocity.angvel = 0.; } if cleanup { diff --git a/src/pathfinding.rs b/src/pathfinding.rs index 8ff37b4..babea0f 100644 --- a/src/pathfinding.rs +++ b/src/pathfinding.rs @@ -322,9 +322,9 @@ fn negotiate_path( } trace!("{entity:?}: path: direction: {direction:?}"); actions.press(&NavigationAction::Move); - if let Some(data) = actions.action_data_mut(&NavigationAction::Move) { - data.axis_pair = Some(DualAxisData::from_xy(Vec2::new(-direction.y, direction.x))); - } + actions + .action_data_mut_or_default(&NavigationAction::Move) + .axis_pair = Some(DualAxisData::from_xy(Vec2::new(-direction.y, direction.x))); if rotation_speed.is_some() { let angle = direction.y.atan2(direction.x); transform.rotation = Quat::from_rotation_z(angle); @@ -360,21 +360,18 @@ fn actions( if *current_dest != dest { trace!("{entity:?}: New destination, zeroing velocity"); navigation_action.press(&NavigationAction::SetLinearVelocity); - if let Some(data) = - navigation_action.action_data_mut(&NavigationAction::SetLinearVelocity) - { - data.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); - } + navigation_action + .action_data_mut_or_default(&NavigationAction::SetLinearVelocity) + .axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); *current_dest = dest; } } else { trace!("{entity:?}: Adding destination, zeroing velocity"); navigation_action.press(&NavigationAction::SetLinearVelocity); - if let Some(data) = - navigation_action.action_data_mut(&NavigationAction::SetLinearVelocity) - { - data.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); - } + + navigation_action + .action_data_mut_or_default(&NavigationAction::SetLinearVelocity) + .axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); commands.entity(entity).insert(dest); } } else if destination.is_some() { @@ -384,9 +381,9 @@ fn actions( .remove::(); } actions.release(&NegotiatePathAction); - if let Some(data) = actions.action_data_mut(&NegotiatePathAction) { - data.axis_pair = None; - } + actions + .action_data_mut_or_default(&NegotiatePathAction) + .axis_pair = None; } } }