refactor(ui): 将英雄等级和动画显示逻辑移至HInfoComp
- 移除HeroViewComp中等级和CD进度条的显示逻辑,简化顶部UI - 在HInfoComp中新增英雄等级显示和动画加载功能 - 添加资源加载和动画管理逻辑,支持英雄闲置动画播放 - 优化组件生命周期管理,确保资源正确释放
This commit is contained in:
@@ -97,11 +97,9 @@ export class HeroViewComp extends CCComp {
|
||||
this.top_node.setScale(this.scale*this.top_node.scale.x,1*this.top_node.scale.y);
|
||||
/* 显示角色血*/
|
||||
this.top_node.getChildByName("hp").active = true;
|
||||
this.top_node.getChildByName("cd").active = true
|
||||
this.top_node.getChildByName("cd").active = false;
|
||||
this.top_node.getChildByName("shield").active = false;
|
||||
this.top_node.getChildByName("lv").active = this.model.fac==FacSet.HERO;
|
||||
this.top_node.getChildByName("lv").getChildByName("lv2").active=this.model.lv >= 2
|
||||
this.top_node.getChildByName("lv").getChildByName("lv3").active=this.model.lv >= 3
|
||||
this.top_node.getChildByName("lv").active = false;
|
||||
this.top_node.active = true;
|
||||
this.setTopBarOpacity(false);
|
||||
|
||||
@@ -162,7 +160,7 @@ export class HeroViewComp extends CCComp {
|
||||
}
|
||||
|
||||
public cd_show(){
|
||||
this.top_node.getChildByName("cd").getComponent(ProgressBar).progress = this.model.getDisplaySkillCdProgress();
|
||||
return;
|
||||
}
|
||||
|
||||
/** 显示护盾 */
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { _decorator, Label, Node } from "cc";
|
||||
import { _decorator, Animation, AnimationClip, Label, Node, Sprite, resources } from "cc";
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
|
||||
import { HeroInfo } from "../common/config/heroSet";
|
||||
import { HeroAttrsComp } from "../hero/HeroAttrsComp";
|
||||
|
||||
const {property, ccclass } = _decorator;
|
||||
@@ -15,6 +16,8 @@ export class HInfoComp extends CCComp {
|
||||
private model: HeroAttrsComp | null = null;
|
||||
private apLabel: Label | null = null;
|
||||
private hpLabel: Label | null = null;
|
||||
private iconVisualToken: number = 0;
|
||||
private iconHeroUuid: number = 0;
|
||||
|
||||
onLoad() {
|
||||
this.cacheLabels();
|
||||
@@ -29,6 +32,13 @@ export class HInfoComp extends CCComp {
|
||||
|
||||
refresh() {
|
||||
if (!this.model) return;
|
||||
this.updateLevelUI();
|
||||
const heroUuid = this.model.hero_uuid ?? 0;
|
||||
if (heroUuid !== this.iconHeroUuid) {
|
||||
this.iconHeroUuid = heroUuid;
|
||||
this.iconVisualToken += 1;
|
||||
this.updateHeroAnimation(this.icon_node, heroUuid, this.iconVisualToken);
|
||||
}
|
||||
if (this.apLabel) {
|
||||
this.apLabel.string = `${Math.max(0, Math.floor(this.model.ap ?? 0))}`;
|
||||
}
|
||||
@@ -59,8 +69,58 @@ export class HInfoComp extends CCComp {
|
||||
return current.getComponent(Label) || current.getComponentInChildren(Label);
|
||||
}
|
||||
|
||||
private updateLevelUI() {
|
||||
if (!this.model) return;
|
||||
const lvNode = this.node.getChildByName("lv");
|
||||
if (!lvNode) return;
|
||||
lvNode.active = true
|
||||
const lv2 = lvNode.getChildByName("lv2");
|
||||
if (lv2) lv2.active = this.model.lv >= 2;
|
||||
const lv3 = lvNode.getChildByName("lv3");
|
||||
if (lv3) lv3.active = this.model.lv >= 3;
|
||||
}
|
||||
|
||||
private updateHeroAnimation(node: Node, uuid: number, token: number) {
|
||||
if (!node) return;
|
||||
const sprite = node.getComponent(Sprite) || node.getComponentInChildren(Sprite);
|
||||
if (sprite) sprite.spriteFrame = null;
|
||||
const hero = HeroInfo[uuid];
|
||||
if (!hero) {
|
||||
this.clearIconAnimation(node);
|
||||
return;
|
||||
}
|
||||
const anim = node.getComponent(Animation) || node.addComponent(Animation);
|
||||
this.clearAnimationClips(anim);
|
||||
const path = `game/heros/hero/${hero.path}/idle`;
|
||||
resources.load(path, AnimationClip, (err, clip) => {
|
||||
if (err || !clip) return;
|
||||
if (token !== this.iconVisualToken || !this.model || this.model.hero_uuid !== uuid) {
|
||||
return;
|
||||
}
|
||||
this.clearAnimationClips(anim);
|
||||
anim.addClip(clip);
|
||||
anim.play("idle");
|
||||
});
|
||||
}
|
||||
|
||||
private clearIconAnimation(node: Node) {
|
||||
const anim = node?.getComponent(Animation);
|
||||
if (!anim) return;
|
||||
anim.stop();
|
||||
this.clearAnimationClips(anim);
|
||||
}
|
||||
|
||||
private clearAnimationClips(anim: Animation) {
|
||||
const clips = (anim as any).clips as AnimationClip[] | undefined;
|
||||
if (!clips || clips.length === 0) return;
|
||||
[...clips].forEach(clip => anim.removeClip(clip, true));
|
||||
}
|
||||
|
||||
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
|
||||
reset() {
|
||||
this.clearIconAnimation(this.icon_node);
|
||||
this.iconVisualToken = 0;
|
||||
this.iconHeroUuid = 0;
|
||||
this.model = null;
|
||||
this.eid = 0;
|
||||
this.node.destroy();
|
||||
|
||||
Reference in New Issue
Block a user