feat(英雄组件): 重构HeroAttrsComp解耦数据与表现层

- 移除HeroAttrsComp中对HeroViewComp的直接依赖,改为脏标签机制
- HeroViewComp改为在update中检查脏标签按需更新UI
- 优化护盾显示逻辑,即使top_node不活跃也更新状态
- 消除双重UI更新,提升性能
This commit is contained in:
panw
2025-12-31 16:07:19 +08:00
parent e1e156fa5b
commit b0b31bf81f
2 changed files with 52 additions and 13 deletions

View File

@@ -102,6 +102,7 @@ export class HeroViewComp extends CCComp {
/* 显示角色血*/
this.top_node.getChildByName("hp").active = true;
this.top_node.getChildByName("mp").active = true;
this.top_node.getChildByName("shield").active = false;
// 初始隐藏血条(被攻击后才显示)
this.top_node.active = true;
@@ -169,14 +170,26 @@ export class HeroViewComp extends CCComp {
/** 显示护盾 */
private show_shield(shield: number = 0, shield_max: number = 0) {
if(!this.top_node.active) return
let shield_progress = shield / shield_max;
this.node.getChildByName("shielded").active = shield > 0;
this.top_node.getChildByName("shield").active = shield > 0;
this.top_node.getChildByName("shield").getComponent(ProgressBar).progress = shield_progress;
this.scheduleOnce(() => {
this.top_node.getChildByName("shield").getChildByName("pb").getComponent(ProgressBar).progress = shield_progress;
}, 0.15);
// ✅ 即使 top_node 不活跃,也要更新护盾的显示状态
// 当护盾为0时隐藏护盾UI
const hasShield = shield > 0 && shield_max > 0;
this.node.getChildByName("shielded").active = hasShield;
// 如果 top_node 活跃,才更新护盾进度条
if (this.top_node) {
this.top_node.getChildByName("shield").active = hasShield;
if (hasShield) {
let shield_progress = shield / shield_max;
this.top_node.getChildByName("shield").getComponent(ProgressBar).progress = shield_progress;
this.scheduleOnce(() => {
if (this.top_node && this.top_node.isValid) {
this.top_node.getChildByName("shield").getChildByName("pb").getComponent(ProgressBar).progress = shield_progress;
}
}, 0.15);
}
}
}
/** 显示血量 */