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() {
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 {

View File

@ -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::<NoPath>();
}
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;
}
}
}