4 releases (breaking)

0.3.0 Sep 24, 2023
0.2.0 Sep 3, 2023
0.1.0 Jan 17, 2023
0.0.0 Jan 16, 2023

#855 in Game dev

AGPL-3.0-only

29KB
864 lines

checs

An Entity-Component-System library.

Example

This is a very basic example of how to use checs.

You can find this example at examples/basic.rs. Run it with:

cargo run --example basic

Define some components and a World to store them in.

Note: Components are just plain old Rust types.

struct Position { x: u32, y: u32, }
struct Health(i32);
struct Visible;

checs::impl_storage!(
    struct Storage {
        positions: Position,
        healths: Health,
        visibles: Visible,
    }
);

let mut world = checs::world::World::<Storage>::new();

Create entities with initial components.

// Either manually...
let player = world.spawn();
world.put(player, Position { x: 0, y: 0 });
world.put(player, Visible);
world.put(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 });
let enemy = checs::spawn!(world, Position { x: 1, y: 4 }, Visible, Health(100));

Find the entities that have some components.

use checs::iter::LendingIterator;
use checs::query::IntoQuery;

let ps = &world.storage.positions;
let hs = &mut world.storage.healths;
let vs = &world.storage.visibles;

let query = (ps, hs, vs).into_query();

query.for_each(|(e, (p, h, _v))| {
    h.0 = h.0 - 17;
    println!("{e:?} at ({}, {}) has {} HP.", p.x, p.y, h.0);
});

// Entity(0) is Visible at (0, 0) with 63 HP.
// Entity(3) is Visible at (1, 4) with 83 HP.

No runtime deps