refactor(英雄视图): 优化血条显示逻辑
将基于持续时间的血条显示机制改为基于最后更新时间 初始隐藏血条,仅在属性更新时显示 添加2秒无更新自动隐藏功能
This commit is contained in:
@@ -49,8 +49,7 @@ export class HeroViewComp extends CCComp {
|
|||||||
deadCD:number=0
|
deadCD:number=0
|
||||||
monDeadTime:number=0.5
|
monDeadTime:number=0.5
|
||||||
// 血条显示相关
|
// 血条显示相关
|
||||||
hpBarShowTime:number = 5; // 血条显示持续时间(秒)
|
lastBarUpdateTime:number = 0; // 最后一次血条/蓝条/护盾更新时间
|
||||||
hpBarShowCD:number = 0; // 血条显示计时器
|
|
||||||
// ==================== UI 节点引用 ====================
|
// ==================== UI 节点引用 ====================
|
||||||
private top_node: Node = null!;
|
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.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);
|
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("hp").active = true;
|
||||||
// 🔥 怪物不显示蓝条
|
// 🔥 怪物不显示蓝条
|
||||||
this.top_node.getChildByName("mp").active = this.model.fac === FacSet.HERO;
|
this.top_node.getChildByName("mp").active = this.model.fac === FacSet.HERO;
|
||||||
this.top_node.getChildByName("shield").active = false;
|
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
|
return
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
// 处理血条显示计时
|
// 处理血条显示计时(2秒无更新则隐藏)
|
||||||
if (this.hpBarShowCD > 0) {
|
if (this.lastBarUpdateTime > 0) {
|
||||||
this.hpBarShowCD -= dt;
|
const timeSinceLastUpdate = Date.now() / 1000 - this.lastBarUpdateTime;
|
||||||
if (this.hpBarShowCD <= 0) {
|
if (timeSinceLastUpdate >= 2) {
|
||||||
// 时间到,隐藏血条
|
|
||||||
this.top_node.active = false;
|
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) {
|
private show_shield(shield: number = 0, shield_max: number = 0) {
|
||||||
|
this.lastBarUpdateTime = Date.now() / 1000;
|
||||||
if(!this.top_node.active) return
|
if(!this.top_node.active) return
|
||||||
let shield_progress = shield / shield_max;
|
let shield_progress = shield / shield_max;
|
||||||
this.node.getChildByName("shielded").active = shield > 0;
|
this.node.getChildByName("shielded").active = shield > 0;
|
||||||
@@ -184,6 +181,7 @@ export class HeroViewComp extends CCComp {
|
|||||||
|
|
||||||
/** 显示血量 */
|
/** 显示血量 */
|
||||||
private hp_show() {
|
private hp_show() {
|
||||||
|
this.lastBarUpdateTime = Date.now() / 1000;
|
||||||
// 不再基于血量是否满来决定显示状态,只更新进度条
|
// 不再基于血量是否满来决定显示状态,只更新进度条
|
||||||
let hp=this.model.hp;
|
let hp=this.model.hp;
|
||||||
let hp_max=this.model.Attrs[Attrs.HP_MAX];
|
let hp_max=this.model.Attrs[Attrs.HP_MAX];
|
||||||
@@ -196,6 +194,7 @@ export class HeroViewComp extends CCComp {
|
|||||||
|
|
||||||
/** 显示魔法值 */
|
/** 显示魔法值 */
|
||||||
private mp_show() {
|
private mp_show() {
|
||||||
|
this.lastBarUpdateTime = Date.now() / 1000;
|
||||||
if(!this.top_node.active) return
|
if(!this.top_node.active) return
|
||||||
let mp=this.model.mp;
|
let mp=this.model.mp;
|
||||||
let mp_max=this.model.Attrs[Attrs.MP_MAX];
|
let mp_max=this.model.Attrs[Attrs.MP_MAX];
|
||||||
@@ -324,6 +323,7 @@ export class HeroViewComp extends CCComp {
|
|||||||
this.heathed();
|
this.heathed();
|
||||||
this.hp_tip(TooltipTypes.health, hp.toFixed(0));
|
this.hp_tip(TooltipTypes.health, hp.toFixed(0));
|
||||||
this.top_node.active = true;
|
this.top_node.active = true;
|
||||||
|
this.lastBarUpdateTime = Date.now() / 1000;
|
||||||
|
|
||||||
// ❌ 移除:UI更新由脏标签机制处理
|
// ❌ 移除:UI更新由脏标签机制处理
|
||||||
// this.hp_show();
|
// this.hp_show();
|
||||||
@@ -333,6 +333,7 @@ export class HeroViewComp extends CCComp {
|
|||||||
// ✅ 仅显示提示,不调用 mp_show()
|
// ✅ 仅显示提示,不调用 mp_show()
|
||||||
this.hp_tip(TooltipTypes.addmp, mp.toFixed(0));
|
this.hp_tip(TooltipTypes.addmp, mp.toFixed(0));
|
||||||
this.top_node.active = true;
|
this.top_node.active = true;
|
||||||
|
this.lastBarUpdateTime = Date.now() / 1000;
|
||||||
|
|
||||||
// ❌ 移除:UI更新由脏标签机制处理
|
// ❌ 移除:UI更新由脏标签机制处理
|
||||||
// this.mp_show();
|
// this.mp_show();
|
||||||
@@ -344,7 +345,7 @@ export class HeroViewComp extends CCComp {
|
|||||||
this.status_change("idle");
|
this.status_change("idle");
|
||||||
this.model.hp =this.model.Attrs[Attrs.HP_MAX]*50/100;
|
this.model.hp =this.model.Attrs[Attrs.HP_MAX]*50/100;
|
||||||
this.top_node.active=false
|
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){
|
do_atked(damage:number,isCrit:boolean,s_uuid:number,isBack:boolean=false){
|
||||||
// 受到攻击时显示血条,并设置显示时间(即使伤害为0也显示)
|
// 受到攻击时显示血条,并更新最后更新时间
|
||||||
this.top_node.active = true;
|
this.top_node.active = true;
|
||||||
this.hpBarShowCD = this.hpBarShowTime;
|
this.lastBarUpdateTime = Date.now() / 1000;
|
||||||
|
|
||||||
if (damage <= 0) return;
|
if (damage <= 0) return;
|
||||||
|
|
||||||
@@ -512,7 +513,7 @@ export class HeroViewComp extends CCComp {
|
|||||||
collider.off(Contact2DType.BEGIN_CONTACT);
|
collider.off(Contact2DType.BEGIN_CONTACT);
|
||||||
}
|
}
|
||||||
this.deadCD=0
|
this.deadCD=0
|
||||||
this.hpBarShowCD=0
|
this.lastBarUpdateTime=0
|
||||||
|
|
||||||
// 清理伤害队列
|
// 清理伤害队列
|
||||||
this.damageQueue.length = 0;
|
this.damageQueue.length = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user