recs

Crates.iorecs
lib.rsrecs
version2.0.1
sourcesrc
created_at2015-05-23 08:53:47.316217
updated_at2016-03-06 12:02:14.972482
descriptionSimple, flexible, macro-free entity-component system in pure (stable!) Rust.
homepage
repositoryhttps://github.com/andybarron/rustic-ecs
max_upload_size
id2176
size19,208
mio-core (github:tokio-rs:mio-core)

documentation

https://andybarron.github.io/rustic-ecs

README

Rustic Entity-Component System

Simple entity-component system in pure Rust. Type reflection - no macros!

Build Status

Install

Visit the crates.io page, and add the specified line ("recs = ...") to the [dependencies] section of your Cargo.toml. From then on, cargo build should automatically download and compile Rustic ECS.

Documentation

https://andybarron.github.io/rustic-ecs

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
    // The Ecs.set method returns an EcsResult that will be set to Err if
    // the specified entity does not exist. If you're sure that the entity exists, suppress
    // Rust's "unused result" warning by prefixing your calls to set(..) with "let _ = ..."
    let _ = system.set(forrest, Age{years: 22});
    let _ = 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};
    let _ = 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), Ok(&Age{years: 23}));

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

}

License

MIT. Hooray!

(See LICENSE.txt for details.)

Commit count: 16

cargo fmt