observers example doesn't follow standards (#13884)

# Objective

- Observers example is using an unseeded random, prints text to console
and doesn't display text as other examples

## Solution

- use seeded random
- log instead of printing
- use common settings for UI text
This commit is contained in:
François Mockers 2024-06-17 02:34:08 +02:00 committed by GitHub
parent 836b6c4409
commit 8b38299bd4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,8 @@ use bevy::{
prelude::*, prelude::*,
utils::{HashMap, HashSet}, utils::{HashMap, HashSet},
}; };
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;
fn main() { fn main() {
App::new() App::new()
@ -47,13 +49,13 @@ struct Mine {
} }
impl Mine { impl Mine {
fn random() -> Self { fn random(rand: &mut ChaCha8Rng) -> Self {
Mine { Mine {
pos: Vec2::new( pos: Vec2::new(
(rand::random::<f32>() - 0.5) * 1200.0, (rand.gen::<f32>() - 0.5) * 1200.0,
(rand::random::<f32>() - 0.5) * 600.0, (rand.gen::<f32>() - 0.5) * 600.0,
), ),
size: 4.0 + rand::random::<f32>() * 16.0, size: 4.0 + rand.gen::<f32>() * 16.0,
} }
} }
} }
@ -67,20 +69,29 @@ struct ExplodeMines {
#[derive(Event)] #[derive(Event)]
struct Explode; struct Explode;
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default()); commands.spawn(Camera2dBundle::default());
commands.spawn(TextBundle::from_section( commands.spawn(
"Click on a \"Mine\" to trigger it.\n\ TextBundle::from_section(
"Click on a \"Mine\" to trigger it.\n\
When it explodes it will trigger all overlapping mines.", When it explodes it will trigger all overlapping mines.",
TextStyle { TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"), color: Color::WHITE,
font_size: 24., ..default()
color: Color::WHITE, },
}, )
)); .with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(12.),
left: Val::Px(12.),
..default()
}),
);
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
commands commands
.spawn(Mine::random()) .spawn(Mine::random(&mut rng))
// Observers can watch for events targeting a specific entity. // Observers can watch for events targeting a specific entity.
// This will create a new observer that runs whenever the Explode event // This will create a new observer that runs whenever the Explode event
// is triggered for this spawned entity. // is triggered for this spawned entity.
@ -97,7 +108,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// As we spawn entities, we can make this observer watch each of them: // As we spawn entities, we can make this observer watch each of them:
for _ in 0..1000 { for _ in 0..1000 {
let entity = commands.spawn(Mine::random()).id(); let entity = commands.spawn(Mine::random(&mut rng)).id();
observer.watch_entity(entity); observer.watch_entity(entity);
} }
@ -140,7 +151,7 @@ fn explode_mine(trigger: Trigger<Explode>, query: Query<&Mine>, mut commands: Co
let Some(mut entity) = commands.get_entity(id) else { let Some(mut entity) = commands.get_entity(id) else {
return; return;
}; };
println!("Boom! {:?} exploded.", id.index()); info!("Boom! {:?} exploded.", id.index());
entity.despawn(); entity.despawn();
let mine = query.get(id).unwrap(); let mine = query.get(id).unwrap();
// Trigger another explosion cascade. // Trigger another explosion cascade.