Add system for limiting speed.

This commit is contained in:
Nolan Darilek 2022-08-04 11:49:17 -05:00
parent c02f9b21d6
commit 1fcec80e0e

View File

@ -97,7 +97,7 @@ fn movement_controls<S>(
if actions.pressed(NavigationAction::Move) { if actions.pressed(NavigationAction::Move) {
if let Some(pair) = actions.clamped_axis_pair(NavigationAction::Move) { if let Some(pair) = actions.clamped_axis_pair(NavigationAction::Move) {
cleanup = true; cleanup = true;
let mut direction = pair.xy(); let direction = pair.xy();
let strength = direction.length(); let strength = direction.length();
let forward_backward_movement_factor = if direction.x > 0. { let forward_backward_movement_factor = if direction.x > 0. {
config.forward_movement_factor config.forward_movement_factor
@ -271,6 +271,14 @@ fn add_speed(mut commands: Commands, query: Query<Entity, (Added<Speed>, Without
} }
} }
fn limit_speed(mut query: Query<(&mut Speed, &MaxSpeed)>) {
for (mut speed, max_speed) in &mut query {
if **speed > **max_speed {
**speed = **max_speed;
}
}
}
fn remove_speed(removed: RemovedComponents<Speed>, mut query: Query<&mut Velocity>) { fn remove_speed(removed: RemovedComponents<Speed>, mut query: Query<&mut Velocity>) {
for entity in removed.iter() { for entity in removed.iter() {
if let Ok(mut velocity) = query.get_mut(entity) { if let Ok(mut velocity) = query.get_mut(entity) {
@ -379,6 +387,7 @@ where
.add_system(tick_snap_timers) .add_system(tick_snap_timers)
.add_system(speak_direction.chain(error_handler)) .add_system(speak_direction.chain(error_handler))
.add_system(add_speed) .add_system(add_speed)
.add_system(limit_speed)
.add_system_to_stage(CoreStage::PostUpdate, remove_speed) .add_system_to_stage(CoreStage::PostUpdate, remove_speed)
.add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::<S, A>); .add_system_to_stage(CoreStage::PostUpdate, log_area_descriptions::<S, A>);
const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS"; const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS";