Crate recs [] [src]

Simple entity-component system. Pure Rust (macro-free)!

Example

extern crate recs;
use recs::{Ecs, EntityId};

#[derive(Clone, PartialEq, Debug)]
struct Age{years: u32}

#[derive(Clone, PartialEq, Debug)]
struct Iq{points: i32}

fn main() {

    // Create an ECS instance
    let mut system: Ecs = Ecs::new();

    // Add entity to the system
    let forrest: EntityId = system.create_entity();

    // Attach components to the entity
    system.set(forrest, &Age{years: 22});
    system.set(forrest, &Iq{points: 75}); // "I may not be a smart man..."

    // Get clone of attached component data from entity
    let age = system.get::<Age>(forrest).unwrap();
    assert_eq!(age.years, 22);

    // Annotating the variable's type may let you skip type parameters
    let iq: Iq = system.get(forrest).unwrap();
    assert_eq!(iq.points, 75);

    // Modify an entity's component
    let older = Age{years: age.years + 1};
    system.set(forrest, &older);

    // Modify a component in-place with a mutable borrow
    system.borrow_mut::<Iq>(forrest).map(|iq| iq.points += 5);

    // Inspect a component in-place without cloning
    assert_eq!(system.borrow::<Age>(forrest), Some(&Age{years: 23}));

    // Inspect a component via cloning
    assert_eq!(system.get::<Iq>(forrest), Some(Iq{points: 80}));

}

Structs

ComponentIter

Iterator that yields references to ECS components.

ComponentIterMut

Iterator that yields mutable references to ECS components.

Ecs

Primary data structure containing entity and component data.

EntityComponentFilter

Iterator for entity IDs filtered by component.

EntityIdIter

Iterator for entity IDs.

Traits

Component

Marker trait for types which can be used as components.

Type Definitions

EntityId

Value type representing an entry in the entity-component system.