#ecs #entity #component #system

recs

Simple, flexible, macro-free entity-component system in pure (stable!) Rust

4 stable releases

Uses old Rust 2015

2.0.1 Mar 6, 2016
1.1.0 Jun 2, 2015
1.0.1 May 23, 2015

#2242 in Game dev

Download history 3/week @ 2024-07-29 1/week @ 2024-08-12 6/week @ 2024-08-26 10/week @ 2024-09-23 6/week @ 2024-09-30

63 downloads per month

MIT license

15KB
165 lines

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.)

No runtime deps