refactor(talent): 拆分天赋项组件并适配精灵图标
将原TalentsComp内的单个天赋项UI更新逻辑抽离为独立的TalentItemComp组件实现代码解耦,更新天赋配置将emoji图标替换为精灵图集资源键,重构TalentsComp的天赋列表渲染逻辑适配新的组件化方案
This commit is contained in:
115
assets/script/game/map/TalentItemComp.ts
Normal file
115
assets/script/game/map/TalentItemComp.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* @file TalentItemComp.ts
|
||||
* @description 单个天赋项组件
|
||||
*/
|
||||
import { _decorator, Node, Label, Button, resources, SpriteAtlas, Sprite } 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 { TalentInfo, TalentType } from "../common/config/TalentSet";
|
||||
import { smc } from "../common/SingletonModuleComp";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('TalentItemComp')
|
||||
@ecs.register('TalentItem', false)
|
||||
export class TalentItemComp extends CCComp {
|
||||
|
||||
@property({ type: Label, tooltip: "天赋名称" })
|
||||
lbl_name: Label = null!;
|
||||
|
||||
@property({ type: Node, tooltip: "图标节点" })
|
||||
icon_node: Node = null!;
|
||||
|
||||
@property({ type: Label, tooltip: "描述" })
|
||||
lbl_desc: Label = null!;
|
||||
|
||||
@property({ type: Label, tooltip: "等级进度" })
|
||||
lbl_level: Label = null!;
|
||||
|
||||
@property({ type: Label, tooltip: "升级消耗" })
|
||||
lbl_cost: Label = null!;
|
||||
|
||||
@property({ type: Button, tooltip: "升级按钮" })
|
||||
btn_upgrade: Button = null!;
|
||||
|
||||
@property({ type: Node, tooltip: "背景" })
|
||||
item_bg: Node = null!;
|
||||
|
||||
@property({ type: Node, tooltip: "图标背景" })
|
||||
icon_bg: Node = null!;
|
||||
|
||||
private _talentId: TalentType = TalentType.Attack;
|
||||
private _onClickCallback: ((talentId: TalentType, currentLevel: number) => void) | null = null;
|
||||
private _currentLevel: number = 0;
|
||||
|
||||
protected onLoad(): void {
|
||||
if (this.btn_upgrade && this.btn_upgrade.node) {
|
||||
this.btn_upgrade.node.on(Button.EventType.CLICK, this.onUpgradeClicked, this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新天赋项显示
|
||||
* @param talentInfo 天赋配置数据
|
||||
* @param currentLevel 当前等级
|
||||
* @param onClickCallback 点击升级按钮的回调
|
||||
*/
|
||||
public updateItem(talentInfo: TalentInfo, currentLevel: number, onClickCallback: (talentId: TalentType, currentLevel: number) => void) {
|
||||
this._talentId = talentInfo.id;
|
||||
this._currentLevel = currentLevel;
|
||||
this._onClickCallback = onClickCallback;
|
||||
|
||||
if (this.lbl_name) {
|
||||
this.lbl_name.string = talentInfo.name;
|
||||
}
|
||||
|
||||
if (this.icon_node && talentInfo.icon) {
|
||||
resources.load("gui/uicons", SpriteAtlas, (err, atlas) => {
|
||||
if (err || !atlas) return;
|
||||
const frame = atlas.getSpriteFrame(talentInfo.icon!);
|
||||
if (frame && this.icon_node && this.icon_node.isValid) {
|
||||
const sprite = this.icon_node.getComponent(Sprite) || this.icon_node.addComponent(Sprite);
|
||||
sprite.spriteFrame = frame;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (this.lbl_desc) {
|
||||
let currentVal = currentLevel === 0 ? 0 : talentInfo.values[currentLevel - 1];
|
||||
this.lbl_desc.string = talentInfo.desc.replace('{value}', currentVal.toString());
|
||||
}
|
||||
|
||||
if (this.lbl_level) {
|
||||
this.lbl_level.string = `${currentLevel}/${talentInfo.maxLevel}`;
|
||||
}
|
||||
|
||||
let isMax = currentLevel >= talentInfo.maxLevel;
|
||||
let cost = isMax ? 0 : (talentInfo.costs[currentLevel] ?? 0);
|
||||
let canUpgrade = !isMax && smc.vmdata.gold >= cost;
|
||||
|
||||
if (this.lbl_cost) {
|
||||
this.lbl_cost.string = isMax ? "已满级" : `${cost}`;
|
||||
}
|
||||
|
||||
if (this.btn_upgrade) {
|
||||
this.btn_upgrade.interactable = canUpgrade;
|
||||
}
|
||||
}
|
||||
|
||||
private onUpgradeClicked() {
|
||||
if (this._onClickCallback) {
|
||||
this._onClickCallback(this._talentId, this._currentLevel);
|
||||
}
|
||||
}
|
||||
|
||||
protected onDestroy(): void {
|
||||
if (this.btn_upgrade && this.btn_upgrade.node && this.btn_upgrade.node.isValid) {
|
||||
this.btn_upgrade.node.off(Button.EventType.CLICK, this.onUpgradeClicked, this);
|
||||
}
|
||||
}
|
||||
|
||||
/** ECS 组件移除时销毁节点 */
|
||||
reset() {
|
||||
this.node.destroy();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user