将天赋图集预加载从天赋界面初始化时提前至游戏公共资源加载阶段,新增全局单例缓存机制替换组件自身的静态缓存,移除冗余的异步加载代码与未使用的导入语句,修正TalentItemComp的初始默认天赋类型为Attack
124 lines
4.2 KiB
TypeScript
124 lines
4.2 KiB
TypeScript
/**
|
|
* @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;
|
|
|
|
private _talentInfo: TalentInfo | null = null;
|
|
|
|
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._talentInfo = talentInfo;
|
|
this._talentId = talentInfo.id;
|
|
this._currentLevel = currentLevel;
|
|
this._onClickCallback = onClickCallback;
|
|
|
|
if (this.lbl_name) {
|
|
this.lbl_name.string = talentInfo.name;
|
|
}
|
|
|
|
// 同步尝试刷新一次图标(如果图集已经缓存过,比如重新打开界面时)
|
|
this.refreshIcon();
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
/** 单独更新图标,供父节点加载完图集后回调或自身更新时调用 */
|
|
public refreshIcon() {
|
|
if (!this._talentInfo || !this.icon_node || !this._talentInfo.icon) return;
|
|
|
|
if (smc.uiconsAtlas) {
|
|
const frame = smc.uiconsAtlas.getSpriteFrame(this._talentInfo.icon);
|
|
if (frame && this.icon_node.isValid) {
|
|
const sprite = this.icon_node.getComponent(Sprite) || this.icon_node.addComponent(Sprite);
|
|
sprite.spriteFrame = frame;
|
|
}
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|