More accurately sync speed and velocity.

This commit is contained in:
Nolan Darilek 2022-05-12 10:07:38 -05:00
parent ef55419bb7
commit 7e6fd923a3

View File

@ -297,6 +297,15 @@ fn speak_direction(
Ok(()) Ok(())
} }
fn add_speed(mut commands: Commands, query: Query<Entity, (Added<Speed>, Without<Velocity>)>) {
for entity in query.iter() {
commands.entity(entity).insert(Velocity {
linvel: Vec2::ZERO,
..default()
});
}
}
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) {
@ -377,6 +386,7 @@ where
.register_type::<Sprinting>() .register_type::<Sprinting>()
.add_system(update_direction) .add_system(update_direction)
.add_system(speak_direction.chain(error_handler)) .add_system(speak_direction.chain(error_handler))
.add_system(add_speed)
.add_system_to_stage(CoreStage::PostUpdate, remove_speed); .add_system_to_stage(CoreStage::PostUpdate, remove_speed);
const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS"; const MOVEMENT_CONTROLS: &str = "MOVEMENT_CONTROLS";
if config.movement_control_states.is_empty() { if config.movement_control_states.is_empty() {