checs 0.5.1

An Entity-Component-System library.
Documentation
use checs::component::ComponentVec;
use checs::iter::LendingIterator;
use checs::query::IntoQuery;
use checs::world;
use checs::Storage;

struct Position {
    x: u32,
    y: u32,
}
struct Health(i32);
#[derive(Debug)]
struct Visible;

#[derive(Default, Storage)]
struct Storage {
    positions: ComponentVec<Position>,
    healths: ComponentVec<Health>,
    visibles: ComponentVec<Visible>,
}

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

    // Create entities with initial components.
    // Either manually...
    let player = world.spawn();
    world.insert(player, Position { x: 0, y: 0 });
    world.insert(player, Visible);
    world.insert(player, Health(80));

    // ...or by using the `spawn` macro...
    let _obstacle = checs::spawn!(world, Position { x: 1, y: 1 }, Visible);
    let _trap = checs::spawn!(world, Position { x: 2, y: 1 });

    // ...or by using `spawn_with`.
    let _enemy = world.spawn_with((Position { x: 1, y: 4 }, Visible, Health(100)));

    let storage = world.storage_mut();

    // Create a query over all entities that have `Position`, `Health`, and
    // `Visible` components.
    let query = (&storage.positions, &mut storage.healths, &storage.visibles).into_query();

    query.for_each(|(_, (_, h, _))| {
        h.0 += 10;
    });

    println!();

    let query = (&storage.positions, &storage.healths, &storage.visibles).into_query();

    query.for_each(|(e, (p, h, v))| {
        println!("{e:?} is {v:?} at ({}, {}) with {} HP", p.x, p.y, h.0);
    });
}