- 在 HInfoComp 组件中新增 sell_node 属性用于关联出售按钮 - 在 hnode.prefab 中添加出售按钮节点及其子标签节点 - 调整现有节点的位置和缩放以适应新按钮的添加
159 lines
5.1 KiB
TypeScript
159 lines
5.1 KiB
TypeScript
import { _decorator, Animation, AnimationClip, Label, Node, NodeEventType, 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";
|
|
import { oops } from "db://oops-framework/core/Oops";
|
|
import { UIID } from "../common/config/GameUIConfig";
|
|
|
|
const {property, ccclass } = _decorator;
|
|
|
|
/** 视图层对象 */
|
|
@ccclass('HInfoComp')
|
|
@ecs.register('HInfoComp', false)
|
|
export class HInfoComp extends CCComp {
|
|
@property(Node)
|
|
icon_node=null!
|
|
@property(Node)
|
|
sell_node=null!
|
|
private eid: number = 0;
|
|
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();
|
|
this.bindEvents();
|
|
}
|
|
|
|
onDestroy() {
|
|
this.unbindEvents();
|
|
}
|
|
|
|
bindData(eid: number, model: HeroAttrsComp) {
|
|
this.eid = eid;
|
|
this.model = model;
|
|
this.cacheLabels();
|
|
this.refresh();
|
|
}
|
|
|
|
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))}`;
|
|
}
|
|
if (this.hpLabel) {
|
|
this.hpLabel.string = `${Math.max(0, Math.floor(this.model.hp_max ?? 0))}`;
|
|
}
|
|
}
|
|
|
|
isModelAlive(): boolean {
|
|
return !!(this.model as any)?.ent;
|
|
}
|
|
|
|
private cacheLabels() {
|
|
if (!this.apLabel) {
|
|
this.apLabel = this.findLabelByPath(["ap", "val"]);
|
|
}
|
|
if (!this.hpLabel) {
|
|
this.hpLabel = this.findLabelByPath(["hp", "val"]);
|
|
}
|
|
}
|
|
|
|
private findLabelByPath(path: string[]): Label | null {
|
|
let current: Node | null = this.node;
|
|
for (let i = 0; i < path.length; i++) {
|
|
current = current?.getChildByName(path[i]) ?? null;
|
|
if (!current) return null;
|
|
}
|
|
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));
|
|
}
|
|
|
|
private bindEvents() {
|
|
this.node.on(NodeEventType.TOUCH_END, this.onOpenIBox, this);
|
|
}
|
|
|
|
private unbindEvents() {
|
|
this.node.off(NodeEventType.TOUCH_END, this.onOpenIBox, this);
|
|
}
|
|
|
|
private onOpenIBox() {
|
|
if (!this.model) return;
|
|
const heroUuid = this.model.hero_uuid ?? 0;
|
|
if (!heroUuid || !HeroInfo[heroUuid]) return;
|
|
const heroLv = Math.max(1, Math.floor(this.model.lv ?? 1));
|
|
oops.gui.remove(UIID.IBox);
|
|
oops.gui.open(UIID.IBox, {
|
|
heroUuid,
|
|
heroLv,
|
|
skills: this.model.skills
|
|
});
|
|
}
|
|
|
|
/** 视图对象通过 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();
|
|
}
|
|
}
|