nya-ecs 0.2.0

nya entity component system
Documentation
//! Query example
#![allow(unused)]
#![allow(dead_code)]

use nya_ecs::{
    ComponentKey, Filter, Query, Queryable as _, World, component, component_id, query::Exclude,
};

struct Tag;
component!(Tag);

struct Name(&'static str);
component!(Name);

struct Num(usize);
component!(Num);

fn main() {
    let mut world = World::new();

    for i in 0..5 {
        let ent = world.spawn();

        world.add(ent, Tag);
        world.add(ent, Name("Object"));

        if i % 2 == 0 {
            world.add(ent, Num(i));
        }
    }

    {
        let ent = world.spawn();
        world.add(ent, Num(1337));
    }

    /// Query with component tuple
    let query = world.query::<(Tag, Num)>();
    for e in &query {
        println!("Entity #{e} has both `Tag` and `Num`");
    }

    /// you can also use `Exclude` to ensure some components are not included
    for e in &world.query::<(Tag, Exclude<(Num,)>)>() {
        println!("Entity #{e} has `Tag` without `Num`");
    }

    // `Filter` allows for more fine-grained control.
    // In fact, the (Component1, Component2, ...) tuple implements `ToFilter` so it can be used in a query
    //
    // `ComponentKey` is used to identify component types,
    // the `component_id` macro can be used as a shorthand to `ComponentKey` functions.
    let filter = Filter::new()
        .with_include(&[ComponentKey::of::<Tag>(), ComponentKey::of::<Name>()])
        .with_exclude(&[component_id!(Num)]);

    for _ in &world.query_filter(filter) {
        // do stuff
    }
}