43 个版本

0.20.0 2023 年 9 月 25 日
0.19.0 2023 年 6 月 10 日
0.18.0 2022 年 7 月 5 日
0.17.0 2021 年 6 月 7 日
0.7.0 2016 年 7 月 8 日

#392 in 游戏开发

Download history 2820/week @ 2024-04-12 2828/week @ 2024-04-19 2697/week @ 2024-04-26 2696/week @ 2024-05-03 2735/week @ 2024-05-10 2612/week @ 2024-05-17 2790/week @ 2024-05-24 3020/week @ 2024-05-31 1991/week @ 2024-06-07 2527/week @ 2024-06-14 2763/week @ 2024-06-21 1617/week @ 2024-06-28 1250/week @ 2024-07-05 2569/week @ 2024-07-12 2539/week @ 2024-07-19 2050/week @ 2024-07-26

8,524 每月下载量
不到 50 包中使用

MIT/Apache

330KB
5.5K SLoC

Specs

Specs Parallel ECS

Build Status Crates.io Gitter MIT/Apache Docs.rs Code coverage LoC

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

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

最低 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

贡献

非常欢迎贡献!如果您之前没有贡献过,请筛选带有 "easy" 或 "good first issue" 标签的问题。请注意,您的贡献默认受 Apache-2.0/MIT 许可的双重许可。

依赖项

~2.9–4MB
~83K SLoC