Switch to action_data_mut_or_default.

This commit is contained in:
Nolan Darilek 2024-03-24 12:15:19 -05:00
parent 764ab980bd
commit aeb8039463
2 changed files with 38 additions and 44 deletions

View File

@ -202,26 +202,25 @@ fn controls(
if character_controller.is_some() { if character_controller.is_some() {
let translation = velocity * time.delta_seconds(); let translation = velocity * time.delta_seconds();
actions.press(&NavigationAction::Translate); actions.press(&NavigationAction::Translate);
if let Some(data) = actions.action_data_mut(&NavigationAction::Translate) { actions
data.axis_pair = Some(DualAxisData::from_xy(translation)); .action_data_mut_or_default(&NavigationAction::Translate)
} .axis_pair = Some(DualAxisData::from_xy(translation));
} else { } else {
// println!("{entity:?}: SetLinearVelocity: {velocity:?}"); // println!("{entity:?}: SetLinearVelocity: {velocity:?}");
actions.press(&NavigationAction::SetLinearVelocity); actions.press(&NavigationAction::SetLinearVelocity);
if let Some(data) =
actions.action_data_mut(&NavigationAction::SetLinearVelocity) actions
{ .action_data_mut_or_default(&NavigationAction::SetLinearVelocity)
data.axis_pair = Some(DualAxisData::from_xy(velocity)); .axis_pair = Some(DualAxisData::from_xy(velocity));
}
} }
} }
} else if actions.just_released(&NavigationAction::Move) { } else if actions.just_released(&NavigationAction::Move) {
trace!("{entity:?}: Stopped moving"); trace!("{entity:?}: Stopped moving");
actions.release(&NavigationAction::SetLinearVelocity); actions.release(&NavigationAction::SetLinearVelocity);
actions.release(&NavigationAction::Translate); actions.release(&NavigationAction::Translate);
if let Some(data) = actions.action_data_mut(&NavigationAction::Move) { actions
data.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); .action_data_mut_or_default(&NavigationAction::Move)
} .axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO));
} }
if actions.pressed(&NavigationAction::SetLinearVelocity) { if actions.pressed(&NavigationAction::SetLinearVelocity) {
if let Some(pair) = actions.axis_pair(&NavigationAction::SetLinearVelocity) { if let Some(pair) = actions.axis_pair(&NavigationAction::SetLinearVelocity) {
@ -234,9 +233,9 @@ fn controls(
} else if actions.just_released(&NavigationAction::SetLinearVelocity) { } else if actions.just_released(&NavigationAction::SetLinearVelocity) {
// println!("{entity:?}: Released velocity"); // println!("{entity:?}: Released velocity");
velocity.linvel = Vec2::ZERO; velocity.linvel = Vec2::ZERO;
if let Some(data) = actions.action_data_mut(&NavigationAction::SetLinearVelocity) { actions
data.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); .action_data_mut_or_default(&NavigationAction::SetLinearVelocity)
} .axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO));
} }
if actions.pressed(&NavigationAction::Translate) { if actions.pressed(&NavigationAction::Translate) {
if let Some(pair) = actions.axis_pair(&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 { if let Some(mut character_controller) = character_controller {
character_controller.translation = None; character_controller.translation = None;
} }
if let Some(data) = actions.action_data_mut(&NavigationAction::Translate) { actions
data.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); .action_data_mut_or_default(&NavigationAction::Translate)
} .axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO));
} }
if !snap_timers.contains_key(&entity) { if !snap_timers.contains_key(&entity) {
if let Some(rotation_speed) = rotation_speed { if let Some(rotation_speed) = rotation_speed {
@ -259,26 +258,24 @@ fn controls(
let delta = -rotation_speed.radians() let delta = -rotation_speed.radians()
* actions.clamped_value(&NavigationAction::Rotate); * actions.clamped_value(&NavigationAction::Rotate);
actions.press(&NavigationAction::SetAngularVelocity); actions.press(&NavigationAction::SetAngularVelocity);
if let Some(data) = actions
actions.action_data_mut(&NavigationAction::SetAngularVelocity) .action_data_mut_or_default(&NavigationAction::SetAngularVelocity)
{ .value = delta;
data.value = delta;
}
} }
} }
} }
if actions.just_released(&NavigationAction::Rotate) { if actions.just_released(&NavigationAction::Rotate) {
actions.release(&NavigationAction::SetAngularVelocity); actions.release(&NavigationAction::SetAngularVelocity);
if let Some(data) = actions.action_data_mut(&NavigationAction::Rotate) { actions
data.value = 0.; .action_data_mut_or_default(&NavigationAction::Rotate)
} .value = 0.;
} }
if actions.pressed(&NavigationAction::SetAngularVelocity) { if actions.pressed(&NavigationAction::SetAngularVelocity) {
velocity.angvel = actions.value(&NavigationAction::SetAngularVelocity); velocity.angvel = actions.value(&NavigationAction::SetAngularVelocity);
} else if actions.just_released(&NavigationAction::SetAngularVelocity) { } else if actions.just_released(&NavigationAction::SetAngularVelocity) {
if let Some(data) = actions.action_data_mut(&NavigationAction::SetAngularVelocity) { actions
data.value = 0.; .action_data_mut_or_default(&NavigationAction::SetAngularVelocity)
} .value = 0.;
velocity.angvel = 0.; velocity.angvel = 0.;
} }
if cleanup { if cleanup {

View File

@ -322,9 +322,9 @@ fn negotiate_path(
} }
trace!("{entity:?}: path: direction: {direction:?}"); trace!("{entity:?}: path: direction: {direction:?}");
actions.press(&NavigationAction::Move); actions.press(&NavigationAction::Move);
if let Some(data) = actions.action_data_mut(&NavigationAction::Move) { actions
data.axis_pair = Some(DualAxisData::from_xy(Vec2::new(-direction.y, direction.x))); .action_data_mut_or_default(&NavigationAction::Move)
} .axis_pair = Some(DualAxisData::from_xy(Vec2::new(-direction.y, direction.x)));
if rotation_speed.is_some() { if rotation_speed.is_some() {
let angle = direction.y.atan2(direction.x); let angle = direction.y.atan2(direction.x);
transform.rotation = Quat::from_rotation_z(angle); transform.rotation = Quat::from_rotation_z(angle);
@ -360,21 +360,18 @@ fn actions(
if *current_dest != dest { if *current_dest != dest {
trace!("{entity:?}: New destination, zeroing velocity"); trace!("{entity:?}: New destination, zeroing velocity");
navigation_action.press(&NavigationAction::SetLinearVelocity); navigation_action.press(&NavigationAction::SetLinearVelocity);
if let Some(data) = navigation_action
navigation_action.action_data_mut(&NavigationAction::SetLinearVelocity) .action_data_mut_or_default(&NavigationAction::SetLinearVelocity)
{ .axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO));
data.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO));
}
*current_dest = dest; *current_dest = dest;
} }
} else { } else {
trace!("{entity:?}: Adding destination, zeroing velocity"); trace!("{entity:?}: Adding destination, zeroing velocity");
navigation_action.press(&NavigationAction::SetLinearVelocity); navigation_action.press(&NavigationAction::SetLinearVelocity);
if let Some(data) =
navigation_action.action_data_mut(&NavigationAction::SetLinearVelocity) navigation_action
{ .action_data_mut_or_default(&NavigationAction::SetLinearVelocity)
data.axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO)); .axis_pair = Some(DualAxisData::from_xy(Vec2::ZERO));
}
commands.entity(entity).insert(dest); commands.entity(entity).insert(dest);
} }
} else if destination.is_some() { } else if destination.is_some() {
@ -384,9 +381,9 @@ fn actions(
.remove::<NoPath>(); .remove::<NoPath>();
} }
actions.release(&NegotiatePathAction); actions.release(&NegotiatePathAction);
if let Some(data) = actions.action_data_mut(&NegotiatePathAction) { actions
data.axis_pair = None; .action_data_mut_or_default(&NegotiatePathAction)
} .axis_pair = None;
} }
} }
} }