forked from djeedai/bevy_hanabi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activate.rs
187 lines (162 loc) Β· 5.95 KB
/
activate.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! 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, prelude::*, render::camera::ScalingMode};
use bevy_hanabi::prelude::*;
mod utils;
use utils::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_exit = utils::make_test_app("activate")
.add_systems(Startup, setup)
.add_systems(Update, update)
.run();
app_exit.into_result()
}
#[derive(Component)]
struct StatusText;
#[derive(Component)]
struct Ball {
velocity_y: f32,
}
fn setup(
mut commands: Commands,
mut effects: ResMut<Assets<EffectAsset>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let mut camera = Camera3dBundle {
tonemapping: Tonemapping::None,
..default()
};
let mut projection = OrthographicProjection::default();
projection.scaling_mode = ScalingMode::FixedVertical(2.);
projection.scale = 1.0;
camera.transform.translation.z = projection.far / 2.0;
camera.projection = Projection::Orthographic(projection);
commands.spawn(camera);
// Blue rectangle mesh for "water"
commands
.spawn(PbrBundle {
mesh: meshes.add(Rectangle {
half_size: Vec2::splat(2.0),
}),
material: materials.add(StandardMaterial {
base_color: utils::COLOR_BLUE,
unlit: true,
..Default::default()
}),
transform: Transform::from_xyz(0.0, -2.0, 0.0),
..Default::default()
})
.insert(Name::new("water"));
let mut ball = commands.spawn(PbrBundle {
mesh: meshes.add(Sphere { radius: 0.05 }),
material: materials.add(StandardMaterial {
base_color: Color::WHITE,
unlit: true,
..Default::default()
}),
..Default::default()
});
ball.insert(Ball { velocity_y: 1.0 })
.insert(Name::new("ball"));
let mut gradient = Gradient::new();
gradient.add_key(0.0, Vec4::new(0.5, 0.5, 1.0, 1.0));
gradient.add_key(1.0, Vec4::new(0.5, 0.5, 1.0, 0.0));
let spawner = Spawner::rate(30.0.into()).with_starts_active(false);
let writer = ExprWriter::new();
let age = writer.lit(0.).expr();
let init_age = SetAttributeModifier::new(Attribute::AGE, age);
let lifetime = writer.lit(5.).expr();
let init_lifetime = SetAttributeModifier::new(Attribute::LIFETIME, lifetime);
let init_pos = SetPositionSphereModifier {
center: writer.lit(Vec3::ZERO).expr(),
radius: writer.lit(0.05).expr(),
dimension: ShapeDimension::Surface,
};
let init_vel = SetVelocitySphereModifier {
center: writer.lit(Vec3::ZERO).expr(),
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);
let effect = effects.add(
EffectAsset::new(vec![32768], spawner, module)
.with_name("activate")
.init(init_pos)
.init(init_vel)
.init(init_age)
.init(init_lifetime)
.update(update_buoyancy)
.update(allow_zone)
.render(SetSizeModifier {
size: Vec2::splat(0.02).into(),
})
.render(ColorOverLifetimeModifier { gradient })
.render(round),
);
ball.with_children(|node| {
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;
for (mut ball, mut transform, children) in q_balls.iter_mut() {
let accel = if transform.translation.y >= 0.0 {
-ACCELERATION
} else {
ACCELERATION
};
ball.velocity_y += accel * time.delta_seconds();
transform.translation.y += ball.velocity_y * time.delta_seconds();
// 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(is_active);
}
let mut text = q_text.single_mut();
text.sections[0].value = (if is_active { "Active" } else { "Inactive" }).to_string();
}
}