Simple and safe ECS library for Rust.
Provides basic features, such as: - create and destroy entities; - attach, get or remove components from the entity; - use entry of the entity to modify it; - view components of different types; - view components immutably or mutably.
For now library provides nothing for systems (are responsible for logic). You are free to create your own system!
This crate contains no unsafe
code.
```rust use toucan_ecs::Registry;
let mut registry = Registry::new();
let entity = registry.create(); assert!(registry.contains(entity));
registry.destroy(entity); assert!(!registry.contains(entity)); ```
```rust use toucan_ecs::Registry;
struct Name(&'static str);
struct ID(u32);
let mut registry = Registry::new();
// Create new entity let entity = { let mut entry = registry.create_entry(); entry.attach((Name("Hello, World"), ID(42))); assert!(entry.attached::<(Name, ID)>()); entry.entity() }; assert!(registry.attached::<(Name, ID)>(entity));
// Or reuse existing ones
if let Some(mut entry) = registry.entry(entity) {
entry.removeone::
```rust use toucan_ecs::{Entity, Registry};
struct Position { x: f32, y: f32, }
struct Mass(f32);
let mut registry = Registry::new();
// Create our entities and their data
for i in 0..10 {
let f = i as f32;
let position = Position{ x: f / 10.0, y: -f / 10.0 };
let entity = registry.createwith((position,));
assert!(registry.attachedone::
// Get all entities which have Position
and CAN have Mass
components
for (, mut position, mass) in registry.viewmut::<(Entity, &mut Position, Option<&Mass>)>() {
position.x += 1.0;
println!("position is {:?}, mass is {:?}", *position, mass.as_deref());
}
```