bevy_mod_reaction 0.1.0

Reactive components for Bevy
Documentation
  • Coverage
  • 0%
    0 out of 41 items documented0 out of 29 items with examples
  • Size
  • Source code size: 138.65 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 6.73 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3m 8s Average build duration of successful builds.
  • all releases: 3m 6s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • matthunz

bevy_mod_reaction

License Crates.io Downloads Docs CI

Reactive components for Bevy.

use bevy::prelude::*;
use bevy_mod_reaction::{react, Reaction, ReactiveQuery, Scope};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(Update, react)
        .run();
}

#[derive(Component)]
struct Health(i32);

#[derive(Component)]
struct Damage(i32);

#[derive(Component)]
struct Armor(i32);

fn setup(mut commands: Commands) {
    // Coarse-grained reactivity:
    // This reaction will only run when the `Health` component belonging to `scope.entity` changes.
    commands.spawn((
        Health(100),
        Reaction::derive(|scope: In<Scope>, mut query: ReactiveQuery<&Health>| {
            let health = query.get(scope.entity).unwrap();
            Damage(health.0 * 2)
        }),
    ));

    commands.spawn(Reaction::new(|_: In<Scope>, query: Query<&Damage>| {
        for dmg in &query {
            dbg!(dmg.0);
        }
    }));

    commands.spawn((
        Health(0),
        Reaction::switch(
            |scope: In<Scope>, query: Query<&Health>| {
                let dmg = query.get(scope.entity).unwrap();
                dmg.0 == 0
            },
            || Armor(50),
            || Damage(100),
        ),
    ));
}