refactor(hero): 移除视图层直接更新逻辑,使用脏标签机制

- 在HeroViewComp中移除hp_show和mp_show的直接调用
- 在HeroAttrsComp中不再直接调用视图层方法
- 使用脏标签机制(dirty flag)来触发UI更新
This commit is contained in:
panw
2025-12-31 15:51:06 +08:00
parent 05b82a912a
commit e1e156fa5b
2 changed files with 28 additions and 18 deletions

View File

@@ -3,7 +3,6 @@ import { Attrs, AttrsType, BType, NeAttrs } from "../common/config/HeroAttrs";
import { BuffConf } from "../common/config/SkillSet"; import { BuffConf } from "../common/config/SkillSet";
import { HeroInfo, AttrSet } from "../common/config/heroSet"; import { HeroInfo, AttrSet } from "../common/config/heroSet";
import { HeroSkillsComp } from "./HeroSkills"; import { HeroSkillsComp } from "./HeroSkills";
import { HeroViewComp } from "./HeroViewComp";
interface talTrigger{ interface talTrigger{
@@ -127,26 +126,32 @@ export class HeroAttrsComp extends ecs.Comp {
if(!isValue){ if(!isValue){
addValue = value * this.Attrs[Attrs.HP_MAX] / 100; addValue = value * this.Attrs[Attrs.HP_MAX] / 100;
} }
let heroView = this.ent.get(HeroViewComp);
if(heroView){ // ✅ 数据层只负责数据修改,不调用视图层
heroView.health(addValue); // let heroView = this.ent.get(HeroViewComp);
} // if(heroView){
// heroView.health(addValue);
// }
this.hp += addValue; this.hp += addValue;
this.hp = Math.max(0, Math.min(this.hp, this.Attrs[Attrs.HP_MAX])); this.hp = Math.max(0, Math.min(this.hp, this.Attrs[Attrs.HP_MAX]));
this.dirty_hp = true; // 标记血量需要更新 this.dirty_hp = true; // ✅ 仅标记需要更新
} }
add_mp(value:number,isValue:boolean){ add_mp(value:number,isValue:boolean){
let addValue = value; let addValue = value;
if(!isValue){ if(!isValue){
addValue = value * this.Attrs[Attrs.MP_MAX] / 100; addValue = value * this.Attrs[Attrs.MP_MAX] / 100;
} }
let heroView = this.ent.get(HeroViewComp);
if(heroView){ // ✅ 数据层只负责数据修改,不调用视图层
heroView.mp_add(addValue); // let heroView = this.ent.get(HeroViewComp);
} // if(heroView){
// heroView.mp_add(addValue);
// }
this.mp += addValue; this.mp += addValue;
this.mp = Math.max(0, Math.min(this.mp, this.Attrs[Attrs.MP_MAX])); this.mp = Math.max(0, Math.min(this.mp, this.Attrs[Attrs.MP_MAX]));
this.dirty_mp = true; // 标记魔法值需要更新 this.dirty_mp = true; // ✅ 仅标记需要更新
} }
add_shield(value:number,isValue:boolean){ add_shield(value:number,isValue:boolean){
let addValue = value; let addValue = value;

View File

@@ -81,7 +81,7 @@ export class HeroViewComp extends CCComp {
collider.enabled = true; // 先禁 collider.enabled = true; // 先禁
collider.group = this.box_group; // 设置为英雄组 collider.group = this.box_group; // 设置为英雄组
} }
},0.1) },0.1)
// let anm = this.node.getChildByName("anm") // let anm = this.node.getChildByName("anm")
// anm.setScale(anm.scale.x*0.8,anm.scale.y*0.8); // anm.setScale(anm.scale.x*0.8,anm.scale.y*0.8);
@@ -312,17 +312,22 @@ export class HeroViewComp extends CCComp {
} }
health(hp: number = 0) { health(hp: number = 0) {
// 生命值更新由 Model 层处理,这里只负责视图表现 // ✅ 仅显示特效和提示,不调用 hp_show()
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.hp_show();
// ❌ 移除UI更新由脏标签机制处理
// this.hp_show();
} }
mp_add(mp: number = 0) { mp_add(mp: number = 0) {
// 生命值更新由 Model 层处理,这里只负责视图表现 // ✅ 仅显示提示,不调用 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.mp_show();
// ❌ 移除UI更新由脏标签机制处理
// this.mp_show();
} }
alive(){ alive(){