Migrate to actions for setting velocities.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nolan Darilek 2022-09-28 09:32:33 -05:00
parent 43b163fbd9
commit e4f60ff024

View File

@ -6,7 +6,7 @@ use std::{
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use bevy_tts::Tts;
use leafwing_input_manager::prelude::*;
use leafwing_input_manager::{axislike::DualAxisData, prelude::*};
use crate::{
commands::RunIfExistsExt,
@ -22,6 +22,8 @@ use crate::{
pub enum NavigationAction {
Move,
Rotate,
SetLinearVelocity,
SetAngularVelocity,
SnapLeft,
SnapRight,
SnapCardinal,
@ -39,16 +41,10 @@ impl Default for MaxSpeed {
}
}
#[derive(Component, Clone, Copy, Debug, Deref, DerefMut, Reflect)]
#[derive(Component, Clone, Copy, Default, Debug, Deref, DerefMut, Reflect)]
#[reflect(Component)]
pub struct RotationSpeed(pub Angle);
impl Default for RotationSpeed {
fn default() -> Self {
Self(Angle::Radians(0.))
}
}
#[derive(Component, Clone, Copy, Debug, Default, Deref, DerefMut, Reflect)]
#[reflect(Component)]
pub struct Speed(pub f32);
@ -65,32 +61,22 @@ impl Default for SnapTimer {
fn movement_controls<S>(
mut commands: Commands,
config: Res<NavigationConfig<S>>,
time: Res<Time>,
snap_timers: Res<HashMap<Entity, SnapTimer>>,
mut query: Query<(
Entity,
&ActionState<NavigationAction>,
&mut ActionState<NavigationAction>,
&mut Velocity,
&mut Speed,
&MaxSpeed,
Option<&RotationSpeed>,
&mut Transform,
Option<&Destination>,
&Transform,
)>,
exploration_focused: Query<Entity, With<ExplorationFocused>>,
) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{
for (
entity,
actions,
mut velocity,
mut speed,
max_speed,
rotation_speed,
mut transform,
destination,
) in &mut query
for (entity, mut actions, mut velocity, mut speed, max_speed, rotation_speed, transform) in
&mut query
{
let sprinting = actions.pressed(NavigationAction::Sprint);
let mut cleanup = false;
@ -126,25 +112,48 @@ fn movement_controls<S>(
.compute_matrix()
.transform_vector3(v.extend(0.))
.truncate();
velocity.linvel = v;
actions.press(NavigationAction::SetLinearVelocity);
actions
.action_data_mut(NavigationAction::SetLinearVelocity)
.axis_pair = Some(DualAxisData::from_xy(v));
}
}
if actions.released(NavigationAction::Move) {
if actions.axis_pair(NavigationAction::Move).is_some() {
actions.press(NavigationAction::SetLinearVelocity);
actions
.action_data_mut(NavigationAction::SetLinearVelocity)
.axis_pair = None;
}
}
if actions.pressed(NavigationAction::SetLinearVelocity) {
if let Some(pair) = actions.axis_pair(NavigationAction::SetLinearVelocity) {
velocity.linvel = pair.into();
} else {
velocity.linvel = Vec2::ZERO;
}
} else if destination.is_none() {
velocity.linvel = Vec2::ZERO;
}
if !snap_timers.contains_key(&entity) {
if let Some(rotation_speed) = rotation_speed {
if actions.pressed(NavigationAction::Rotate) {
cleanup = true;
let delta = rotation_speed.radians()
* time.delta_seconds()
* actions.clamped_value(NavigationAction::Rotate);
transform.rotate(Quat::from_rotation_z(delta));
let delta =
rotation_speed.radians() * actions.clamped_value(NavigationAction::Rotate);
actions.press(NavigationAction::SetAngularVelocity);
actions
.action_data_mut(NavigationAction::SetAngularVelocity)
.value = delta;
}
} else {
velocity.angvel = 0.;
}
} else {
velocity.angvel = 0.;
}
if actions.released(NavigationAction::Rotate) {
actions.press(NavigationAction::SetAngularVelocity);
actions
.action_data_mut(NavigationAction::SetAngularVelocity)
.value = 0.;
}
if actions.pressed(NavigationAction::SetAngularVelocity) {
velocity.angvel = actions.value(NavigationAction::SetAngularVelocity);
}
if cleanup {
commands.entity(entity).remove::<Destination>();
@ -164,13 +173,12 @@ fn snap(
Entity,
&ActionState<NavigationAction>,
&mut Transform,
&mut Velocity,
&CardinalDirection,
),
With<Player>,
>,
) -> Result<(), Box<dyn Error>> {
for (entity, actions, mut transform, mut velocity, direction) in query.iter_mut() {
for (entity, actions, mut transform, direction) in &mut query {
if snap_timers.contains_key(&entity) {
continue;
} else if actions.pressed(NavigationAction::SnapLeft) {
@ -181,7 +189,6 @@ fn snap(
CardinalDirection::South => 0.,
CardinalDirection::West => -PI / 2.,
});
velocity.angvel = 0.;
} else if actions.pressed(NavigationAction::SnapRight) {
snap_timers.insert(entity, SnapTimer::default());
transform.rotation = Quat::from_rotation_z(match direction {
@ -190,17 +197,14 @@ fn snap(
CardinalDirection::South => PI,
CardinalDirection::West => PI / 2.,
});
velocity.angvel = 0.;
} else if actions.pressed(NavigationAction::SnapReverse) {
snap_timers.insert(entity, SnapTimer::default());
transform.rotate(Quat::from_rotation_z(PI));
velocity.angvel = 0.;
} else if actions.pressed(NavigationAction::SnapCardinal) {
let yaw: Angle = direction.into();
let yaw = yaw.radians();
transform.rotation = Quat::from_rotation_z(yaw);
tts.speak(direction.to_string(), true)?;
velocity.angvel = 0.;
}
}
Ok(())