Loading...
Loading...
Compare original and translation side by side
MeshMaterial3d<T>Handle<T>commands.trigger()add_observer()references/bevy_specific_tips.mdMeshMaterial3d<T>Handle<T>commands.trigger()add_observer()references/bevy_specific_tips.md~/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy-0.17.1/examples~/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy-0.17.1/examplespub struct CombatPlugin;
impl Plugin for CombatPlugin {
fn build(&self, app: &mut App) {
app
.add_event::<DamageEvent>()
.add_systems(Update, (process_damage, check_death));
}
}references/bevy_specific_tips.mdpub struct CombatPlugin;
impl Plugin for CombatPlugin {
fn build(&self, app: &mut App) {
app
.add_event::<DamageEvent>()
.add_systems(Update, (process_damage, check_death));
}
}references/bevy_specific_tips.mdreferences/bevy_specific_tips.mdreferences/bevy_specific_tips.md// ✅ GOOD: Small, focused components
#[derive(Component)]
pub struct Health { pub current: f32, pub max: f32 }
#[derive(Component)]
pub struct Armor { pub defense: f32 }
// ❌ BAD: Monolithic component
#[derive(Component)]
pub struct CombatStats {
pub health: f32,
pub armor: f32,
pub strength: f32,
// ... wastes memory for entities that only have some stats
}impl Health {
pub fn is_alive(&self) -> bool {
self.current > 0.0
}
pub fn percentage(&self) -> f32 {
self.current / self.max
}
}references/ecs_patterns.md// ✅ 推荐:小巧、聚焦的组件
#[derive(Component)]
pub struct Health { pub current: f32, pub max: f32 }
#[derive(Component)]
pub struct Armor { pub defense: f32 }
// ❌ 不推荐:单体式组件
#[derive(Component)]
pub struct CombatStats {
pub health: f32,
pub armor: f32,
pub strength: f32,
// ... 对只拥有部分属性的实体来说是内存浪费
}impl Health {
pub fn is_alive(&self) -> bool {
self.current > 0.0
}
pub fn percentage(&self) -> f32 {
self.current / self.max
}
}references/ecs_patterns.md.add_systems(
Update,
(
// 1. Input processing
handle_input,
// 2. State changes
process_events,
update_state,
// 3. Derive properties from state
calculate_derived_values,
// 4. Visual updates
update_materials,
update_animations,
// 5. UI updates (must run last)
update_ui_displays,
),
)// Only process entities where Health changed
pub fn update_health_bar(
query: Query<(&Health, &mut HealthBar), Changed<Health>>,
) {
for (health, mut bar) in query.iter_mut() {
bar.width = health.percentage() * 100.0;
}
}references/ecs_patterns.md.add_systems(
Update,
(
// 1. 输入处理
handle_input,
// 2. 状态变更
process_events,
update_state,
// 3. 从状态派生属性
calculate_derived_values,
// 4. 视觉更新
update_materials,
update_animations,
// 5. UI更新(必须最后运行)
update_ui_displays,
),
)// 仅处理Health组件发生变化的实体
pub fn update_health_bar(
query: Query<(&Health, &mut HealthBar), Changed<Health>>,
) {
for (health, mut bar) in query.iter_mut() {
bar.width = health.percentage() * 100.0;
}
}references/ecs_patterns.mdcargo build --features bevy/dynamic_linkingcargo checkcargo build --releasecargo build --features bevy/dynamic_linkingcargo checkcargo build --releasecargo cleanreferences/bevy_specific_tips.mdcargo cleanreferences/bevy_specific_tips.mdcargo checkcargo checkcargo build --features bevy/dynamic_linkingcargo checkcargo checkcargo build --features bevy/dynamic_linking#[derive(Component)]
pub struct HealthBar;
#[derive(Component)]
pub struct ScoreDisplay;pub fn setup_ui(mut commands: Commands) {
commands.spawn((
HealthBar,
Node {
position_type: PositionType::Absolute,
left: Val::Px(10.0),
top: Val::Px(10.0),
width: Val::Px(200.0),
height: Val::Px(20.0),
..default()
},
BackgroundColor(Color::srgba(0.8, 0.2, 0.2, 0.9)),
));
}pub fn update_health_ui(
health: Query<&Health, With<Player>>,
mut ui: Query<&mut Node, With<HealthBar>>,
) {
if let (Ok(health), Ok(mut node)) = (health.get_single(), ui.get_single_mut()) {
node.width = Val::Px(health.percentage() * 200.0);
}
}references/ui_development.md#[derive(Component)]
pub struct HealthBar;
#[derive(Component)]
pub struct ScoreDisplay;pub fn setup_ui(mut commands: Commands) {
commands.spawn((
HealthBar,
Node {
position_type: PositionType::Absolute,
left: Val::Px(10.0),
top: Val::Px(10.0),
width: Val::Px(200.0),
height: Val::Px(20.0),
..default()
},
BackgroundColor(Color::srgba(0.8, 0.2, 0.2, 0.9)),
));
}pub fn update_health_ui(
health: Query<&Health, With<Player>>,
mut ui: Query<&mut Node, With<HealthBar>>,
) {
if let (Ok(health), Ok(mut node)) = (health.get_single(), ui.get_single_mut()) {
node.width = Val::Px(health.percentage() * 200.0);
}
}references/ui_development.md1. Plan → 2. Implement → 3. Build → 4. Test → 5. Refine
↑ ↓
←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←1. 规划 → 2. 实现 → 3. 构建 → 4. 测试 → 5. 优化
↑ ↓
←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←Query<&Component, Changed<Component>>Query<&A, (With<B>, Without<C>)>Query<&Component, Changed<Component>>Query<&A, (With<B>, Without<C>)>references/common_pitfalls.mdmain.rsget_many_mutChanged<T>if let Ok()references/common_pitfalls.mdreferences/common_pitfalls.mdmain.rsget_many_mutChanged<T>if let Ok()references/common_pitfalls.mdGoal: [One sentence describing end state]
Current State: [What exists now]
Requirements: [Numbered list of what to build]
Implementation Steps: [Suggested approach]
Success Criteria: [How to verify it works]
Notes: [Important context, edge cases, design principles]Implement Health System
Goal: Implement a health system with damage, healing, and death mechanics.
Current State:
- Player entity exists
- No health tracking yet
Requirements:
1. Create Health component with current/max values
2. Create DamageEvent for dealing damage
3. Create system to process damage events
4. Add death detection when health reaches 0
5. Add visual health bar UI
Implementation Steps:
1. Create Health component in src/components/properties.rs
2. Create DamageEvent in src/events.rs
3. Create process_damage system in src/systems/combat.rs
4. Create check_death system
5. Create health bar UI in src/systems/ui/health_bar.rs
6. Register all systems in main.rs in correct order
Success Criteria:
- Player spawns with Health component
- Damage events reduce health
- Health bar updates when health changes
- Entity despawns when health reaches 0
- Code compiles without errors目标:[描述最终状态的一句话]
当前状态:[现有内容]
需求:[待构建内容的编号列表]
实现步骤:[建议的方法]
成功标准:[验证功能正常的方式]
注意事项:[重要上下文、边缘情况、设计原则]实现生命值系统
目标:实现包含伤害、治疗和死亡机制的生命值系统。
当前状态:
- 玩家实体已存在
- 尚未实现生命值追踪
需求:
1. 创建包含当前值/最大值的Health组件
2. 创建用于造成伤害的DamageEvent
3. 创建处理伤害事件的系统
4. 添加生命值归0时的死亡检测
5. 添加视觉化生命值条UI
实现步骤:
1. 在src/components/properties.rs中创建Health组件
2. 在src/events.rs中创建DamageEvent
3. 在src/systems/combat.rs中创建process_damage系统
4. 创建check_death系统
5. 在src/systems/ui/health_bar.rs中创建生命值条UI
6. 在main.rs中按正确顺序注册所有系统
成功标准:
- 玩家生成时带有Health组件
- 伤害事件会减少生命值
- 生命值变化时生命值条会更新
- 生命值归0时实体被销毁
- 代码编译无错误references/project_structure.mdreferences/project_structure.mdreferences/bevy_specific_tips.mdreferences/ecs_patterns.mdreferences/ui_development.mdreferences/common_pitfalls.mdreferences/project_structure.mdreferences/bevy_specific_tips.mdreferences/ecs_patterns.mdreferences/ui_development.mdreferences/common_pitfalls.mdreferences/project_structure.md~/.cargo/registry/...