Store cardinal direction on player.

This commit is contained in:
Nolan Darilek 2021-07-26 15:50:25 -05:00
parent 12b7ecd2ea
commit 42513e7912

View File

@ -174,26 +174,31 @@ fn movement_controls<S, A: 'static>(
} }
} }
fn speak_direction( fn update_direction(
mut tts: ResMut<Tts>, mut commands: Commands,
mut cache: Local<HashMap<Entity, CardinalDirection>>, query: Query<
player: Query<(Entity, &Player, &Transform), Changed<Transform>>, (Entity, &Transform, Option<&CardinalDirection>),
) -> Result<(), Box<dyn Error>> { (With<Player>, Changed<Transform>),
if let Ok((entity, _, transform)) = player.single() { >,
) {
for (entity, transform, direction) in query.iter() {
let forward = transform.local_x(); let forward = transform.local_x();
let yaw = Angle::Radians(forward.y.atan2(forward.x)); let yaw = Angle::Radians(forward.y.atan2(forward.x));
if let Some(old_direction) = cache.get(&entity) { let new_direction: CardinalDirection = yaw.into();
let old_direction = *old_direction; if direction != Some(&new_direction) {
let direction: CardinalDirection = yaw.into(); commands.entity(entity).insert(new_direction);
if old_direction != direction { }
let direction: String = direction.into(); }
}
fn speak_direction(
mut tts: ResMut<Tts>,
player: Query<&CardinalDirection, (With<Player>, Changed<CardinalDirection>)>,
) -> Result<(), Box<dyn Error>> {
if let Ok(direction) = player.single() {
let direction: String = (*direction).into();
tts.speak(direction, true)?; tts.speak(direction, true)?;
} }
cache.insert(entity, direction);
} else {
cache.insert(entity, yaw.into());
}
}
Ok(()) Ok(())
} }
@ -258,6 +263,7 @@ where
app.register_type::<MaxSpeed>() app.register_type::<MaxSpeed>()
.register_type::<RotationSpeed>() .register_type::<RotationSpeed>()
.register_type::<Sprinting>() .register_type::<Sprinting>()
.add_system(update_direction.system())
.add_system(speak_direction.system().chain(error_handler.system())); .add_system(speak_direction.system().chain(error_handler.system()));
if config.movement_control_states.is_empty() { if config.movement_control_states.is_empty() {
app.add_system(movement_controls::<S, A>.system()); app.add_system(movement_controls::<S, A>.system());