Prefer writing a component value where possible, and other minor cleanup.

This commit is contained in:
Nolan Darilek 2022-02-14 06:56:45 -06:00
parent 1176c65ce9
commit 6c97ff6be4

View File

@ -58,7 +58,7 @@ fn movement_controls<S, A: 'static>(
),
With<Player>,
>,
exploration_focused: Query<(Entity, &ExplorationFocused)>,
exploration_focused: Query<Entity, With<ExplorationFocused>>,
) where
S: bevy::ecs::component::Component + Clone + Debug + Eq + Hash,
A: Hash + Eq + Clone + Send + Sync,
@ -117,7 +117,7 @@ fn movement_controls<S, A: 'static>(
if direction.length_squared() != 0. {
commands.entity(entity).remove::<Destination>();
commands.entity(entity).remove::<Exploring>();
for (entity, _) in exploration_focused.iter() {
for entity in exploration_focused.iter() {
commands.entity(entity).remove::<ExplorationFocused>();
}
direction = direction.normalize();
@ -176,16 +176,20 @@ fn movement_controls<S, A: 'static>(
fn update_direction(
mut commands: Commands,
query: Query<
(Entity, &Transform, Option<&CardinalDirection>),
mut query: Query<
(Entity, &Transform, Option<&mut CardinalDirection>),
(With<Player>, Changed<Transform>),
>,
) {
for (entity, transform, direction) in query.iter() {
for (entity, transform, direction) in query.iter_mut() {
let forward = transform.local_x();
let yaw = Angle::Radians(forward.y.atan2(forward.x));
let new_direction: CardinalDirection = yaw.into();
if direction != Some(&new_direction) {
if let Some(mut direction) = direction {
if *direction != new_direction {
*direction = new_direction;
}
} else {
commands.entity(entity).insert(new_direction);
}
}