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(
mut tts: ResMut<Tts>,
mut cache: Local<HashMap<Entity, CardinalDirection>>,
player: Query<(Entity, &Player, &Transform), Changed<Transform>>,
) -> Result<(), Box<dyn Error>> {
if let Ok((entity, _, transform)) = player.single() {
fn update_direction(
mut commands: Commands,
query: Query<
(Entity, &Transform, Option<&CardinalDirection>),
(With<Player>, Changed<Transform>),
>,
) {
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<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)?;
}
Ok(())
}
@ -258,6 +263,7 @@ where
app.register_type::<MaxSpeed>()
.register_type::<RotationSpeed>()
.register_type::<Sprinting>()
.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::<S, A>.system());