Skip to content

Commit

Permalink
observers example doesn't follow standards (#13884)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
mockersf authored Jun 17, 2024
1 parent 836b6c4 commit 8b38299
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions examples/ecs/observers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use bevy::{
prelude::*,
utils::{HashMap, HashSet},
};
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;

fn main() {
App::new()
Expand Down Expand Up @@ -47,13 +49,13 @@ struct Mine {
}

impl Mine {
fn random() -> Self {
fn random(rand: &mut ChaCha8Rng) -> Self {
Mine {
pos: Vec2::new(
(rand::random::<f32>() - 0.5) * 1200.0,
(rand::random::<f32>() - 0.5) * 600.0,
(rand.gen::<f32>() - 0.5) * 1200.0,
(rand.gen::<f32>() - 0.5) * 600.0,
),
size: 4.0 + rand::random::<f32>() * 16.0,
size: 4.0 + rand.gen::<f32>() * 16.0,
}
}
}
Expand All @@ -67,20 +69,29 @@ struct ExplodeMines {
#[derive(Event)]
struct Explode;

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn(TextBundle::from_section(
"Click on a \"Mine\" to trigger it.\n\
commands.spawn(
TextBundle::from_section(
"Click on a \"Mine\" to trigger it.\n\
When it explodes it will trigger all overlapping mines.",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 24.,
color: Color::WHITE,
},
));
TextStyle {
color: Color::WHITE,
..default()
},
)
.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
.spawn(Mine::random())
.spawn(Mine::random(&mut rng))
// Observers can watch for events targeting a specific entity.
// This will create a new observer that runs whenever the Explode event
// is triggered for this spawned entity.
Expand All @@ -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:
for _ in 0..1000 {
let entity = commands.spawn(Mine::random()).id();
let entity = commands.spawn(Mine::random(&mut rng)).id();
observer.watch_entity(entity);
}

Expand Down Expand Up @@ -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 {
return;
};
println!("Boom! {:?} exploded.", id.index());
info!("Boom! {:?} exploded.", id.index());
entity.despawn();
let mine = query.get(id).unwrap();
// Trigger another explosion cascade.
Expand Down

0 comments on commit 8b38299

Please sign in to comment.