File size: 2,665 Bytes
48ca417 |
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 |
# `bevy_flurx`
---
## Spawn and Respawn `flurx` Reactor
---
```rust
//! This example shows how to toggle [`Reactor`] processing.
//!
//! When you press [`KeyCode::Escape`], the box stops rotating.
//! When you press [`KeyCode::Enter`], the box starts rotating again.
//!
//! [`Reactor`]: bevy_flurx::prelude::Reactor
use bevy::DefaultPlugins;
use bevy::prelude::*;
use bevy_flurx::prelude::*;
#[derive(Component)]
struct RotateBox;
fn main() {
App::new()
.add_plugins((DefaultPlugins, FlurxPlugin))
.add_systems(Startup, (setup_camera_and_box, spawn_reactor))
.add_systems(Update, toggle)
.run();
}
fn setup_camera_and_box(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>
) {
commands.spawn((
PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(StandardMaterial {
base_color: Color::BLUE,
..default()
}),
..default()
},
RotateBox,
));
commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
intensity: 10_000_000.0,
range: 100.0,
..default()
},
transform: Transform::from_xyz(8.0, 16.0, 8.0),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 6.0),
..default()
});
}
fn spawn_reactor(mut commands: Commands) {
commands.spawn(
Reactor::schedule(|task| async move {
task.will(Update, wait::until(rotate_shape)).await;
})
);
}
fn rotate_shape(mut shape: Query<&mut Transform, With<RotateBox>>, time: Res<Time>) -> bool {
for mut t in shape.iter_mut() {
t.rotate_y(time.delta_seconds());
}
false
}
fn toggle(
mut commands: Commands,
reactor: Query<Entity, With<Reactor>>,
input: Res<ButtonInput<KeyCode>>
) {
if input.just_pressed(KeyCode::Escape) {
if let Ok(entity) = reactor.get_single() {
info!("Despawning reactor.");
commands.entity(entity).remove::<Reactor>();
}
}
if input.just_pressed(KeyCode::Enter) {
if reactor.iter().next().is_none() {
info!("Spawning reactor.");
commands.spawn(
Reactor::schedule(|task| async move {
task.will(Update, wait::until(rotate_shape)).await;
})
);
}
}
}
```
|