37 个版本

0.7.0 2024年1月3日
0.6.2 2022年7月3日
0.6.1 2022年2月13日
0.3.21 2021年12月29日

#104游戏开发 分类中

Download history 60/week @ 2024-03-11 68/week @ 2024-03-18 94/week @ 2024-03-25 109/week @ 2024-04-01 36/week @ 2024-04-08 45/week @ 2024-04-15 53/week @ 2024-04-22 79/week @ 2024-04-29 71/week @ 2024-05-06 92/week @ 2024-05-13 84/week @ 2024-05-20 22/week @ 2024-05-27 51/week @ 2024-06-03 51/week @ 2024-06-10 34/week @ 2024-06-17 141/week @ 2024-06-24

每月 277 次下载
用于 14 Crate(其中7个直接使用)

自定义许可

66KB
1.5K SLoC

hecs-schedule

hecs-schedule

hecs-schedule 是一个为 hecs 提供系统抽象的并行执行的框架。

SubWorld

SubWorld 允许将世界分割成更小的部分,这些部分只能访问组件的子集。这允许

Commandbuffer

CommandBuffer 通过组件插入、移除、实体生成和销毁以及后续通过闭包进行任意世界修改(这将稍后执行)的方式提供延迟世界修改。

命令缓冲区扩展了现有的 hecs::CommandBuffer 并提供了更多功能。

系统和调度

系统表示一个可以访问任何资源的单元。为任何函数和闭包以及任何数量的参数(好吧,由于元组大小和编译时间,有一个合理的限制)实现了系统。

系统可以访问子世界并安全地访问声明的组件。它还可以通过 ReadWrite 包装器访问任何其他类型值。

此值将从提供的 Context 中提取,该 Context 作为可变引用传递给 Schedule::execute。这意味着系统可以访问 ECS 外部的局部变量和结构体成员。如果未提供该类型的值,则系统将干净地退出并返回错误。

系统可以返回空值或空结果,这些将正确装箱并传播

调度是一个有序系统执行集合。

当执行调度时,将提供包含系统的引用元组。

用法

use hecs_schedule::*;
use hecs::*;

let mut world = World::default();

#[derive(Debug)]
struct App {
    name: &'static str,
}

let mut app = App {
    name: "hecs-schedule"
};

// Spawn some entities
let a = world.spawn(("a", 42));
world.spawn(("b", 0));
world.spawn(("c", 7));

// Create a simple system to print the entities
let print_system = | w: SubWorld<(& &'static str, &i32)> | {
  w.query::<(&&'static str, &i32)>().iter().for_each(|(e, val)| {
    println!("Entity {:?}: {:?}", e, val);
  })
};

// Get a component from a specific entity, failing gracefully if the entity
// didn't exist or the subworld did not support the component. The result
// will propogate to the schedule execution.
let get_system = move | w: SubWorld<&i32> | -> anyhow::Result<()> {
  let val = w.get::<i32>(a)?;

  // Prints the answer to life, the universe, and everything.
  // Welp, maybe not how to please the borrow checker, but almost
  // everything.
  println!("Got: {}", *val);

  Ok(())
};

// Declare a system which borrows the app and prints it.
// This requires that a reference to app was provided to execute.
// Otherwise, the system fails and returns an error, which propogates to the
// schedule and stops execution.

// It is also possible to modify the app via `mut Write<App>`
let print_app = |app: Read<App>| {
    println!("App: {:?}", app);
};

// Note: the `hecs_schedule::CommandBuffer` is a superset of `hecs::CommandBuffer` and is
// accesible as a shared resource from systems.
let spawn_system = |mut cmd: Write<hecs_schedule::CommandBuffer>| {
    cmd.spawn(("c", 5));
};

// Construct a schedule
let mut schedule = Schedule::builder()
    .add_system(spawn_system)
    .add_system(print_system)
    .add_system(print_app)
    .add_system(get_system)
    .build();

// Execute the schedule's systems and provide the world and app. This will parallelize as much
// as possible.
schedule.execute((&mut world, &mut app)).expect("Failed to execute schedule");

依赖项

~2.1–3MB
~56K SLoC