5 releases

0.2.0-alpha.4 Oct 9, 2024
0.2.0-alpha.3 Oct 4, 2024
0.2.0-alpha.2 Apr 25, 2024
0.2.0-alpha.1 Apr 24, 2024
0.1.0 Apr 10, 2024

#2100 in Game dev

Download history 1/week @ 2024-07-27 5/week @ 2024-09-21 24/week @ 2024-09-28 192/week @ 2024-10-05 38/week @ 2024-10-12 2/week @ 2024-10-19 13/week @ 2024-11-02

64 downloads per month

MIT/Apache

33KB
307 lines

bevy-compose

Crates.io version docs.rs docs CI status

Reactive ECS plugin for Bevy.

This crate provides a framework for reactive systems using the ECS.

use bevy::prelude::*;
use bevy_compose::TemplatePlugin;

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

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

#[derive(Component)]
struct Zombie;

fn main() {
    App::new()
        .add_plugins(TemplatePlugin::default().with_template(
            // Spawning a Zombie will spawn the following components:
            Zombie,
            (
                // This only runs once.
                || Health(100),
                // This runs every time a `Health` component is updated,
                // and it's guraranteed to run after the `Health` component is updated.
                |entity: In<Entity>, health_query: Query<&Health>| {
                    let health = health_query.get(*entity).unwrap();
                    Damage(**health * 2)
                },
            ),
        ))
        .add_systems(Startup, setup)
        .add_systems(PostUpdate, debug)
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(Zombie);
}

fn debug(query: Query<&Damage>) {
    for dmg in &query {
        dbg!(**dmg);
    }
}

Inspiration

This crate is inspired by Xilem, Concoct and SwiftUI with its typed approach to reactivity.

Dependencies

~20–47MB
~773K SLoC