diff --git a/src/navigation.rs b/src/navigation.rs index 6c6adb8..0ff6d99 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -174,26 +174,31 @@ fn movement_controls( } } -fn speak_direction( - mut tts: ResMut, - mut cache: Local>, - player: Query<(Entity, &Player, &Transform), Changed>, -) -> Result<(), Box> { - if let Ok((entity, _, transform)) = player.single() { +fn update_direction( + mut commands: Commands, + query: Query< + (Entity, &Transform, Option<&CardinalDirection>), + (With, Changed), + >, +) { + for (entity, transform, direction) in query.iter() { let forward = transform.local_x(); let yaw = Angle::Radians(forward.y.atan2(forward.x)); - if let Some(old_direction) = cache.get(&entity) { - let old_direction = *old_direction; - let direction: CardinalDirection = yaw.into(); - if old_direction != direction { - let direction: String = direction.into(); - tts.speak(direction, true)?; - } - cache.insert(entity, direction); - } else { - cache.insert(entity, yaw.into()); + let new_direction: CardinalDirection = yaw.into(); + if direction != Some(&new_direction) { + commands.entity(entity).insert(new_direction); } } +} + +fn speak_direction( + mut tts: ResMut, + player: Query<&CardinalDirection, (With, Changed)>, +) -> Result<(), Box> { + if let Ok(direction) = player.single() { + let direction: String = (*direction).into(); + tts.speak(direction, true)?; + } Ok(()) } @@ -258,6 +263,7 @@ where app.register_type::() .register_type::() .register_type::() + .add_system(update_direction.system()) .add_system(speak_direction.system().chain(error_handler.system())); if config.movement_control_states.is_empty() { app.add_system(movement_controls::.system());