More query filter usage (#851)

* Examples now use With<>

* More Bevy systems now use With<>

* parent_update_system now uses Changed<>
This commit is contained in:
MinerSebas 2020-11-13 03:22:46 +01:00 committed by GitHub
parent 6b8b8e75e5
commit 43aac1a784
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 29 additions and 27 deletions

View File

@ -1,13 +1,15 @@
use crate::components::*; use crate::components::*;
use bevy_ecs::{Commands, Entity, IntoSystem, Query, System, Without}; use bevy_ecs::{Changed, Commands, Entity, IntoSystem, Query, System, Without};
use bevy_utils::HashMap; use bevy_utils::HashMap;
use smallvec::SmallVec; use smallvec::SmallVec;
pub fn parent_update_system( pub fn parent_update_system(
commands: &mut Commands, commands: &mut Commands,
removed_parent_query: Query<(Entity, &PreviousParent), Without<Parent>>, removed_parent_query: Query<(Entity, &PreviousParent), Without<Parent>>,
// TODO: ideally this only runs when the Parent component has changed mut changed_parent_query: Query<
mut changed_parent_query: Query<(Entity, &Parent, Option<&mut PreviousParent>)>, (Entity, &Parent, Option<&mut PreviousParent>),
Changed<Parent>,
>,
mut children_query: Query<&mut Children>, mut children_query: Query<&mut Children>,
) { ) {
// Entities with a missing `Parent` (ie. ones that have a `PreviousParent`), remove // Entities with a missing `Parent` (ie. ones that have a `PreviousParent`), remove

View File

@ -9,7 +9,7 @@ pub const UI_Z_STEP: f32 = 0.001;
pub fn ui_z_system( pub fn ui_z_system(
root_node_query: Query<Entity, (With<Node>, Without<Parent>)>, root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
mut node_query: Query<(Entity, &Node, &mut Transform)>, mut node_query: Query<(Entity, &mut Transform), With<Node>>,
children_query: Query<&Children>, children_query: Query<&Children>,
) { ) {
let mut current_global_z = 0.0; let mut current_global_z = 0.0;
@ -29,7 +29,7 @@ pub fn ui_z_system(
} }
fn update_node_entity( fn update_node_entity(
node_query: &mut Query<(Entity, &Node, &mut Transform)>, node_query: &mut Query<(Entity, &mut Transform), With<Node>>,
entity: Entity, entity: Entity,
parent_result: Option<f32>, parent_result: Option<f32>,
previous_result: Option<f32>, previous_result: Option<f32>,

View File

@ -1,6 +1,6 @@
use crate::CalculatedSize; use crate::CalculatedSize;
use bevy_asset::{Assets, Handle}; use bevy_asset::{Assets, Handle};
use bevy_ecs::{Query, Res}; use bevy_ecs::{Query, Res, With};
use bevy_math::Size; use bevy_math::Size;
use bevy_render::texture::Texture; use bevy_render::texture::Texture;
use bevy_sprite::ColorMaterial; use bevy_sprite::ColorMaterial;
@ -19,9 +19,9 @@ impl Default for Image {
pub fn image_node_system( pub fn image_node_system(
materials: Res<Assets<ColorMaterial>>, materials: Res<Assets<ColorMaterial>>,
textures: Res<Assets<Texture>>, textures: Res<Assets<Texture>>,
mut query: Query<(&Image, &mut CalculatedSize, &Handle<ColorMaterial>)>, mut query: Query<(&mut CalculatedSize, &Handle<ColorMaterial>), With<Image>>,
) { ) {
for (_image, mut calculated_size, material_handle) in query.iter_mut() { for (mut calculated_size, material_handle) in query.iter_mut() {
if let Some(texture) = materials if let Some(texture) = materials
.get(material_handle) .get(material_handle)
.and_then(|material| material.texture.as_ref()) .and_then(|material| material.texture.as_ref())

View File

@ -131,12 +131,12 @@ fn setup(
fn select_system( fn select_system(
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
mut sel: ResMut<ContributorSelection>, mut sel: ResMut<ContributorSelection>,
mut dq: Query<(&ContributorDisplay, Mut<Text>)>, mut dq: Query<Mut<Text>, With<ContributorDisplay>>,
mut tq: Query<(&SelectTimer, Mut<Timer>)>, mut tq: Query<Mut<Timer>, With<SelectTimer>>,
mut q: Query<(&Contributor, &Handle<ColorMaterial>, &mut Transform)>, mut q: Query<(&Contributor, &Handle<ColorMaterial>, &mut Transform)>,
) { ) {
let mut timer_fired = false; let mut timer_fired = false;
for (_, mut t) in tq.iter_mut() { for mut t in tq.iter_mut() {
if !t.just_finished { if !t.just_finished {
continue; continue;
} }
@ -166,7 +166,7 @@ fn select_system(
let (name, e) = &sel.order[sel.idx]; let (name, e) = &sel.order[sel.idx];
if let Ok((c, handle, mut tr)) = q.get_mut(*e) { if let Ok((c, handle, mut tr)) = q.get_mut(*e) {
for (_, mut text) in dq.iter_mut() { for mut text in dq.iter_mut() {
select( select(
&mut *materials, &mut *materials,
handle.clone(), handle.clone(),
@ -231,7 +231,7 @@ fn velocity_system(time: Res<Time>, mut q: Query<Mut<Velocity>>) {
/// force. /// force.
fn collision_system( fn collision_system(
wins: Res<Windows>, wins: Res<Windows>,
mut q: Query<(&Contributor, Mut<Velocity>, Mut<Transform>)>, mut q: Query<(Mut<Velocity>, Mut<Transform>), With<Contributor>>,
) { ) {
let mut rnd = rand::thread_rng(); let mut rnd = rand::thread_rng();
@ -243,7 +243,7 @@ fn collision_system(
let wall_left = -((win.width() / 2) as f32); let wall_left = -((win.width() / 2) as f32);
let wall_right = (win.width() / 2) as f32; let wall_right = (win.width() / 2) as f32;
for (_, mut v, mut t) in q.iter_mut() { for (mut v, mut t) in q.iter_mut() {
let left = t.translation.x() - SPRITE_SIZE / 2.0; let left = t.translation.x() - SPRITE_SIZE / 2.0;
let right = t.translation.x() + SPRITE_SIZE / 2.0; let right = t.translation.x() + SPRITE_SIZE / 2.0;
let top = t.translation.y() + SPRITE_SIZE / 2.0; let top = t.translation.y() + SPRITE_SIZE / 2.0;

View File

@ -15,8 +15,8 @@ fn main() {
struct Rotator; struct Rotator;
/// rotates the parent, which will result in the child also rotating /// rotates the parent, which will result in the child also rotating
fn rotator_system(time: Res<Time>, mut query: Query<(&Rotator, &mut Transform)>) { fn rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<Rotator>>) {
for (_rotator, mut transform) in query.iter_mut() { for mut transform in query.iter_mut() {
transform.rotation *= Quat::from_rotation_x(3.0 * time.delta_seconds); transform.rotation *= Quat::from_rotation_x(3.0 * time.delta_seconds);
} }
} }

View File

@ -19,18 +19,18 @@ fn main() {
struct Rotator; struct Rotator;
/// rotates the parent, which will result in the child also rotating /// rotates the parent, which will result in the child also rotating
fn rotator_system(time: Res<Time>, mut query: Query<(&Rotator, &mut Transform)>) { fn rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<Rotator>>) {
for (_rotator, mut transform) in query.iter_mut() { for mut transform in query.iter_mut() {
transform.rotation *= Quat::from_rotation_x(3.0 * time.delta_seconds); transform.rotation *= Quat::from_rotation_x(3.0 * time.delta_seconds);
} }
} }
fn camera_order_color_system( fn camera_order_color_system(
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
camera_query: Query<(&Camera, &VisibleEntities)>, camera_query: Query<&VisibleEntities, With<Camera>>,
material_query: Query<&Handle<StandardMaterial>>, material_query: Query<&Handle<StandardMaterial>>,
) { ) {
for (_camera, visible_entities) in camera_query.iter() { for visible_entities in camera_query.iter() {
for visible_entity in visible_entities.iter() { for visible_entity in visible_entities.iter() {
if let Ok(material_handle) = material_query.get(visible_entity.entity) { if let Ok(material_handle) = material_query.get(visible_entity.entity) {
let material = materials.get_mut(&*material_handle).unwrap(); let material = materials.get_mut(&*material_handle).unwrap();

View File

@ -91,11 +91,11 @@ fn setup(
fn rotate( fn rotate(
commands: &mut Commands, commands: &mut Commands,
time: Res<Time>, time: Res<Time>,
mut parents_query: Query<(Entity, &mut Children, &Sprite)>, mut parents_query: Query<(Entity, &mut Children), With<Sprite>>,
mut transform_query: Query<&mut Transform, With<Sprite>>, mut transform_query: Query<&mut Transform, With<Sprite>>,
) { ) {
let angle = std::f32::consts::PI / 2.0; let angle = std::f32::consts::PI / 2.0;
for (parent, mut children, _) in parents_query.iter_mut() { for (parent, mut children) in parents_query.iter_mut() {
if let Ok(mut transform) = transform_query.get_mut(parent) { if let Ok(mut transform) = transform_query.get_mut(parent) {
transform.rotate(Quat::from_rotation_z(-angle * time.delta_seconds)); transform.rotate(Quat::from_rotation_z(-angle * time.delta_seconds));
} }

View File

@ -30,12 +30,12 @@ impl FromResources for ButtonMaterials {
fn button_system( fn button_system(
button_materials: Res<ButtonMaterials>, button_materials: Res<ButtonMaterials>,
mut interaction_query: Query< mut interaction_query: Query<
(&Button, &Interaction, &mut Handle<ColorMaterial>, &Children), (&Interaction, &mut Handle<ColorMaterial>, &Children),
Mutated<Interaction>, (Mutated<Interaction>, With<Button>),
>, >,
mut text_query: Query<&mut Text>, mut text_query: Query<&mut Text>,
) { ) {
for (_button, interaction, mut material, children) in interaction_query.iter_mut() { for (interaction, mut material, children) in interaction_query.iter_mut() {
let mut text = text_query.get_mut(children[0]).unwrap(); let mut text = text_query.get_mut(children[0]).unwrap();
match *interaction { match *interaction {
Interaction::Clicked => { Interaction::Clicked => {

View File

@ -16,8 +16,8 @@ fn main() {
// A unit struct to help identify the FPS UI component, since there may be many Text components // A unit struct to help identify the FPS UI component, since there may be many Text components
struct FpsText; struct FpsText;
fn text_update_system(diagnostics: Res<Diagnostics>, mut query: Query<(&mut Text, &FpsText)>) { fn text_update_system(diagnostics: Res<Diagnostics>, mut query: Query<&mut Text, With<FpsText>>) {
for (mut text, _tag) in query.iter_mut() { for mut text in query.iter_mut() {
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) { if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(average) = fps.average() { if let Some(average) = fps.average() {
text.value = format!("FPS: {:.2}", average); text.value = format!("FPS: {:.2}", average);