Skip to content

Commit

Permalink
Update Activate example with status text (#315)
Browse files Browse the repository at this point in the history
Update the `activate.rs` example to add a text showing the `Spawner`
status, and a `KillAabbModifier` to constraint particles inside the
water.
  • Loading branch information
djeedai authored Apr 6, 2024
1 parent 60e21ff commit 2da6d8d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,19 @@ jobs:
for example in .github/example-run/3d/*.ron; do
example_name=`basename $example .ron`
echo "running $example_name - "`date`
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d bevy/bevy_ci_testing"
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font 3d bevy/bevy_ci_testing"
sleep 10
done
for example in .github/example-run/3dpng/*.ron; do
example_name=`basename $example .ron`
echo "running $example_name - "`date`
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/png 3d bevy/bevy_ci_testing"
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font bevy/png 3d bevy/bevy_ci_testing"
sleep 10
done
for example in .github/example-run/2d/*.ron; do
example_name=`basename $example .ron`
echo "running $example_name - "`date`
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_sprite 2d bevy/bevy_ci_testing"
time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features="bevy/bevy_winit bevy/bevy_sprite bevy/bevy_ui bevy/default_font 2d bevy/bevy_ci_testing"
sleep 10
done
env:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ required-features = [ "bevy/bevy_winit", "bevy/bevy_pbr", "3d" ]

[[example]]
name = "activate"
required-features = [ "bevy/bevy_winit", "bevy/bevy_pbr", "3d" ]
required-features = [ "bevy/bevy_winit", "bevy/bevy_pbr", "bevy/bevy_ui", "bevy/default_font", "3d" ]

[[example]]
name = "force_field"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ cargo run --example multicam --features="bevy/bevy_winit bevy/bevy_pbr 3d"

### Activate

This example demonstrates manual activation and deactivation of a spawner, from code (CPU). The circle bobs up and down in the water, spawning square bubbles when in the water only.
This example demonstrates manual activation and deactivation of a `Spawner`, from code (CPU). The circle bobs up and down in the water, spawning bubbles when in the water only. The bubble particles are constrained to the water with a `KillAabbModifier`, and a small vertical acceleration simulate some pseudo buoyancy.

```shell
cargo run --example activate --features="bevy/bevy_winit bevy/bevy_pbr 3d"
Expand Down
Binary file modified examples/activate.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 49 additions & 6 deletions examples/activate.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
//! A circle bobs up and down in the water, spawning square bubbles when in the
//! water.
//! A circle bobs up and down in the water, spawning bubbles when in the water.
//!
//! This example demonstrates the use of [`Spawner::set_active()`] to enable or
//! disable particle spawning, under the control of the application. This is
//! similar to the `spawn_on_command.rs` example, where [`Spawner::reset()`] is
//! used instead to spawn a single burst of particles.
//!
//! A small vertical acceleration simulate a pseudo-buoyancy making the bubbles
//! slowly move upward toward the surface. The example uses a
//! [`KillAabbModifier`] to ensure the bubble particles never escape water, and
//! are despawned when reaching the surface.
use bevy::{
core_pipeline::tonemapping::Tonemapping,
Expand Down Expand Up @@ -58,6 +62,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[derive(Component)]
struct StatusText;

#[derive(Component)]
struct Ball {
velocity_y: f32,
Expand All @@ -80,6 +87,7 @@ fn setup(
camera.projection = Projection::Orthographic(projection);
commands.spawn(camera);

// Blue rectangle mesh for "water"
commands
.spawn(PbrBundle {
mesh: meshes.add(Rectangle {
Expand Down Expand Up @@ -132,6 +140,14 @@ fn setup(
speed: writer.lit(0.1).expr(),
};

let buoyancy = writer.lit(Vec3::Y * 0.2).expr();
let update_buoyancy = AccelModifier::new(buoyancy);

// Kill particles getting out of water
let center = writer.lit(Vec3::Y * -2.02).expr();
let half_size = writer.lit(Vec3::splat(2.0)).expr();
let allow_zone = KillAabbModifier::new(center, half_size);

let mut module = writer.finish();

let round = RoundModifier::constant(&mut module, 1.0);
Expand All @@ -143,9 +159,10 @@ fn setup(
.init(init_vel)
.init(init_age)
.init(init_lifetime)
.render(SizeOverLifetimeModifier {
gradient: Gradient::constant(Vec2::splat(0.02)),
screen_space_size: false,
.update(update_buoyancy)
.update(allow_zone)
.render(SetSizeModifier {
size: Vec2::splat(0.02).into(),
})
.render(ColorOverLifetimeModifier { gradient })
.render(round),
Expand All @@ -155,11 +172,33 @@ fn setup(
node.spawn(ParticleEffectBundle::new(effect))
.insert(Name::new("effect"));
});

commands.spawn((
TextBundle {
style: Style {
position_type: PositionType::Absolute,
top: Val::Px(30.),
right: Val::Px(30.),
..default()
},
text: Text::from_section(
"Active",
TextStyle {
font_size: 60.0,
color: Color::WHITE,
..default()
},
),
..default()
},
StatusText,
));
}

fn update(
mut q_balls: Query<(&mut Ball, &mut Transform, &Children)>,
mut q_spawner: Query<&mut EffectSpawner>,
mut q_text: Query<&mut Text, With<StatusText>>,
time: Res<Time>,
) {
const ACCELERATION: f32 = 1.0;
Expand All @@ -175,8 +214,12 @@ fn update(
// Note: On first frame where the effect spawns, EffectSpawner is spawned during
// CoreSet::PostUpdate, so will not be available yet. Ignore for a frame
// if so.
let is_active = transform.translation.y < 0.0;
if let Ok(mut spawner) = q_spawner.get_mut(children[0]) {
spawner.set_active(transform.translation.y < 0.0);
spawner.set_active(is_active);
}

let mut text = q_text.single_mut();
text.sections[0].value = (if is_active { "Active" } else { "Inactive" }).to_string();
}
}
2 changes: 1 addition & 1 deletion run_examples.bat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cargo r --example multicam --no-default-features --features="bevy/bevy_winit bev
cargo r --example visibility --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example random --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example spawn_on_command --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example activate --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example activate --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font 3d examples_world_inspector"
cargo r --example force_field --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example init --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example lifetime --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
Expand Down
2 changes: 1 addition & 1 deletion run_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cargo r --example multicam --no-default-features --features="bevy/bevy_winit bev
cargo r --example visibility --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example random --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example spawn_on_command --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example activate --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example activate --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font 3d examples_world_inspector"
cargo r --example force_field --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example init --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
cargo r --example lifetime --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
Expand Down

0 comments on commit 2da6d8d

Please sign in to comment.