Sparsey is a sparse set-based Entity Component System (ECS).
Send + Sync + 'static
type can be used as a component.```rust use sparsey::prelude::*;
struct Position(f32); struct Velocity(f32); struct Frozen;
fn updatevelocities(mut velocities: CompMut
fn updatepositions(mut positions: CompMut
fn main() { let mut schedule = Schedule::builder() .addsystem(updatevelocities) .addsystem(updatepositions) .build();
let mut world = World::default();
schedule.set_up(&mut world);
world.create((Position(0.0), Velocity(1.0)));
world.create((Position(0.0), Velocity(2.0)));
world.create((Position(0.0), Velocity(3.0), Frozen));
let mut resources = Resources::default();
for _ in 0..5 {
schedule.run(&mut world, &mut resources);
}
} ```
Systems are plain functions that borrow data from World
and Resources
.
```rust
fn updatepositions(mut positions: CompMut
fn updatehps(mut hps: CompMut
Systems will be scheduled to run in parallel if their paramters don't conflict.
```rust let mut schedule = Schedule::builder() .addsystem(updatepositions) .addsystem(updatehps) .build();
schedule.run(&mut world, &mut resources); ```
Get, include and exclude components using Sparsey's query API.
```rust
fn queries(a: Comp, b: Comp, c: Comp
// Iter components A from entities with A and B.
for a in (&a).include(&b).iter() {}
// Iter components A from entities with A and without B.
for a in (&a).exclude(&b).iter() {}
// Iter components A from entities with A and B, without C.
for a in (&a).include(&b).exclude(&c).iter() {}
} ```
Sparsey allows the user to "group" component storages to greatly optimize
iteration performance. Groups are created by setting a Layout
on the World
.
```rust let layout = Layout::builder() .addgroup(<(A, B)>::group()) .addgroup(<(A, B, C, D>)>::group()) .build();
let world = World::with_layout(&layout); ```
After the layout is set, iterators over the grouped storages become "dense", greatly improving their performance. Additionally, grouped storages allow access to their components and entities as slices.
```rust fn denseiterators(a: Comp, b: Comp) { if let Some(entities) = (&a, &b).intoentities() { // ... }
if let Some((a_slice, b_slice)) = (&a, &b).into_components() {
// ...
}
if let Some((entities, (a_slice, b_slice))) = (&a, &b).into_entities_and_components() {
// ...
}
} ```
Sparsey takes inspiration and borrows features from other free and open source ECS projects, namely Bevy, EnTT, Legion, Shipyard and Specs. Make sure you check them out!
Sparsey is dual-licensed under either
MIT License (LICENSE-MIT or https://opensource.org/license/mit/)
Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above without any additional terms or conditions.