Spawn and Trigger an Observer
While bevy_pipe_affect effects are somewhat minimal and don't include a command_add_observer, observers still integrate well with effects.
The following code examples are pulled from the observer cargo example.
- Create an event type (
InflateEventin this example)
use bevy::prelude::*; use bevy_pipe_affect::prelude::*; #[derive(Event)] pub struct InflateEvent; #[derive(Component)] pub struct Inflatable; fn inflate(_event: On<InflateEvent>) -> impl Effect + use<> { components_set_filtered_with::<_, _, With<Inflatable>>(|(transform,): (Transform,)| { (transform.with_scale(transform.scale * 1.1),) }) } pub fn spawn_observer() -> impl Effect { command_spawn(Observer::new(inflate.pipe(affect))) } pub fn trigger_observer(input: Res<ButtonInput<KeyCode>>) -> Option<CommandTrigger<InflateEvent>> { if input.just_pressed(KeyCode::Space) { Some(command_trigger(InflateEvent)) } else { None } } fn main() {}
- Create a system that accepts an
On<InflateEvent>and returns an effect that will be ran by the observer.
use bevy::prelude::*; use bevy_pipe_affect::prelude::*; #[derive(Event)] pub struct InflateEvent; #[derive(Component)] pub struct Inflatable; fn inflate(_event: On<InflateEvent>) -> impl Effect + use<> { components_set_filtered_with::<_, _, With<Inflatable>>(|(transform,): (Transform,)| { (transform.with_scale(transform.scale * 1.1),) }) } pub fn spawn_observer() -> impl Effect { command_spawn(Observer::new(inflate.pipe(affect))) } pub fn trigger_observer(input: Res<ButtonInput<KeyCode>>) -> Option<CommandTrigger<InflateEvent>> { if input.just_pressed(KeyCode::Space) { Some(command_trigger(InflateEvent)) } else { None } } fn main() { bevy::ecs::system::assert_is_system(inflate.pipe(affect)) }
- Create a system that spawns an
Observercomponent withcommand_spawn, using the previous system.pipe(affect)-ed as the observer system.
use bevy::prelude::*; use bevy_pipe_affect::prelude::*; #[derive(Event)] pub struct InflateEvent; #[derive(Component)] pub struct Inflatable; fn inflate(_event: On<InflateEvent>) -> impl Effect + use<> { components_set_filtered_with::<_, _, With<Inflatable>>(|(transform,): (Transform,)| { (transform.with_scale(transform.scale * 1.1),) }) } pub fn spawn_observer() -> impl Effect { command_spawn(Observer::new(inflate.pipe(affect))) } pub fn trigger_observer(input: Res<ButtonInput<KeyCode>>) -> Option<CommandTrigger<InflateEvent>> { if input.just_pressed(KeyCode::Space) { Some(command_trigger(InflateEvent)) } else { None } } fn main() { bevy::ecs::system::assert_is_system(spawn_observer.pipe(affect)) }
- Trigger your observer with the
command_triggereffect.
use bevy::prelude::*; use bevy_pipe_affect::prelude::*; #[derive(Event)] pub struct InflateEvent; #[derive(Component)] pub struct Inflatable; fn inflate(_event: On<InflateEvent>) -> impl Effect + use<> { components_set_filtered_with::<_, _, With<Inflatable>>(|(transform,): (Transform,)| { (transform.with_scale(transform.scale * 1.1),) }) } pub fn spawn_observer() -> impl Effect { command_spawn(Observer::new(inflate.pipe(affect))) } pub fn trigger_observer(input: Res<ButtonInput<KeyCode>>) -> Option<CommandTrigger<InflateEvent>> { if input.just_pressed(KeyCode::Space) { Some(command_trigger(InflateEvent)) } else { None } } fn main() { bevy::ecs::system::assert_is_system(trigger_observer.pipe(affect)) }