Skip to content

Commit 2da6d8d

Browse files
authored
Update Activate example with status text (#315)
Update the `activate.rs` example to add a text showing the `Spawner` status, and a `KillAabbModifier` to constraint particles inside the water.
1 parent 60e21ff commit 2da6d8d

File tree

7 files changed

+56
-13
lines changed

7 files changed

+56
-13
lines changed

.github/workflows/ci.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,19 @@ jobs:
123123
for example in .github/example-run/3d/*.ron; do
124124
example_name=`basename $example .ron`
125125
echo "running $example_name - "`date`
126-
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"
126+
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"
127127
sleep 10
128128
done
129129
for example in .github/example-run/3dpng/*.ron; do
130130
example_name=`basename $example .ron`
131131
echo "running $example_name - "`date`
132-
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"
132+
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"
133133
sleep 10
134134
done
135135
for example in .github/example-run/2d/*.ron; do
136136
example_name=`basename $example .ron`
137137
echo "running $example_name - "`date`
138-
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"
138+
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"
139139
sleep 10
140140
done
141141
env:

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ required-features = [ "bevy/bevy_winit", "bevy/bevy_pbr", "3d" ]
110110

111111
[[example]]
112112
name = "activate"
113-
required-features = [ "bevy/bevy_winit", "bevy/bevy_pbr", "3d" ]
113+
required-features = [ "bevy/bevy_winit", "bevy/bevy_pbr", "bevy/bevy_ui", "bevy/default_font", "3d" ]
114114

115115
[[example]]
116116
name = "force_field"

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ cargo run --example multicam --features="bevy/bevy_winit bevy/bevy_pbr 3d"
202202

203203
### Activate
204204

205-
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.
205+
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.
206206

207207
```shell
208208
cargo run --example activate --features="bevy/bevy_winit bevy/bevy_pbr 3d"

examples/activate.gif

-420 KB
Loading

examples/activate.rs

+49-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
//! A circle bobs up and down in the water, spawning square bubbles when in the
2-
//! water.
1+
//! A circle bobs up and down in the water, spawning bubbles when in the water.
32
//!
43
//! This example demonstrates the use of [`Spawner::set_active()`] to enable or
54
//! disable particle spawning, under the control of the application. This is
65
//! similar to the `spawn_on_command.rs` example, where [`Spawner::reset()`] is
76
//! used instead to spawn a single burst of particles.
7+
//!
8+
//! A small vertical acceleration simulate a pseudo-buoyancy making the bubbles
9+
//! slowly move upward toward the surface. The example uses a
10+
//! [`KillAabbModifier`] to ensure the bubble particles never escape water, and
11+
//! are despawned when reaching the surface.
812
913
use bevy::{
1014
core_pipeline::tonemapping::Tonemapping,
@@ -58,6 +62,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5862
Ok(())
5963
}
6064

65+
#[derive(Component)]
66+
struct StatusText;
67+
6168
#[derive(Component)]
6269
struct Ball {
6370
velocity_y: f32,
@@ -80,6 +87,7 @@ fn setup(
8087
camera.projection = Projection::Orthographic(projection);
8188
commands.spawn(camera);
8289

90+
// Blue rectangle mesh for "water"
8391
commands
8492
.spawn(PbrBundle {
8593
mesh: meshes.add(Rectangle {
@@ -132,6 +140,14 @@ fn setup(
132140
speed: writer.lit(0.1).expr(),
133141
};
134142

143+
let buoyancy = writer.lit(Vec3::Y * 0.2).expr();
144+
let update_buoyancy = AccelModifier::new(buoyancy);
145+
146+
// Kill particles getting out of water
147+
let center = writer.lit(Vec3::Y * -2.02).expr();
148+
let half_size = writer.lit(Vec3::splat(2.0)).expr();
149+
let allow_zone = KillAabbModifier::new(center, half_size);
150+
135151
let mut module = writer.finish();
136152

137153
let round = RoundModifier::constant(&mut module, 1.0);
@@ -143,9 +159,10 @@ fn setup(
143159
.init(init_vel)
144160
.init(init_age)
145161
.init(init_lifetime)
146-
.render(SizeOverLifetimeModifier {
147-
gradient: Gradient::constant(Vec2::splat(0.02)),
148-
screen_space_size: false,
162+
.update(update_buoyancy)
163+
.update(allow_zone)
164+
.render(SetSizeModifier {
165+
size: Vec2::splat(0.02).into(),
149166
})
150167
.render(ColorOverLifetimeModifier { gradient })
151168
.render(round),
@@ -155,11 +172,33 @@ fn setup(
155172
node.spawn(ParticleEffectBundle::new(effect))
156173
.insert(Name::new("effect"));
157174
});
175+
176+
commands.spawn((
177+
TextBundle {
178+
style: Style {
179+
position_type: PositionType::Absolute,
180+
top: Val::Px(30.),
181+
right: Val::Px(30.),
182+
..default()
183+
},
184+
text: Text::from_section(
185+
"Active",
186+
TextStyle {
187+
font_size: 60.0,
188+
color: Color::WHITE,
189+
..default()
190+
},
191+
),
192+
..default()
193+
},
194+
StatusText,
195+
));
158196
}
159197

160198
fn update(
161199
mut q_balls: Query<(&mut Ball, &mut Transform, &Children)>,
162200
mut q_spawner: Query<&mut EffectSpawner>,
201+
mut q_text: Query<&mut Text, With<StatusText>>,
163202
time: Res<Time>,
164203
) {
165204
const ACCELERATION: f32 = 1.0;
@@ -175,8 +214,12 @@ fn update(
175214
// Note: On first frame where the effect spawns, EffectSpawner is spawned during
176215
// CoreSet::PostUpdate, so will not be available yet. Ignore for a frame
177216
// if so.
217+
let is_active = transform.translation.y < 0.0;
178218
if let Ok(mut spawner) = q_spawner.get_mut(children[0]) {
179-
spawner.set_active(transform.translation.y < 0.0);
219+
spawner.set_active(is_active);
180220
}
221+
222+
let mut text = q_text.single_mut();
223+
text.sections[0].value = (if is_active { "Active" } else { "Inactive" }).to_string();
181224
}
182225
}

run_examples.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cargo r --example multicam --no-default-features --features="bevy/bevy_winit bev
99
cargo r --example visibility --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
1010
cargo r --example random --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
1111
cargo r --example spawn_on_command --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
12-
cargo r --example activate --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
12+
cargo r --example activate --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font 3d examples_world_inspector"
1313
cargo r --example force_field --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
1414
cargo r --example init --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
1515
cargo r --example lifetime --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"

run_examples.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cargo r --example multicam --no-default-features --features="bevy/bevy_winit bev
88
cargo r --example visibility --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
99
cargo r --example random --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
1010
cargo r --example spawn_on_command --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
11-
cargo r --example activate --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
11+
cargo r --example activate --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr bevy/bevy_ui bevy/default_font 3d examples_world_inspector"
1212
cargo r --example force_field --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
1313
cargo r --example init --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"
1414
cargo r --example lifetime --no-default-features --features="bevy/bevy_winit bevy/bevy_pbr 3d examples_world_inspector"

0 commit comments

Comments
 (0)