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