关于 bevy-ecs-patterns
面向使用 Bevy 的游戏开发者,提供 ECS 高级模式与实践,涵盖查询、调度、变更检测和性能优化。
主要功能
- 01复杂查询与过滤器
- 02查询状态与缓存
- 03实体关系与层级
- 04系统调度与依赖排序
- 05变更检测与响应式系统
- 06并行迭代与 ParamSet
适用场景
- 01优化 ECS 查询性能
- 02设计复杂实体层级
- 03配置系统顺序与运行条件
- 04排查 Archetype 碎片问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ laurigates/ claude-plugins --skill bevy-ecs-patterns安装后在 Claude Code 中输入/bevy-ecs-patterns确认已加载。
附带文件REFERENCE.md
详细介绍
Bevy ECS 模式
Bevy 实体组件系统的高级模式与技巧,重点关注性能、可维护性和复杂的游戏架构。
何时使用此技能
| 在以下情况下使用此技能…… | 在以下情况下改用 bevy-game-engine…… |
|---|---|
| 优化 ECS 查询性能 | 从头开始设置新的 Bevy 项目 |
| 实现复杂的实体关系或层级结构 | 学习基础 ECS 概念(组件、系统、资源) |
| 设计系统调度和顺序 | 处理输入、资源或游戏状态 |
使用变更检测(Changed<T>、Added<T>) | 编写基础游戏逻辑或应用结构 |
使用 ParamSet、并行迭代或批量操作 | 配置渲染、UI 或音频 |
| 调试原型碎片化或存储策略 | 添加插件或常用 Bevy 依赖项 |
核心专长
高级查询
- 复杂的查询筛选条件及其组合
- 查询状态与缓存
- 实体关系与层级结构
- 并行迭代策略
系统调度
- 系统顺序与依赖关系
- 运行条件与门控
- 系统集与调度
- 固定时间步系统
变更检测
Changed<T>和Added<T>筛选器- 使用
Ref<T>进行变更跟踪 - 高效的响应式系统
高级查询模式
查询筛选器
use bevy::prelude::*;
// Multiple filters
fn targeting_system(
query: Query<
(&Transform, &mut Target),
(With<Enemy>, Without<Dead>, Changed<Health>)
>,
) {
for (transform, mut target) in &query {
// Only enemies that are alive and recently damaged
}
}
// Optional components
fn flexible_system(
query: Query<(&Transform, Option<&Velocity>, Option<&Acceleration>)>,
) {
for (transform, velocity, acceleration) in &query {
if let Some(vel) = velocity {
// Has velocity
}
}
}
// Or filters
fn pickup_system(
query: Query<Entity, Or<(With<HealthPickup>, With<AmmoPickup>)>>,
) {
for entity in &query {
// Process any pickup type
}
}
存在冲突的多个查询
// Use ParamSet when queries have conflicting access
fn combat_system(
mut param_set: ParamSet<(
Query<&mut Health, With<Player>>,
Query<&mut Health, With<Enemy>>,
)>,
) {
// Access one query at a time
for mut health in param_set.p0().iter_mut() {
health.0 += 1.0; // Heal player
}
for mut health in param_set.p1().iter_mut() {
health.0 -= 10.0; // Damage enemies
}
}
// Query transmutation for flexible access
fn dynamic_query(
all_entities: Query<(Entity, &Transform)>,
players: Query<&Player>,
) {
for (entity, transform) in &all_entities {
if players.get(entity).is_ok() {
// This is a player
}
}
}
实体关系
// Parent-child hierarchies
#[derive(Component)]
struct Inventory;
#[derive(Component)]
struct InventorySlot(usize);
fn spawn_inventory(mut commands: Commands) {
commands.spawn((Inventory, SpatialBundle::default()))
.with_children(|parent| {
for i in 0..10 {
parent.spawn((InventorySlot(i), SpriteBundle::default()));
}
});
}
// Query parent from child
fn child_system(
children: Query<(&InventorySlot, &Parent)>,
parents: Query<&Inventory>,
) {
for (slot, parent) in &children {
if let Ok(inventory) = parents.get(parent.get()) {
// Access parent inventory
}
}
}
// Query children from parent
fn parent_system(
parents: Query<&Children, With<Inventory>>,
slots: Query<&InventorySlot>,
) {
for children in &parents {
for &child in children.iter() {
if let Ok(slot) = slots.get(child) {
// Access child slot
}
}
}
}
Agent 优化
| 场景 | 命令 |
|---|---|
| 快速编译检查 | cargo check 2>&1 | head -30 |
| 快速运行测试 | cargo test --lib -- --test-threads=1 -q |
| 检查 ECS 用法错误 | cargo clippy -- -W clippy::all 2>&1 | head -50 |
| 列出项目中的组件 | grep -rn "derive(Component)" src/ --include="*.rs" |
| 列出项目中的系统 | grep -rn "fn.*Query<" src/ --include="*.rs" |
| 查找系统顺序设置 | grep -rn "\.add_systems|\.chain()|\.after(|\.before(" src/ --include="*.rs" |
| 检查 ParamSet 用法 | grep -rn "ParamSet" src/ --include="*.rs" |
有关系统调度、变更检测、性能模式、自定义实体命令和设计最佳实践,请参阅 REFERENCE.md。
评论
支持 Markdown