#ecs #entity #component #system

recs

纯Rust(稳定!)中的简单、灵活、无宏的实体-组件系统

4个稳定版本

使用旧的Rust 2015

2.0.1 2016年3月6日
1.1.0 2015年6月2日
1.0.1 2015年5月23日

#1392 in 游戏开发

每月32次下载

MIT许可证

15KB
165

Rustic实体-组件系统

纯Rust中的简单实体-组件系统。类型反射 - 无宏!

Build Status

安装

访问crates.io页面,并将指定的行("recs = ...")添加到你的[dependencies]部分。从那时起,运行cargo build应该会自动下载并编译Rustic ECS。

文档

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

示例

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}));

}

许可证

MIT。太好了!

(有关详细信息,请参阅LICENSE.txt)

无运行时依赖