#ecs #entity #gamedev #muge

无std muds

极简数据结构和实体-组件-系统库

6个版本

0.1.5 2022年5月9日
0.1.4 2022年4月27日

#1010 in 游戏开发

每月21次下载

MIT 许可证

165KB
3.5K SLoC

〰ᗰᑌᗪᔕ〰

极简数据结构和实体-组件-系统库


License: MIT Crates.io Docs.rs

概述

muds 是一个用于高性能实时应用(如游戏)的数据导向设计极简数据结构库。它提供了一个以代际索引为键的集合库,以及一个基于该库构建的 代际索引实体-组件-系统(ECS)库。

亮点

  • 灵活且可扩展的代际索引ECS模型
    • 支持类似关系数据库的多个实体类型,具有类型安全的实体ID
    • 实体和组件的自定义存储类型
  • 跨平台;兼容 no_std 和 WASM 构建。默认使用 f64 代际索引与 WASM 中的 JS 互操作。
  • 最小化大小,最小化依赖,良好性能
  • 库,不是框架。与其他带有 System 调度器/派发器的 ECS 不同,muds 不会控制你的程序流程。
  • 使用 cons 以优雅的方式执行类型级递归,而不是像其他 ECS 库那样使用宏实现固定长度元组的实现。

安装

[dependencies]
muds = "0.1"

特性

  • std - 启用 std 支持。默认启用。
  • serde - 启用集合和索引的 serde 序列化/反序列化实现
  • derive - 启用 #[derive(Entity)]#[derive(Component)]
  • index-u64 - 使用 IndexU64 作为 ECS 的代际索引类型,而不是默认的 IndexF64

文档

请参阅 Docs.rs: https://docs.rs/muds

使用方法

以下是对ECS库的示例用法。更多示例请参阅bench

use muds::prelude::*;
use muds::ecs::storage::SparseSetStorage;

// 1. Defines the entity and components.

#[derive(Entity, Debug)]
struct Ent;

#[derive(Component, Debug)]
struct Pos(u32, u32);

// `#[storage(S)]` can be used to customize the entity / component storage type.
#[derive(Component, Debug)]
#[storage(SparseSetStorage)]
struct Vel(u32, u32);

// 2. Registers the entity-components archetype to registry.
let mut registry = Registry::default();
registry.register_archetype::<Ent, Cons!(Pos, Vel)>();

// 3. Insert entities/components to mut storage.
// registry.storage returns cons of collection types.
// Each entity/component storage can be retrieved either as immutable (&C) or mutable (&mut C).
{
  let cons!(mut ent, mut pos, mut vel) = registry.storage::<&mut Ent, Cons!(&mut Pos, &mut Vel)>();
  for i in 0..10 {
      let eid = ent.insert(E);
      pos.insert(eid, Pos(i * 2, i * 2 + 1));
      vel.insert(eid, Vel(i, i + 1));
  }
}

// 4. Storages are just standard Map types that can be iterated.
// Use MapJoin trait to jointly iterate components as cons.
{
  let cons!(_ent, mut pos, vel) = registry.storage::<&Ent, Cons!(&mut Pos, &Vel)>();
  for cons!(_eid, v, p) in pos.iter_mut().cons().map_join(&*vel) {
      p.0 += v.0;
      p.1 += v.1;
  }
}

许可证

本存储库及其内部代码受MIT许可证的许可。更多信息请参阅LICENSE

依赖关系

~390–630KB
~13K SLoC