bevy/examples/ecs/change_detection.rs
François bbb9849506 Replace default method calls from Glam types with explicit const (#1645)
it's a followup of #1550 

I think calling explicit methods/values instead of default makes the code easier to read: "what is `Quat::default()`" vs "Oh, it's `Quat::IDENTITY`"

`Transform::identity()` and `GlobalTransform::identity()` can also be consts and I replaced the calls to their `default()` impl with `identity()`
2021-03-13 18:23:39 +00:00

45 lines
1.4 KiB
Rust

use bevy::prelude::*;
use rand::Rng;
// This example illustrates how to react to component change
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(change_component.system())
.add_system(change_detection.system())
.add_system(flags_monitoring.system())
.run();
}
#[derive(Debug)]
struct MyComponent(f64);
fn setup(mut commands: Commands) {
commands.spawn((MyComponent(0.),));
commands.spawn((Transform::identity(),));
}
fn change_component(time: Res<Time>, mut query: Query<(Entity, &mut MyComponent)>) {
for (entity, mut component) in query.iter_mut() {
if rand::thread_rng().gen_bool(0.1) {
info!("changing component {:?}", entity);
component.0 = time.seconds_since_startup();
}
}
}
// There are query filters for `Changed<T>`, `Added<T>` and `Mutated<T>`
// Only entities matching the filters will be in the query
fn change_detection(query: Query<(Entity, &MyComponent), Changed<MyComponent>>) {
for (entity, component) in query.iter() {
info!("{:?} changed: {:?}", entity, component,);
}
}
// By looking at flags, the query is not filtered but the information is available
fn flags_monitoring(query: Query<(Entity, Option<&MyComponent>, Option<Flags<MyComponent>>)>) {
for (entity, component, flags) in query.iter() {
info!("{:?}: {:?} -> {:?}", entity, component, flags,);
}
}