5个版本
0.1.3 | 2023年10月7日 |
---|---|
0.1.2 | 2023年10月7日 |
0.1.1 | 2023年10月7日 |
0.1.0 | 2023年10月7日 |
0.0.0 | 2023年10月7日 |
#163 在 内存管理
13KB
169 行
Plushy
Plushy是一个为任意类型提供舒适代际竞技场的库。你可以把它想象成 thunderdome
对所有类型的统一,或者是一个你一次只能有一个组件的ECS。
Plushy是一个相对简单的crate,它基于thunderdome构建,但增加了一些不错的东西,特别是
- 围绕
Index
的强类型包装,这意味着你不可能错误地混淆你的实体ID。如果你插入一个Player
,相应的ID是Id<Player>
。 - 你只需要一个
Store
来存储所有类型。使用thunderdome
,你需要为每个你想要存储的类型T
创建一个单独的Arena<T>
。
let mut store = Store::new();
struct Enemy {
pub x: i32,
}
struct Player {
pub health: f32,
}
// New entities can just be spawned, we don't need to register
// the types anywhere.
store.spawn(Enemy { x: 1 });
store.spawn(Enemy { x: 2 });
// Store the player's ID for later
let player = store.spawn(Player { health: 100.0 });
assert_eq!(
&[1, 2],
store
.iter::<Enemy>()
.map(|t| t.1.x)
.collect::<Vec<_>>()
.as_slice()
);
// Fetch the player based on the ID. Note we don't need to write
// `store.get::<Player>(player)`, the type is inferred from the
// strongly typed ID.
assert_eq!(100.0, store.get(player.clone()).unwrap().health);
// Change player health
store.get_mut(player).unwrap().health = 200.0;
// Fetch it again and verify the change.
assert_eq!(200.0, store.get(player).unwrap().health);
许可证
Plushy是免费和开源的,并且根据MIT和Apache 2.0许可证双授权。
依赖关系
~57KB