refactor(英雄视图): 优化血条显示逻辑

将基于持续时间的血条显示机制改为基于最后更新时间
初始隐藏血条,仅在属性更新时显示
添加2秒无更新自动隐藏功能
This commit is contained in:
walkpan
2026-01-02 15:50:12 +08:00
parent b99f75b1b9
commit ebd67472c7

View File

@@ -49,8 +49,7 @@ export class HeroViewComp extends CCComp {
deadCD:number=0
monDeadTime:number=0.5
// 血条显示相关
hpBarShowTime:number = 5; // 血条显示持续时间(秒)
hpBarShowCD:number = 0; // 血条显示计时器
lastBarUpdateTime:number = 0; // 最后一次血条/蓝条/护盾更新时间
// ==================== UI 节点引用 ====================
private top_node: Node = null!;
@@ -97,16 +96,14 @@ export class HeroViewComp extends CCComp {
/** 方向 */
this.node.setScale(this.scale*this.node.scale.x,1*this.node.scale.y);
this.top_node.setScale(this.scale*this.top_node.scale.x,1*this.top_node.scale.y);
// if(this.model && this.model.is_boss){
// this.top_node.position=v3(this.node.position.x,this.node.position.y+70,0)
// }
/* 显示角色血*/
this.top_node.getChildByName("hp").active = true;
// 🔥 怪物不显示蓝条
this.top_node.getChildByName("mp").active = this.model.fac === FacSet.HERO;
this.top_node.getChildByName("shield").active = false;
// 初始隐藏血条(被攻击后才显示)
this.top_node.active = true;
// 初始隐藏血条(有更新时才显示)
this.top_node.active = false;
}
@@ -139,13 +136,12 @@ export class HeroViewComp extends CCComp {
return
} ;
// 处理血条显示计时
if (this.hpBarShowCD > 0) {
this.hpBarShowCD -= dt;
if (this.hpBarShowCD <= 0) {
// 时间到,隐藏血条
// 处理血条显示计时2秒无更新则隐藏
if (this.lastBarUpdateTime > 0) {
const timeSinceLastUpdate = Date.now() / 1000 - this.lastBarUpdateTime;
if (timeSinceLastUpdate >= 2) {
this.top_node.active = false;
this.hpBarShowCD = 0;
this.lastBarUpdateTime = 0;
}
}
@@ -172,6 +168,7 @@ export class HeroViewComp extends CCComp {
/** 显示护盾 */
private show_shield(shield: number = 0, shield_max: number = 0) {
this.lastBarUpdateTime = Date.now() / 1000;
if(!this.top_node.active) return
let shield_progress = shield / shield_max;
this.node.getChildByName("shielded").active = shield > 0;
@@ -184,6 +181,7 @@ export class HeroViewComp extends CCComp {
/** 显示血量 */
private hp_show() {
this.lastBarUpdateTime = Date.now() / 1000;
// 不再基于血量是否满来决定显示状态,只更新进度条
let hp=this.model.hp;
let hp_max=this.model.Attrs[Attrs.HP_MAX];
@@ -196,6 +194,7 @@ export class HeroViewComp extends CCComp {
/** 显示魔法值 */
private mp_show() {
this.lastBarUpdateTime = Date.now() / 1000;
if(!this.top_node.active) return
let mp=this.model.mp;
let mp_max=this.model.Attrs[Attrs.MP_MAX];
@@ -324,6 +323,7 @@ export class HeroViewComp extends CCComp {
this.heathed();
this.hp_tip(TooltipTypes.health, hp.toFixed(0));
this.top_node.active = true;
this.lastBarUpdateTime = Date.now() / 1000;
// ❌ 移除UI更新由脏标签机制处理
// this.hp_show();
@@ -333,6 +333,7 @@ export class HeroViewComp extends CCComp {
// ✅ 仅显示提示,不调用 mp_show()
this.hp_tip(TooltipTypes.addmp, mp.toFixed(0));
this.top_node.active = true;
this.lastBarUpdateTime = Date.now() / 1000;
// ❌ 移除UI更新由脏标签机制处理
// this.mp_show();
@@ -344,7 +345,7 @@ export class HeroViewComp extends CCComp {
this.status_change("idle");
this.model.hp =this.model.Attrs[Attrs.HP_MAX]*50/100;
this.top_node.active=false
this.hpBarShowCD=0
this.lastBarUpdateTime=0
}
/**
@@ -400,9 +401,9 @@ export class HeroViewComp extends CCComp {
}
do_atked(damage:number,isCrit:boolean,s_uuid:number,isBack:boolean=false){
// 受到攻击时显示血条,并设置显示时间即使伤害为0也显示
// 受到攻击时显示血条,并更新最后更新时间
this.top_node.active = true;
this.hpBarShowCD = this.hpBarShowTime;
this.lastBarUpdateTime = Date.now() / 1000;
if (damage <= 0) return;
@@ -512,7 +513,7 @@ export class HeroViewComp extends CCComp {
collider.off(Contact2DType.BEGIN_CONTACT);
}
this.deadCD=0
this.hpBarShowCD=0
this.lastBarUpdateTime=0
// 清理伤害队列
this.damageQueue.length = 0;