5个版本 (3个重大更新)

0.4.1 2020年2月12日
0.4.0 2019年1月4日
0.3.0 2018年10月28日
0.2.0 2018年4月22日
0.1.0 2017年7月22日

#974 in 进程宏

Download history 1444/week @ 2024-03-30 1085/week @ 2024-04-06 1424/week @ 2024-04-13 1351/week @ 2024-04-20 1142/week @ 2024-04-27 1265/week @ 2024-05-04 1167/week @ 2024-05-11 1352/week @ 2024-05-18 1356/week @ 2024-05-25 1575/week @ 2024-06-01 864/week @ 2024-06-08 1395/week @ 2024-06-15 1419/week @ 2024-06-22 761/week @ 2024-06-29 752/week @ 2024-07-06 1322/week @ 2024-07-13

每月下载量 4,476
55 个crate中使用(其中13个直接使用)

MIT/Apache

20KB
375

Specs

Specs Parallel Entity-Component System

Build Status Crates.io Gitter MIT/Apache Docs.rs

Specs是用Rust编写的实体-组件系统。与其他大多数ECS库不同,它提供

  • 易于实现并行处理
  • 高度灵活性
    • 包含5种不同的组件存储,可由用户扩展
    • 其类型大多不耦合,因此您可以轻松编写部分代码,同时仍然使用Specs
    • System可以从组件和资源中读取和写入,可以相互依赖,并且您可以使用屏障来强制执行系统执行的多个阶段
  • 针对实际应用的极高性能

最低Rust版本:1.70

示例

use specs::prelude::*;

// A component contains data
// which is associated with an entity.
#[derive(Debug)]
struct Vel(f32);

impl Component for Vel {
    type Storage = VecStorage<Self>;
}

#[derive(Debug)]
struct Pos(f32);

impl Component for Pos {
    type Storage = VecStorage<Self>;
}

struct SysA;

impl<'a> System<'a> for SysA {
    // These are the resources required for execution.
    // You can also define a struct and `#[derive(SystemData)]`,
    // see the `full` example.
    type SystemData = (WriteStorage<'a, Pos>, ReadStorage<'a, Vel>);

    fn run(&mut self, (mut pos, vel): Self::SystemData) {
        // The `.join()` combines multiple component storages,
        // so we get access to all entities which have
        // both a position and a velocity.
        for (pos, vel) in (&mut pos, &vel).join() {
            pos.0 += vel.0;
        }
    }
}

fn main() {
    // The `World` is our
    // container for components
    // and other resources.
    let mut world = World::new();
    world.register::<Pos>();
    world.register::<Vel>();

    // An entity may or may not contain some component.

    world.create_entity().with(Vel(2.0)).with(Pos(0.0)).build();
    world.create_entity().with(Vel(4.0)).with(Pos(1.6)).build();
    world.create_entity().with(Vel(1.5)).with(Pos(5.4)).build();

    // This entity does not have `Vel`, so it won't be dispatched.
    world.create_entity().with(Pos(2.0)).build();

    // This builds a dispatcher.
    // The third parameter of `with` specifies
    // logical dependencies on other systems.
    // Since we only have one, we don't depend on anything.
    // See the `full` example for dependencies.
    let mut dispatcher = DispatcherBuilder::new().with(SysA, "sys_a", &[]).build();
    // This will call the `setup` function of every system.
    // In this example this has no effect since we already registered our components.
    dispatcher.setup(&mut world);

    // This dispatches all the systems in parallel (but blocking).
    dispatcher.dispatch(&mut world);
}

请查看示例目录以获取更多信息。

公共依赖

crate 版本
hibitset hibitset
rayon rayon
shred shred
shrev shrev

贡献

非常欢迎贡献!如果您之前没有贡献过,请筛选带有“简单”或“良好入门问题”标签的问题。请注意,您的贡献假定根据Apache-2.0/MIT双重许可。


lib.rs:

实现了 #[derive(Component)]#[derive(Saveload)] 宏和 #[component] 属性,用于 Specs

依赖项

~1.5MB
~35K SLoC